Home » Node.js

Create PDF file from URL using Node.js and puppeteer API

In this article, we will learn to create PDF file from URL using Node.js and puppeteer API.
Submitted by Godwill Tetah, on May 29, 2019

We will continue using Node.js and puppeteer which is a node library. As we saw in our last article, Puppeteer is a Node library developed by Google and provides a high-level API for developers.

Note: You should have Node.js installed in your PC and puppeteer installed via NPM (node package manager).

If you don't yet have Node.js installed, visit the Node.js official website and download for your PC version.

After the download, you can quickly install the puppeteer module by opening a command prompt window and type: npm I puppeteer

Create PDF file from URL using Node.js and puppeteer API 1

npm will download and install the puppeteer library together with other dependencies and Chromium.

Open a text editor and type the following code and save it with the file name as app.js:

const puppeteer = require ('puppeteer');     // Include puppeteer module
const fs = require ('fs');   // file system Node.js module.
(async function () {
	try {	
		const browser = await puppeteer.launch();   // launch puppeteer API
		const page = await browser.newPage();	
		 //1. Create PDF from URL
		  await page.goto('file:///E:/HDD%2080%20GB%20DATA/CODING%20TUTORIALS%20AND%20CODES/go237.com/go237%20web/New%20design/index.html')
  
		
		await page.emulateMedia ('screen');
		await page.pdf ({
		path: 'testpdf.pdf', // name of your pdf file in directory
		format: 'A4', //  specifies the format
		printBackground: true      // print background property
		});
		console.log ('done');  // console message when conversion  is complete!
		await browser.close();
		process.exit();
	} catch (e) {
		console.log ('our error', e);
	}
	
} ) () ;

The file system module is responsible for handling the properties of the output file, such as name or directory.

The puppeteer API is then launched and it creates a new A4 page with file name testpdf.pdf

Note: At the URL field, you can use any URL of your choice. In my case, I used the url of a website I'm developing on my local host.

If you're using any URL on the world wide web (www), make sure you have internet connection which will enable puppeteer visit the website online and convert the current page to pdf.

Run the code by initiating the file at the command prompt like a regular Node.js file.

Following our code, done will be printed out on the console when the conversion is complete.

Create PDF file from URL using Node.js and puppeteer API 2

The Output pdf file is then stored in the default node modules directory following our code, with name test.pdf.

Create PDF file from URL using Node.js and puppeteer API 3

Output file:

Create PDF file from URL using Node.js and puppeteer API 4

So far, we have seen the three ways we can use to create a pdf file using Node.js and Google puppeteer API.

Isn't it cool !!!

In our next series we'll look at how to perform another powerful task with this awesome API.

Please feel free to drop your comments if you have any problem.



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.