How to Convert PDF to TIFF with JavaScript in NodeJS

Learn how to convert PDF to TIFF using pdfRest PDF to Images API tool with JavaScript
Share this page

Why Convert PDF to TIFF with JavaScript?

The pdfRest PDF to Images API Tool is a powerful resource that allows developers to convert PDF documents into image files, such as TIF, using JavaScript. This tutorial will guide you through the process of sending an API call to convert a PDF to images using JavaScript.

Imagine you have a PDF document containing multiple pages of scanned text, and you need to extract each page as a separate image file for further processing or analysis. By using the PDF to Images API, you can automate this task, saving time and reducing the risk of manual errors.

PDF to TIFF with JavaScript Code Example

// This request demonstrates how to convert a PDF into TIF image files, one per PDF page.
var axios = require('axios');
var FormData = require('form-data');
var fs = require('fs');

// Create a new form data instance and append the file and parameters to it
var data = new FormData();
data.append('file', fs.createReadStream('/path/to/file')); 
data.append('pages', '1-last');
data.append('resolution', '300');
data.append('color_model', 'rgb');
data.append('output', 'pdfrest_tif');

// define configuration options for axios request
var config = {
  method: 'post',
  maxBodyLength: Infinity, // set maximum length of the request body
  url: 'https://api.pdfrest.com/tif', 
  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); 
});


// If you would like to download the file instead of getting the JSON response, please see the 'get-resource-id-endpoint.js' sample.

Source: GitHub

Breaking Down the Code

Let's break down the provided code to understand how it works:

var axios = require('axios');

This line imports the Axios library, which is used to make HTTP requests.

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

This line imports the FormData library, which is used to construct form data for the request.

var fs = require('fs');

This line imports the fs (file system) module, which is used to read files from the local file system.

var data = new FormData();
data.append('file', fs.createReadStream('/path/to/file')); 
data.append('pages', '1-last');
data.append('resolution', '300');
data.append('color_model', 'rgb');
data.append('output', 'pdfrest_tif');

Here, a new FormData instance is created, and the required parameters are appended to it:

  • file: The PDF file to be converted, read from the local file system.
  • pages: Specifies the range of pages to convert. '1-last' means all pages from the first to the last.
  • resolution: The resolution of the output images, set to 300 DPI.
  • color_model: The color model of the output images, set to RGB.
  • output: The prefix for the output file names.
var config = {
  method: 'post',
  maxBodyLength: Infinity, 
  url: 'https://api.pdfrest.com/tif', 
  headers: { 
    'Api-Key': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx', 
    ...data.getHeaders() 
  },
  data : data 
};

This section defines the configuration for the Axios request:

  • method: The HTTP method, set to 'post'.
  • maxBodyLength: Sets the maximum length of the request body to Infinity.
  • url: The API endpoint URL.
  • headers: Includes the API key and headers from the FormData instance.
  • data: The form data to be sent with the request.
axios(config)
.then(function (response) {
  console.log(JSON.stringify(response.data));
})
.catch(function (error) {
  console.log(error); 
});

This section sends the request using Axios and handles the response or any errors. The response data is logged to the console in JSON format.

Beyond the Tutorial

In this tutorial, you learned how to use JavaScript to send an API call to the pdfRest PDF to Images API Tool, converting a PDF document into TIF image files.

To explore more pdfRest API Tools, visit the API Lab. For detailed information about each API endpoint, refer to the API Reference Guide.

Note: This is an example of a multipart API call. Code samples using JSON payloads can be found at 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