How to generate PDF in Nodejs

Hey, did you ever faced a challenge to generate a nice and beautiful pdf’s on you nodejs side.

Ok! you are in the right place to learn about how to create a fantastic pdf’s in your applications. We will be using headless chrome engine called puppeteer

Puppeteer is an excellent tool to convert your html pages to pdf/images. We need to install puppeteer using npm. You can install using the following command.

npm i puppeteer

That’s it. half of your job is already done. Now you need to use in your nodejs server side code. Following code is the basic code for to load a url and generate a pdf of the page.

const puppeteer = require('puppeteer');

(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('', {
waitUntil: 'networkidle2',
await page.pdf({ path: 'hn.pdf', format: 'a4' });

await browser.close();

Easy right!.. That’s it. Now in the places of example URL just give your html page url and you will get a pdf instantly. Now show your magic in your html file. There are lot of options are available in puppeteer. please check here

If you have any doubts please feel free to drop a comment below.

Happy coding….