How to Linearize PDF with JavaScript in NodeJS

Learn how to enable Fast Web View on PDF files with JavaScript using the Linearize PDF API Tool from pdfRest.
Share this page

Why Linearize PDF with JavaScript?

The pdfRest Linearize PDF API Tool is designed to optimize PDFs for faster web viewing by reorganizing the file structure to download only the parts of the PDF that are required for viewing, rather than the entire file.

This tutorial will show how to send an API call to Linearize PDF with JavaScript. For instance, if a user is serving large PDFs on their website, linearizing them would significantly improve the user experience by allowing the PDFs to be viewed almost immediately, without waiting for the entire file to download.

Linearize PDF with JavaScript Code Example

var axios = require('axios');
var FormData = require('form-data');
var fs = require('fs');
// This request demonstrates how to liearize a PDF for fast web viewing.

// Create a new form data instance and append the PDF file and parameters to it
var data = new FormData();
data.append('file', fs.createReadStream('/path/to/file'));
data.append('output', 'pdfrest_linearized_pdf');

// define configuration options for axios request
var config = {
  method: 'post',
  maxBodyLength: Infinity, // set maximum length of the request body
  url: 'https://api.pdfrest.com/linearized-pdf', 
  headers: { 
    'Api-Key': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx', // Replace with your API key
    ...data.getHeaders() // set headers for the request
  },
  data : data // set the data to be sent with the request
};

// send request and handle response or error
axios(config)
.then(function (response) {
  console.log(JSON.stringify(response.data));
})
.catch(function (error) {
  console.log(error); 
});

Reference: pdfRest API Samples on GitHub

Breaking Down the JavaScript Code

The provided code uses the Axios library to send an HTTP POST request to the pdfRest API to linearize a PDF. Here's a breakdown of the code:

var axios = require('axios');
var FormData = require('form-data');
var fs = require('fs');

This snippet imports the necessary modules: Axios for HTTP requests, FormData to create multipart/form-data payloads, and fs to interact with the file system.

var data = new FormData();
data.append('file', fs.createReadStream('/path/to/file'));
data.append('output', 'pdfrest_linearized_pdf');

Here, a new FormData instance is created, and the PDF file to be linearized is appended to the form data along with an output parameter that names the output file.

var config = {
  method: 'post',
  maxBodyLength: Infinity,
  url: 'https://api.pdfrest.com/linearized-pdf', 
  headers: { 
    'Api-Key': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
    ...data.getHeaders()
  },
  data : data
};

The Axios request configuration is defined, including the API endpoint URL, the API key header (which should be replaced with your actual API key), and the form data payload.

axios(config)
.then(function (response) {
  console.log(JSON.stringify(response.data));
})
.catch(function (error) {
  console.log(error); 
});

The Axios instance is then used to send the request. The response is logged to the console if successful, or an error is logged if the request fails.

Beyond the Tutorial

In this tutorial, we've accomplished sending a request to the pdfRest API to linearize a PDF for fast web viewing. Users are encouraged to demo all of the pdfRest API Tools in the API Lab and refer to the API Reference documentation for further exploration of the API's capabilities.

Note: This is an example of a multipart API call. Code samples using JSON payloads can be found at pdfRest API Samples on GitHub.

Generate a self-service API Key now!

Create your FREE API Key to start processing PDFs in seconds, only possible with pdfRest.

Compare Plans
Contact Us