How to Reduce PDF File Size in JavaScript
Due to the secure and shareable nature of the PDF, it has become a favorite format for larger documents such as contracts. However, if you attempt to email these substantial documents, you can run into issues with file size; to solve these issues, the following API can be run in JavaScript to reduce the file size and optimize the content of a PDF.
The first step is to install the jQuery library like so:
bower install jquery
With the installation out of the way, we are ready to call the function with the following code:
var form = new FormData();
form.append("inputFile", fileInput.files[0], "file");var settings = {
"url": "https://api.cloudmersive.com/convert/edit/pdf/optimize/reduce-file-size",
"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);
});
This will return a downloadable PDF file that’s ready for use wherever you need it. To retrieve your personal API key, head to the Cloudmersive website to register for a free account.