Creating PDF from html template in node js

13,326

Solution 1

Puppeteer is the best way for converting HTML to the PDF, and also for web scraping.

A short instruction for generating PDF from HTML You can see here Also, the Chrome DevTools team maintains the library, so it is the best solution. About the page-break. This issue can be solved in the HTML code. There is a style option for solving the issue.

style="page-break-after:always;"

Solution 2

The problem with using PDF converter libraries available on NPM like pdfkit is that, you gonna have to recreate the page structures again in your html templates to get the desired output.

One of the best approach to rendering html and convert to pdf is by using Puppeteer on NodeJs. Puppeteer is a Node library which provides a high-level API to control Chrome or Chromium over the DevTools Protocol. It can be used to generate screenshots and PDFs of html pages in your case.

Share:
13,326
TechChain
Author by

TechChain

I have 4+ of experience of overall experience.Currently working on Hyperledger Fabric blockchain framework. I have strong knowledge of Objective c, Swift.I have developed lot of apps from chat app, finance apps,location & map based apps.

Updated on July 21, 2022

Comments

  • TechChain
    TechChain almost 2 years

    I have a requirement that I need to create a PDF. For me the best way is to do it render html template and create a PDF with any third party lib. I have come across the solution which render HTML with ejs and create pdf with html-pdf. It works fine though but I had a problem with a page break.

    There is a popular module pdfkit. But it uses it own concept and procedure to render pdf. For node it does not render html file but for python it does render html template.

    Please tell me how can i render html template to pdf using pdfKit and also what is the best way to render html and convert to pdf ?

    Thanks

  • NingaCodingTRV
    NingaCodingTRV about 2 years
    Where page-break-after:always; goes?
  • CyberEternal
    CyberEternal about 2 years
    @NingaCodingTRV github.com/puppeteer/puppeteer/issues/5277 could be helpful