0

I need to create PDF files via server side. I have a pug file with the HTML and it's working as expected (the format and the content is perfect). Also, I have a service that gets the HTML from the pug file in a string. The problem is that I'm facing issues using the node-html-pdf library to generate a PDF.

  let config = {
    format: 'Letter',
    orientation: 'portrait',
    type: 'pdf'
  };

 pdf.create(htmlConverted, config).toBuffer((errConvertingHtml, buffer) => {
    if (errConvertingHtml) {
      this.logError(errConvertingHtml, null, `${this.className} createPDF`);
      return this.returnResult(errConvertingHtml, false, null);
    } else {
      const filename = 'Invoice' + '.pdf';
      const base64PDF = buffer.toString('base64');
      const email = new models.EmailModel();
      email.to.email = order.email;
      email.to.name = order.full_name;
      email.template_id = subscriptionTemplate.value;
      email.dynamic_template_data = dynamic_data;
      email.attachments = [
        {
          filename: filename,
          content: base64PDF,
          type: 'application/pdf',
          disposition: 'attachment'
        }
      ];

      let mailIntegration = new integrations.MailIntegration();
      mailIntegration.SendNow(email);
    }
  });

This code works, but the PDF attach in the email doesn't have the right format, it's really small. I can use another library, or another implementation, I don't have any issue with that. Any suggestions or recommendations for this code? I have been searching for another library, but honestly, I haven't found something to convert the HTML string to PDF and then the PDF to a buffer and then to base64. Any help is well received. Thanks, I really appreciate your responses.

Graham
  • 7,431
  • 18
  • 59
  • 84
Jose R. Chacón
  • 99
  • 1
  • 12
  • Just a thought, if the overall format is small, maybe using some CSS code to make it big might help. have you tried this? also, can you share the code resources here as well? – Ayyub Kolsawala Nov 23 '19 at 07:57
  • Yes, I tried that but I note the issue is not with CSS, is the library by itself. The solution for me was to change the library, now I'm using puppeteer and is working as expected. For confidentiality terms, I can't share the resources file, sorry for that. – Jose R. Chacón Nov 26 '19 at 15:52
  • I understand, Thanks :) – Ayyub Kolsawala Nov 26 '19 at 17:01

0 Answers0