DEV Community

Discussion on: React and Puppeteer: Pdf generation (pdf generation api)

Collapse
 
tahliamacghey profile image
TahliaMacghey

How to generate PDF from HTML in React / Node.js app? wiccan spell to break up a relationship

Collapse
 
lwhiteley profile image
Layton Whiteley • Edited

at the core of this article this is achieved by using puppeteer in a Nodejs app

const content = '< html content >'
const browser = await puppeteer.launch();
 const page = await browser.newPage();
    await page.setContent(content, {
      waitUntil: 'networkidle2',
    });

// alternatively: instead of setting the html content
//                       you can visit a page
//  await page.goto('https://example.com', {
//      waitUntil: 'networkidle2',
//    });

    const pdf = await page.pdf({
      format: 'a4',
      printBackground: true,
      preferCSSPageSize: true,
    });
 await browser.close();
Enter fullscreen mode Exit fullscreen mode

Im not sure i understood the question fully but let me know if this answers your question.

Please note that there are many alternatives to creating pdf documents and puppeteer is only one that this article focuses on

Collapse
 
renaud profile image
Renaud πŸ€–

Thanks for your article, I agree and think that Puppeteer is the best way to create HTML to PDF. Unfortunately it is sometimes a little complicated to achieve on a large scale... In the end I used a micro-saas: doppio.sh

Thread Thread
 
lwhiteley profile image
Layton Whiteley

Thanks for the input. I didn't know about this service. I'll check it out