How to convert HTML to PDF in Javascript
1 min readMar 10, 2020
Ah, file format converting, everyone’s favorite subject. What do you say we skip all the hard stuff and just knock this out in a couple of minutes instead? Let’s get to it.
We begin by using this script tag to import our API client:
<script src="https://cdn.cloudmersive.com/jsclient/cloudmersive-document-convert-client.js"></script>
With our API now available for function calls, we shall utilize convertWebHtmlToPdf with this block of code:
var CloudmersiveConvertApiClient = require('cloudmersive-convert-api-client');var defaultClient = CloudmersiveConvertApiClient.ApiClient.instance;// Configure API key authorization: Apikeyvar Apikey = defaultClient.authentications['Apikey'];Apikey.apiKey = 'YOUR API KEY';// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)//Apikey.apiKeyPrefix = 'Token';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);
Whew, that was easy, eh? APIs make everything a snap. Need format conversion for other document types? We have functions that work with DOCX, XLSX, PPTX, legacy versions of these, and many more. The sky is the limit!