←  Back
Home Chevron Icon Our Guides Chevron Icon Convert HTML to PDF using NodeJS & PHP’s package

JS logo

How to convert HTML to PDF in Node.js

Fetchlab provides a Node.js package available for free at https://github.com/fetchlab/fetchlab-nodejs

Documentation

See the full documentation on Fetchlab's documentation.

Installation

You should not require this code directly. Instead, just run:

npm install --save pdfshift
Requirements

Usage

This library needs to be configured with your api_key received when creating an account. Setting it is easy as:

const pdfshift = require('pdfshift')('your_api_key');
const fs = require('fs');

pdfshift.convert('https://www.example.com').then(function
(binary_file) { fs.writeFile('result.pdf', binary_file,
"binary", function () {}) }).catch(function({message,
code, response, errors = null}) {})

We also highly recommend checking for errors after the conversion is made, before processing the document, in order to avoid issues later on. This can be easily handled with requests by doing the following:

pdfshift.convert('https://www.example.com').then(function
(binary_file) { fs.writeFile('result.pdf', binary_file,
"binary", function () {}) }).catch(function({message,
code, response, errors = null}) {})
With a URL

Converting a URL with PDFShift is really easy. All you have to do is send a POST request with the source parameter set to the URL, like the following:

const pdfshift = require('pdfshift')('your_api_key');
const fs = require('fs');