How to Convert PDF to PDF/X with JavaScript

Learn how to Convert to PDF/X using JavaScript to ensure your PDF documents meet compliance requirements.
Share this page

Why Convert to PDF/X with JavaScript?

The pdfRest Convert to PDF/X API Tool allows developers to programmatically convert documents to the PDF/X format, which is a subset of the PDF specification designed for prepress graphics exchange. This standardized format is used to ensure that files are ready for print production with the expectation that they will render the same way across different machines and printing presses.

Using JavaScript to call this API can be particularly useful for web applications that need to automate the process of preparing documents for high-quality printing. For example, a graphic design platform might offer users the ability to convert their projects to PDF/X before sending them off to a professional printer.

Convert to PDF/X with JavaScript Code Example

// This request demonstrates how to convert any of several formats to PDF/X. 
var axios = require('axios');
var FormData = require('form-data');
var fs = require('fs');

// Create a new form data object and append the PDF file and parameters to it
var data = new FormData();
data.append('file', fs.createReadStream('/path/to/file'));
data.append('output_type', 'PDF/X-4'); 
data.append('output', 'pdfrest_pdfx');

// define configuration options for axios request
var config = {
  method: 'post',
  maxBodyLength: Infinity, // set maximum length of the request body
  url: 'https://api.pdfrest.com/pdfx', 
  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: pdf-rest-api-samples on GitHub

Breaking Down the JavaScript Code

The provided JavaScript code uses Axios for making HTTP requests and FormData to construct a multipart/form-data payload, which is required for file uploads. Here's a breakdown of the key components:

var data = new FormData();
data.append('file', fs.createReadStream('/path/to/file'));
data.append('output_type', 'PDF/X-4');
data.append('output', 'pdfrest_pdfx');

This snippet creates a new FormData object and appends the file to be converted and additional parameters. The 'output_type' specifies the version of PDF/X to which the document should be converted. The 'output' parameter is a custom name for the resulting file.

var config = {
  //...
  headers: { 
    'Api-Key': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
    ...data.getHeaders()
  },
  //...
};

The 'headers' object includes the API key required for authentication and the headers from the FormData object. The API key should be replaced with your actual key obtained from pdfRest.

axios(config)
.then(function (response) {
  // Handle success
})
.catch(function (error) {
  // Handle error
});

The Axios library sends the request with the specified configuration. The '.then' method handles a successful response, while '.catch' deals with any errors.

Beyond the Tutorial

By following the above steps, we have successfully sent an API call to convert a document to PDF/X using JavaScript. You can now explore and demo all of the pdfRest API Tools in the API Lab at https://pdfrest.com/apilab/ and refer to the API Reference documentation at https://pdfrest.com/documentation/ for further information and additional capabilities.

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

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