How to Convert PDF Colors with JavaScript in NodeJS

Learn how to use pdfRest Convert PDF Colors API tool with JavaScript to change PDF color profiles
Share this page

Why Convert PDF Colors with JavaScript?

The pdfRest Convert PDF Colors API Tool is a powerful resource for developers looking to manipulate the color profiles of PDF documents programmatically. This tutorial will show you how to send an API call to convert PDF colors using JavaScript, making it easier to integrate this functionality into your web applications or automated workflows.

Imagine you are a graphic designer who needs to ensure that all PDF documents adhere to a specific color profile before sending them to print. Using the Convert PDF Colors API, you can automate this process, ensuring consistency and saving time. This tool can also be useful for developers who need to standardize color profiles in PDFs for web display or archival purposes.

Convert PDF Colors with JavaScript Code Example

// This request demonstrates how to convert PDF colors.
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('color_profile', 'srgb');
data.append('output', 'pdfrest_pdf_with_converted_colors');

// 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-with-converted-colors', 
  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 code to understand how it works:

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

Here, we import the necessary modules: axios for making HTTP requests, FormData for handling form data, and fs for file system operations.

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

We create a new instance of FormData and append the PDF file and parameters to it. The file parameter is the path to the PDF file, color_profile specifies the desired color profile (e.g., 'srgb'), and output defines the output file name.

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

We define the configuration options for the axios request. The method is set to 'post', maxBodyLength is set to Infinity to allow large file uploads, and the url is the endpoint for the Convert PDF Colors API. The headers include the API key and the headers from the FormData instance. Finally, the data is set to the form data we created earlier.

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

We send the request using axios and handle the response or any errors. If the request is successful, the response data is logged to the console. If there is an error, it is also logged to the console.

Beyond the Tutorial

In this tutorial, we demonstrated how to use JavaScript to call the pdfRest Convert PDF Colors API. This is just one of many tools available through the pdfRest API. To explore more, you can demo all of the pdfRest API Tools in the API Lab.

For more detailed information, refer to the API Reference Guide. Note that this example uses a multipart API call. Code samples using JSON payloads can be found here.

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