How to Linearize PDF with PHP

Learn how to use PHP to call the Linearize PDF API Tool from pdfRest to enable Fast Web View on your documents.
Share this page

Why Linearize PDF with PHP?

The pdfRest Linearize PDF API Tool provides a convenient way to optimize PDF files for faster web viewing by rearranging the file structure. This process, known as linearization, allows a PDF to be displayed one page at a time as it is downloaded, which can be particularly useful for large documents that need to be viewed online.

For example, if you have an e-book platform where users can read large PDFs directly in their browsers, using this API to linearize your PDFs would enhance the user experience by allowing immediate access to individual pages without the need to download the entire document first.

This tutorial will demonstrate how to send an API call to the Linearize PDF endpoint using PHP.

Linearize PDF with PHP Code Example

 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
];

$options = [
  'multipart' => [
    [
      'name' => 'file',
      'contents' => Utils::tryFopen('/path/to/file', 'r'),
      'filename' => '/path/to/file',
      'headers' => [
        'Content-Type' => ''
      ]
    ],
    [
      'name' => 'output',
      'contents' => 'pdfrest_linearized_pdf'
    ]
  ]
];

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

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

echo $res->getBody();
?>

Source code reference: pdfRest API Samples on GitHub

Breaking Down the PHP Code

The above PHP code demonstrates how to perform a multipart/form-data POST request to the pdfRest API to linearize a PDF file. Here's a breakdown of the key parts of the code:

require 'vendor/autoload.php';

This line includes the Composer-generated autoloader, which automatically loads the Guzzle HTTP client library.

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

The headers array contains the API key required for authentication with the pdfRest API.

$options = [
  'multipart' => [
    // File part
    [
      'name' => 'file',
      'contents' => Utils::tryFopen('/path/to/file', 'r'),
      'filename' => '/path/to/file',
      'headers' => [
        'Content-Type' => ''
      ]
    ],
    // Output part
    [
      'name' => 'output',
      'contents' => 'pdfrest_linearized_pdf'
    ]
  ]
];

The options array specifies the multipart request payload. The 'file' part includes the file stream, filename, and content type. The 'output' part defines the desired output option.

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

This line creates a new HTTP POST request with the specified endpoint and headers.

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

The client sends the request asynchronously and waits for the response. The response body contains the linearized PDF content.

Beyond the Tutorial

In this tutorial, we've learned how to use PHP to call the pdfRest Linearize PDF API to optimize a PDF for fast web viewing. By sending an API call with the necessary parameters, we've accomplished the task of linearizing a PDF document.

To explore further, you can demo all of the pdfRest API Tools in the API Lab and refer to the API Reference documentation.

Note: This is an example of a multipart API call. Code samples using JSON payloads can be found at pdfRest 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