html-pdf: css and js files do not appear to be pro

2019-07-04 08:09发布

问题:

I am taking a webpage, using jquery to load the page on document.ready, css to format the page, node.js http to retrieve the page from the server, html-pdf to convert the html to a pdf file. From this point, I am sending the file as an email attachment. The ultimate goal is to create an invoice. The issue is that the css and js do not appear to be processing when it is in external files. I would prefer to keep in external files but need jquery to populate the page on document.ready and the css to properly format the page. Also, I added cheerio to the mix to see if that would make sure the processing occurred correctly and it did not help. The code is outlined below:

email sending code:
    var transporter = nodemailer.createTransport({
        service: 'gmail',
        host: this.hostname,
        auth: {
            user: this.smtpusername,
            pass: this.smtppassword
        }
    });

    var mailOptions = {
        from: fromaddress, // sender address
        to: toaddresslist, // list of receivers
        subject: subject, // Subject line
        html: text
    };

    if (attachments){
        mailOptions.attachments = [
            {   // binary buffer as an attachment
                filename: 'invoice.pdf',
                content: attachments,
                contentType: 'application/pdf',
                encoding: 'base64'
            }
        ];
    }

html conversion code:
                let body = [];
                var options = {
                  host: 'localhost',
                  path: '/invoice/' + invoiceid,
                  port: '3000'
                };

                http.request(options, function(response){
                  response.on('data', function (chunk) {
                      body.push(chunk);
                  });

                  response.on('end', function () {
                    let buffer = '';
                    buffer = Buffer.concat(body).toString();
                    let $ = cheerio.load(buffer);
                    let newdocument = $.html();
                    console.log(newdocument);

                    pdf.create(newdocument, {
                        directory: "tmp",
                        format: "letter",
                        orientation: "portrait",
                        zoomFactor: ".5", // default is 1 
                        type: "pdf",
                        quality: "75"
                    }).toBuffer(function(err, newbuffer){
                        if (err){
                            reject(err);
                        }

                        if (Buffer.isBuffer(newbuffer)){
                            resolve(newbuffer);
                        } else {
                            reject(new Error('The pdf file could not be generated at this time.  Please try again later.'));
                        }
                    });
                  });
                })
                .end();

When I check the email, I am getting the pdf, but is does not appear populated with the data I expect from the api call and the formatting is way off. I tried to move the css code inside the document for testing purposes, and though the css was still off, it was clear it was able to see the css in this scenario.

1) How can I get html-pdf package to recognize and process the external css and js files?

2) What type of html formatting does the html-pdf work with, div or tables?

回答1:

Have you tried to use absolute links for your css files ? On the html head section ?

<link rel='stylesheet' type='text/css' href='http://www.url.com/css/my.css' />

use http://localhost:3000 might be the case for you as you are using localhost.

Make sure you also have the paths properly set up in the nodejs server configuration. (otherwise you will get a 404 error).

Edit : I have also found this thread on their repository. https://github.com/marcbachmann/node-html-pdf/issues/13

Hope this helps.



回答2:

Are you using the base option? Is your css and js available on the server? Also is your javascript a long running script, maybe with ajax or anything that takes time? Not sure how good html-pdf is, but it may not wait for js to finish, some libraries have options for that:

   // Rendering options 
      "base": "file:///home/www/your-asset-path", // Base path that's used to load files (images, css, js) when they aren't referenced using a host

https://www.npmjs.com/package/html-pdf