How to convert HTML to PDF in Node.js

To start, first we need to add a reference in package.json:

"dependencies": {
"cloudmersive-convert-api-client": "^1.2.7"
}

Now, we need to load the package with a require:

var CloudmersiveConvertApiClient = require('cloudmersive-convert-api-client');

Finally, we just call the convertWebHtmlToPdf function which will take an HTML string as input, and return a PDF as the result:

var CloudmersiveConvertApiClient = require('cloudmersive-convert-api-client');
var defaultClient = CloudmersiveConvertApiClient.ApiClient.instance;
// Configure API key authorization: Apikey
var Apikey = defaultClient.authentications['Apikey'];
Apikey.apiKey = 'YOUR API KEY';
var apiInstance = new CloudmersiveConvertApiClient.ConvertWebApi();var input = new CloudmersiveConvertApiClient.HtmlToPdfRequest(); // HtmlToPdfRequest | HTML to PDF request parametersvar callback = function(error, data, response) {
if (error) {
console.error(error);
} else {
console.log('API called successfully. Returned data: ' + data);
}
};
apiInstance.convertWebHtmlToPdf(input, callback);

Simply fill in the appropriate field in the input parameter above an input string containing valid HTML. Images can also be included (absolute URLs are recommended).

It’s that easy!

Written by

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store