The pdfRest Convert PDF Colors API Tool is a valuable resource for developers working with PDF documents that require precise color control, especially when converting from RGB to CMYK. This tutorial will guide you through the process of using JavaScript to send an API call to the Convert PDF Colors endpoint, allowing you to effortlessly achieve this color profile conversion.
Converting RGB (Red, Green, Blue) colors to CMYK (Cyan, Magenta, Yellow, Black) is essential for preparing PDF documents for printing. CMYK is the color model used by most printers, and accurately converting RGB colors to CMYK ensures that the printed colors match the intended appearance. This is particularly important for projects like print advertising, product catalogs, and packaging design where color accuracy is paramount.
// 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', 'acrobat9-cmyk'); 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
The code begins by requiring 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', 'acrobat9-cmyk'); data.append('output', 'pdfrest_pdf_with_converted_colors');
Here, a new FormData
instance is created. The PDF file is appended using fs.createReadStream('/path/to/file')
, which reads the file from the specified path. The color_profile
parameter is set to 'acrobat9-cmyk'
, indicating the desired color profile conversion. The output
parameter specifies 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 };
The config
object defines the Axios request configuration. The method
is set to 'post'
, and maxBodyLength
is set to Infinity
to accommodate large files. The url
is the endpoint for the Convert PDF Colors tool. The headers
include the API key (replace with your actual key) and headers from the form data. The request data
is the form data instance created earlier.
axios(config) .then(function (response) { console.log(JSON.stringify(response.data)); }) .catch(function (error) { console.log(error); });
The axios
function sends the request using the specified configuration. If successful, the response data is logged to the console. If an error occurs, it is caught and logged.
In this tutorial, you learned how to use JavaScript to interact with the pdfRest Convert PDF Colors API Tool. This example can be a starting point for automating PDF color conversions in your projects. For further exploration, try out all the pdfRest API Tools in the API Lab. For more detailed information, 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 Repository.
Create your FREE API Key to start processing PDFs in seconds, only possible with pdfRest.