Convert a DOCX File to PDF in JavaScript
Converting DOCX files into the PDF format is quite the challenge. Due to the complexity of the Word format, the initial parsing of the document can be incredibly time consuming, and once it’s complete you still have to render it, rasterize it, and convert it to PDF. In this brief tutorial, I will demonstrate how to use an API in JavaScript to automatically perform the conversion and avoid the headache you would get completing the operation manually.
First, we install the jQuery library using this command here:
bower install jquery
Then, we can call the conversion function with the following code:
var form = new FormData();
form.append("inputFile", fileInput.files[0], "file");var settings = {
"url": "https://api.cloudmersive.com/convert/docx/to/pdf",
"method": "POST",
"timeout": 0,
"headers": {
"Content-Type": "multipart/form-data",
"Apikey": "YOUR-API-KEY-HERE"
},
"processData": false,
"mimeType": "multipart/form-data",
"contentType": false,
"data": form
};$.ajax(settings).done(function (response) {
console.log(response);
});
Much easier, right? If you need to obtain an API key for the function, you can do so by registering for a free account on the Cloudmersive website; this will provide access to 800 calls/month across our library of APIs.