Webpage to PDF in JavaScript using PDF.co Web API

In this tutorial, we will show you how to convert a webpage to PDF in JavaScript using PDF.co Web API. Below is the image of Wikipedia’s main page and its PDF version after conversion.

Webpage Input
Webpage Input
PDF Output
PDF Output

Step 1: Source Code

First, open the source code in your preferred editor and save it as app.js in a folder. You can get a copy of the source code here.

Step 2: Add API Key

In line 21, use your PDF.co API Key as the API_Key variable’s value. You can get it here.

Add PDF.co API Key

Step 3: Source and Destination File

In line 25, enter the Wikipedia link https://en.wikipedia.org/wiki/Main_Page as the source link and its destination file in line 27.

Step 4: Run Program

Now, type in node app.js in the Terminal. Be sure to set the path where the file is stored.

Run Program

Step 5: Output File

After the program confirms that the PDF file has been generated, open the folder to see the result.

PDF Output Generated

In this tutorial, you learned how to convert a webpage to PDF in JavaScript using PDF.co Web API. You also learned how to set up the PDF.co URL to PDF parameters to get the result right away.