marcbachmann / node-html-pdf

This repo isn't maintained anymore as phantomjs got dreprecated a long time ago. Please migrate to headless chrome/puppeteer.
MIT License
3.56k stars 544 forks source link

Pdf saved in TMP directory #697

Open sumantripathi opened 1 year ago

sumantripathi commented 1 year ago

Hello All,

I have used npm package 'html-pdf' to generate and downloaded a pdf through streaming . But I wonder why my generated pdf stored in TMP directory with name 'html-pdf-$someid'.

Any help will be highly appreciated please . Thanks

czarjulius commented 1 year ago

Hi @sumantripathi I used the same html-pdf package to generate a pdf for the customer's receipt. I also downloaded it and stored it in a directory I called pdf and it works just fine. This is how I did it.

import ejs from "ejs";
import pdf from "html-pdf";

const options = {
  height: "11.25in",
  width: "8.5in",
  header: {
    height: "2mm",
  },
  footer: {
    height: "10mm",
  },
};

export const generatePDF = async (details, ejsFileName, pdfFileName) => {
  return new Promise((resolve, reject) => {
    const ejsFilePath = `${process.cwd()}/src/assets/mails/${ejsFileName}.ejs`;
    ejs.renderFile(ejsFilePath, { details }, null, (err, data) => {
      if (err) {
        console.log("Error rendering EJS file:", err);
        reject(err);
      } else {
        const pdfFilePath = `${process.cwd()}/src/assets/pdf/${pdfFileName}.pdf`;
        pdf.create(data, options).toFile(pdfFilePath, (err) => {
          if (err) {
            console.log("Error generating PDF:", err);
            reject(err);
          } else {
            console.log("PDF generation successful.");
            resolve(true);
          }
        });
      }
    });
  });
};
Screenshot 2023-08-10 at 1 33 54 PM

I hope this helps