0

So with puppeteer I'm generating the pdf in my server and it works just fine, but I also want to add another function that after I generate the PDF i send the file back to the user and the download starts via API.

So here is my function:

function createPdf async (req, res) => {
    const browser = await puppeteer.launch();
    const page = await browser.newPage();
    await page.goto('http://localhost:3000', {
      timeout: 10000,
      waitUntil: ['load', 'domcontentloaded', 'networkidle0', 'networkidle2'],
    });
    await page.pdf({
      path: `./invoices/${Math.random()}.pdf`,
      landscape: false,
      format: 'A4',
      margin: {
        top: '0px',
        right: '0px',
        bottom: '0px',
        left: '0px',
      },
    });
    await browser.close();
    if (page)
      res.status(200).send({
        success: true,
      });
  },

How can I do that?

ggorlen
  • 33,459
  • 6
  • 59
  • 67
cutrimafyi
  • 67
  • 4
  • Does this answer your question? [Download a file from NodeJS Server using Express](https://stackoverflow.com/questions/7288814/download-a-file-from-nodejs-server-using-express) – ggorlen Mar 31 '22 at 14:45

0 Answers0