How to Linearize & Optimize a PDF in Node.js

Cloudmersive
2 min readOct 2, 2023

--

Linearized PDFs are much easier to view on the internet because they allow a server to download one page at a time.

Using the below code, you can take advantage of a free API that will quickly linearize your PDF documents and optimize them for internet streaming/download. You’ll just need a free-tier API key to authorize your requests, and you can make up to 800 API calls per month with no additional commitments.

Your first step is to install the SDK. You can either run this command:

npm install cloudmersive-convert-api-client --save

Or you can add this snippet to your package.json:

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

Lastly, you can copy the below code directly into your file to structure your request, and then you can load in your PDF contents as form-data:

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.EditPdfApi();

var inputFile = Buffer.from(fs.readFileSync("C:\\temp\\inputfile").buffer); // File | Input file to perform the operation on.


var callback = function(error, data, response) {
if (error) {
console.error(error);
} else {
console.log('API called successfully. Returned data: ' + data);
}
};
apiInstance.editPdfLinearize(inputFile, callback);

No more code required — nice and simple.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

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

No responses yet