How to Convert TIFF to PDF with JavaScript in NodeJS

Learn how to turn TIFF images into PDF documents by calling the Convert to PDF API Tool from pdfRest.
Share this page

Why Use Convert to PDF with JavaScript?

The pdfRest Convert to PDF API Tool provides a convenient way to programmatically convert various file formats to PDF. This is particularly useful in web applications where users may need to convert documents for uniformity, archiving, or compliance with certain standards. For instance, a user might want to convert TIFF images to PDF to ensure compatability with a wide array of viewers.

Convert to PDF with JavaScript Code Example

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

// 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_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/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); 
});

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

Reference: GitHub - pdf-rest-api-samples

Breaking Down the Code

The code uses the Axios library to make HTTP requests and the FormData library to construct multipart/form-data payloads. Here's a breakdown of the key parts:

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

This section creates a new FormData object and appends different fields to it, including the file to be converted and any conversion parameters.

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

The config object defines the HTTP request configuration, including the API endpoint, headers (with your API key), and the data payload.

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

This snippet sends the request and handles the response or error. On success, it logs the response data, which would typically include information about the converted PDF.

Beyond the Tutorial

In this tutorial, we've seen how to send an API call to the pdfRest Convert to PDF API using JavaScript. By following the steps outlined, you can integrate PDF conversion into your applications, enhancing their functionality and user experience. To explore all of the pdfRest API Tools, visit the API Lab at https://pdfrest.com/apilab/, and for more detailed information, refer to the API Reference documentation at https://pdfrest.com/documentation/.

Note: This is an example of a multipart API call. Code samples using JSON payloads can be found at GitHub - pdf-rest-api-samples.

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