How to Convert PDF to PNG with PHP

Learn how to use pdfRest PDF to Images API Tool with PHP to convert PDF pages to PNG images
Share this page

Why Convert PDF to PNG with PHP?

The pdfRest PDF to Images API Tool is a powerful and flexible solution for converting PDF documents into image formats like PNG, JPEG, and others. By leveraging this tool, developers can easily integrate PDF to image conversion capabilities into their PHP applications. This tutorial will demonstrate how to send an API call to the PDF to Images endpoint using PHP, specifically focusing on converting a PDF to PNG.

An example of why a user might use PDF to Images is for generating thumbnails or previews of PDF documents. For instance, a document management system might need to display a preview of each page of a PDF document to its users. By converting the PDF pages into images, the system can provide a quick and visually appealing way for users to browse through documents without needing to open the entire PDF file.

PDF to PNG 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 the '/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_png' // Set the value for the output option (in this case, 'pdfrest_png').
    ]
  ]
];

$request = new Request('POST', 'https://api.pdfrest.com/png', $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 PNG image.

Source of the provided code: GitHub

Breaking Down the Code

require 'vendor/autoload.php';
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Utils;

This section imports the necessary libraries. The vendor/autoload.php file is required to load the Guzzle HTTP client, and the use statements import the necessary classes from the Guzzle library.

$client = new Client();

This line creates a new instance of the Guzzle HTTP client, which will be used to send the API request.

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

The $headers array sets the API key for authentication. Replace xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx with your actual API key.

$options = [
  'multipart' => [
    [
      'name' => 'file',
      'contents' => Utils::tryFopen('/path/to/file', 'r'),
      'filename' => '/path/to/file',
      'headers' => [
        'Content-Type' => ''
      ]
    ],
    [
      'name' => 'pages',
      'contents' => '1-last'
    ],
    [
      'name' => 'resolution',
      'contents' => '300'
    ],
    [
      'name' => 'color_model',
      'contents' => 'rgb'
    ],
    [
      'name' => 'output',
      'contents' => 'pdfrest_png'
    ]
  ]
];

The $options array configures the multipart form data for the API request. Each element in the multipart array represents a form field:

  • file: Specifies the file to be converted. Utils::tryFopen('/path/to/file', 'r') opens the file for reading, and 'filename' => '/path/to/file' sets the filename. Replace /path/to/file with the actual path to your PDF file.
  • pages: Specifies the pages to convert, in this case, all pages from the first to the last.
  • resolution: Sets the resolution for the output images, here it is set to 300 DPI.
  • color_model: Sets the color model for the output images, in this case, RGB.
  • output: Specifies the output format, which is pdfrest_png for PNG images.
$request = new Request('POST', 'https://api.pdfrest.com/png', $headers);

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

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

The sendAsync method sends the asynchronous request, and wait waits for the response.

echo $res->getBody();

This line outputs the response body, which contains the converted PNG image.

Beyond the Tutorial

In this tutorial, we demonstrated how to use the pdfRest PDF to Images API Tool to convert a PDF document into PNG images using PHP. By following the steps outlined above, you can integrate similar functionality into your own PHP applications.

We encourage you to explore all of the pdfRest API Tools in the API Lab. For more detailed information, refer to the API Reference Guide.

Note: This is an example of a multipart API call. 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.