How to Convert PDF to TIFF with PHP

Learn how to convert PDF pages to TIFF images using pdfRest PDF to Images API tool with PHP
Share this page

Why Convert PDF to TIFF with PHP?

The pdfRest PDF to Images API Tool allows developers to convert PDF documents into image formats like TIF, PNG, or JPEG. This tutorial will show how to send an API call to PDF to Images with PHP, leveraging the Guzzle HTTP client to handle the request and response.

Imagine you have a web application that processes PDF documents and needs to display individual pages as images for better user interaction. By converting PDF pages to images, you can easily integrate these images into your web pages, making them more accessible and user-friendly. This is particularly useful for applications like document management systems, online libraries, or any service that requires visual representation of PDF content.

PDF to TIFF with PHP Code Example

require 'vendor/autoload.php'; // Require the autoload file to load Guzzle HTTP client.

use GuzzleHttp\Client; // Import the Guzzle HTTP client namespace.
use GuzzleHttp\Psr7\Request; // Import the PSR-7 Request class.
use GuzzleHttp\Psr7\Utils; // Import the PSR-7 Utils class for working with streams.

$client = new Client(); // Create a new instance of the Guzzle HTTP client.

$headers = [
  'Api-Key' =--> 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' // Set the API key in the headers for authentication.
];

$options = [
  'multipart' => [
    [
      'name' => 'file', // Specify the field name for the file.
      'contents' => Utils::tryFopen('/path/to/file', 'r'), // Open the file specified by '/path/to/file' for reading.
      'filename' => '/path/to/file', // Set the filename for the file to be converted, in this case, '/path/to/file'.
      'headers' => [
        'Content-Type' => '' // Set the Content-Type header for the file.
      ]
    ],
    [
      'name' => 'pages', // Specify the field name for the pages option.
      'contents' => '1-last' // Set the value for the pages option (in this case, '1-last').
    ],
    [
      'name' => 'resolution', // Specify the field name for the resolution option.
      'contents' => '300' // Set the value for the resolution option (in this case, '300').
    ],
    [
      'name' => 'color_model', // Specify the field name for the color_model option.
      'contents' => 'rgb' // Set the value for the color_model option (in this case, 'rgb').
    ],
    [
      'name' => 'output', // Specify the field name for the output option.
      'contents' => 'pdfrest_tif' // Set the value for the output option (in this case, 'pdfrest_tif').
    ]
  ]
];

$request = new Request('POST', 'https://api.pdfrest.com/tif', $headers); // Create a new HTTP POST request with the API endpoint and headers.

$res = $client->sendAsync($request, $options)->wait(); // Send the asynchronous request and wait for the response.

echo $res->getBody(); // Output the response body, which contains the converted TIF image.

Source: GitHub

Breaking Down the Code

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' => '']]
    This specifies the file to be uploaded. Replace '/path/to/file' with the actual file path and '' with the appropriate content type.
  • ['name' => 'pages', 'contents' => '1-last']
    This specifies the range of pages to convert. '1-last' means all pages.
  • ['name' => 'resolution', 'contents' => '300']
    This sets the resolution of the output images to 300 DPI.
  • ['name' => 'color_model', 'contents' => 'rgb']
    This sets the color model of the output images to RGB.
  • ['name' => 'output', 'contents' => 'pdfrest_tif']
    This specifies the output format, which is TIF in this case.

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

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

echo $res->getBody();

Beyond the Tutorial

In this tutorial, we demonstrated how to use PHP and the Guzzle HTTP client to send a request to the pdfRest PDF to Images API Tool and convert a PDF document to TIF images. This process can be adapted to handle other image formats and additional options provided by the API.

We encourage you to explore all the pdfRest API Tools in the API Lab. For more detailed information, refer to the API Reference Guide. Note that this example uses a multipart API call, and code samples using JSON payloads can be found at GitHub.

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