How to Convert PDF Files from Color to Black and White (Grayscale) with JavaScript in NodeJS
Why Convert PDFs from Color to Black and White with JavaScript?
The pdfRest Convert PDF Colors API Tool is a powerful utility that allows users to change the color profile of a PDF document. This tutorial will demonstrate how to send an API call to convert a color PDF to grayscale using JavaScript, providing a practical example of integrating this functionality into your applications. By understanding this process, developers can automate color conversion tasks, ensuring PDFs meet specific color standards or requirements.
A user might need to convert a PDF to black and white for various reasons, such as reducing file size, improving readability on certain devices, or preparing documents for printing. For example, a document preparation specialist might need to convert a colorful PDF report into a grayscale version for archival purposes or to reduce printing costs. This tool provides a streamlined way to handle such conversions programmatically.
Convert PDFs from Color to Black and White with JavaScript Code Example
// This request demonstrates how to reduce the file size of a PDF. 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', 'gamma-22'); 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 Repository
Breaking Down the Code
The code begins by importing necessary modules: axios
for making HTTP requests, form-data
for handling form submissions, and fs
for file system operations.
var data = new FormData(); data.append('file', fs.createReadStream('/path/to/file')); data.append('color_profile', 'gamma-22'); data.append('output', 'pdfrest_pdf_with_converted_colors');
Here, a new FormData
instance is created, and the PDF file is appended using fs.createReadStream()
. The color_profile
parameter is set to 'gamma-22', indicating the desired color profile for conversion. The output
parameter specifies the filename for the converted PDF.
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 };
The config
object defines the request configuration for axios
. It specifies the HTTP method as 'POST', sets the request URL, and includes headers with the API key and form data headers. The maxBodyLength
is set to Infinity
to accommodate large files.
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
This tutorial demonstrated how to use JavaScript to call the pdfRest Convert PDF Colors API, allowing you to automate PDF color profile conversions. To explore more possibilities, you can demo all pdfRest API Tools in the API Lab. For further details, refer to the API Reference Guide.
Note: This example is a multipart API call. Code samples using JSON payloads can be found at this GitHub repository.