How to Convert PDF to PDF/X with PHP

Learn how to use PHP to call the Convert to PDF/X API Tool from pdfRest and ensure your documents are compliant.
Share this page

Why Convert to PDF/X with PHP?

The pdfRest Convert to PDF/X API Tool is a powerful resource for converting documents to the PDF/X format, which is a subset of the PDF specification intended for prepress graphics exchange. This API tool ensures that the PDF files conform to the standards required for high-quality printing and publishing.

In this tutorial, we will demonstrate how to send an API call to Convert to PDF/X with PHP, which is particularly useful for automating the conversion process in web applications or document management systems. For example, a printing service provider might use this API to ensure all incoming PDF documents are standardized to PDF/X before proceeding with the printing process.

Convert to PDF/X with PHP Code Example

<?php
require 'vendor/autoload.php';

use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Utils;

$client = new Client();

$headers = [
  'Api-Key' => 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
];

$options = [
  'multipart' => [
    [
      'name' => 'file',
      'contents' => Utils::tryFopen('/path/to/file', 'r'),
      'filename' => '/path/to/file',
      'headers' => [
        'Content-Type' => '<Content-type header>'
      ]
    ],
    [
      'name' => 'output_type',
      'contents' => 'PDF/X-4'
    ],
    [
      'name' => 'output',
      'contents' => 'pdfrest_pdfx'
    ]
  ]
];

$request = new Request('POST', 'https://api.pdfrest.com/pdfx', $headers);

$res = $client->sendAsync($request, $options)->wait();

echo $res->getBody();
?>

Source of the provided code: pdf-rest-api-samples on GitHub.

Breaking Down the PHP Code

The provided PHP code uses the Guzzle HTTP client to send a multipart/form-data POST request to the pdfRest API. Here's a breakdown of the key components:

$headers = [
  'Api-Key' => 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
];

This sets the API key required for authentication with the pdfRest API.

$options = [
  'multipart' => [
    // ...multipart array...
  ]
];

The $options array contains the multipart payload, which includes the file to convert and the conversion parameters:

  • 'name' => 'file': The field name for the uploaded file.
  • 'contents' => Utils::tryFopen('/path/to/file', 'r'): The file stream opened for reading.
  • 'filename' => '/path/to/file': The path to the file to be converted.
  • 'Content-Type': The MIME type of the file being uploaded.
  • 'name' => 'output_type': The desired PDF/X standard for the output file.
  • 'name' => 'output': The output option to specify the naming convention for the processed file.

Beyond the Tutorial

In this tutorial, we've learned how to use PHP to call the pdfRest Convert to PDF/X API Tool. By sending a multipart/form-data POST request with the necessary parameters, we can convert documents to the PDF/X format suitable for high-quality printing workflows. Users are encouraged to 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 more information.

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