How to Convert PDF Files from RGB to CMYK with JavaScript in NodeJS

Learn how to convert PDF colors from RGB to CMYK using pdfRest API Toolkit with JavaScript
Share this page

Convert RGB PDF Content to CMYK with Node.js

PDFs created for screens often use RGB color, while many commercial printing workflows expect CMYK output prepared for a defined printing condition. A simple label change is not enough: the color values must be transformed so the printed result represents the source colors as accurately as the selected profile allows. The pdfRest Convert PDF Colors API Tool performs that conversion across PDF text, images, and other color-managed content through the /pdf-with-converted-colors endpoint.

pdfRest provides preset color profiles for common RGB, CMYK, grayscale, and black-and-white workflows and also accepts custom ICC profiles when a printer or production standard requires one. The conversion is powered by Adobe PDF Library technology, giving applications a consistent server-side process without requiring local PDF rendering and color-management libraries.

For example, a Node.js web-to-print service can receive a marketing brochure exported in RGB, convert it with a CMYK preset before sending it to a commercial print queue, and retain the output resource ID for a later preflight or PDF/X step. Automating the conversion makes the preparation rule repeatable, while a proofing step confirms that brand colors and images remain acceptable for the intended press condition.

This example streams a local PDF through Axios and FormData, selects the acrobat9-cmyk preset, and prints the JSON response.

JavaScript RGB-to-CMYK Code Example

Install axios and form-data, then replace the API-key and local PDF path placeholders. Keep the profile value in application configuration when different print destinations require different output conditions.

// This request demonstrates how to convert PDF colors from RGB to CMYK.
var axios = require('axios');
var FormData = require('form-data');
var fs = require('fs');

// By default, we use the US-based API service. This is the primary endpoint for global use.
var apiUrl = "https://api.pdfrest.com";

/* For GDPR compliance and enhanced performance for European users, you can switch to the EU-based service by uncommenting the URL below.
 * For more information visit https://pdfrest.com/pricing#how-do-eu-gdpr-api-calls-work
 */
//var apiUrl = "https://eu-api.pdfrest.com";

// 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: apiUrl + '/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: pdfRest Convert PDF Colors multipart JavaScript sample

Select the Output Color Profile

The color_profile field controls the conversion target. This tutorial uses the built-in acrobat9-cmyk preset to demonstrate an RGB-to-CMYK request. A preset is convenient when its intended color condition matches the workflow. When a print provider supplies a specific ICC profile, use the custom-profile option documented for the endpoint rather than assuming a generic CMYK profile will produce the required result.

Color conversion can change appearance because RGB and CMYK have different reproducible color ranges. Highly saturated screen colors may not have exact CMYK equivalents. The API performs the transformation, but the application should still validate representative output against the delivery requirements. Profile selection is a production decision, not merely a code setting.

data.getHeaders() supplies the multipart boundary required by the request. The PDF stream prevents Node.js from loading the complete input into memory, and maxBodyLength: Infinity avoids Axios rejecting the request because of its client-side body-size default.

Validate and Continue the Print Workflow

Check the response status and expected output fields before saving or forwarding the PDF. For production use, add a timeout, distinguish connection failures from API validation errors, and prevent documents or credentials from being written to shared logs. Inspect a proof or preflight result for imagery, vector artwork, gradients, transparency, spot colors, and small text that matters to the job.

The response resource ID can be passed directly to another pdfRest operation, such as converting the result to PDF/X, without an intermediate download and upload. This makes color conversion one controlled stage in a larger automated print-preparation pipeline.

When the source PDF is already uploaded, use a JSON payload with its resource ID. See the official Convert PDF Colors JSON-payload JavaScript sample. Try presets in API Lab and consult the Convert PDF Colors API reference for the current profile and custom-ICC parameters.

Generate a self-service API Key now!
Create your FREE API Key to start processing PDFs in seconds, only possible with pdfRest.