How to Convert PNG to PDF in PHP

Learn how to convert PNG images into PDF documents in PHP by calling the Convert to PDF API Tool from pdfRest.
Share this page

Why Use Convert to PDF with PHP?

The pdfRest Convert to PDF API Tool is a powerful resource that allows developers to convert various file formats into PDFs programmatically. By integrating this API, PHP developers can automate the process of converting documents to PDF, which is a widely accepted format for sharing and archiving files.

For instance, a user might need to convert a batch of PNG images into PDFs for a presentation or ensure that invoices generated by their application are in a format that is both print-ready and universally accessible.

Convert to PDF with PHP Code Example


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

$request = new Request('POST', '', $headers);

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

echo $res->getBody();

Source of the provided code: GitHub

Breaking Down the Code

The code begins by loading the required Guzzle HTTP client library and setting up the necessary namespaces. A new client instance is created, and the API key is set in the headers for authentication:

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

The options array is constructed with multipart form data, specifying the file to be converted and various conversion options:

'options' => [
  'multipart' => [
    // ...multipart form data...

Each multipart entry includes a 'name' and 'contents' key. The 'file' entry also includes a 'filename' and 'headers' key to specify the file's content type. The other entries set conversion options like 'compression', 'downsample', 'tagged_pdf', and 'output'.

A new POST request is created with the API endpoint and headers, and the request is sent asynchronously. The response body is then echoed, which contains the processed PDF content.

Beyond the Tutorial

This tutorial demonstrated how to make an API call to the pdfRest Convert to PDF API using PHP. By sending a multipart request, we've instructed the API to convert a file to PDF with specific options such as compression and downsampling settings.

To explore and demo all of the pdfRest API Tools, visit the API Lab at For more detailed information on the API and its capabilities, refer to the API Reference documentation at

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.

Compare Plans
Contact Us