How to convert HTML to PDF in Node.js
1 min readMay 16, 2019
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!