'Puppeteer: Sending the PDF for download after is generated

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?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source