How to Convert HTML to PDF with PHP

Learn how to use PHP to convert HTML to PDF with Convert to PDF API by pdfRest
Share this page

Why Convert HTML to PDF with PHP?

The pdfRest Convert to PDF API Tool offers a powerful and efficient solution for converting HTML files into high-quality PDFs using PHP. This tutorial will walk you through the process of sending an API call to convert HTML content to PDF with PHP. By leveraging the pdfRest API, developers can easily integrate HTML-to-PDF conversion into their applications, streamlining workflows and enhancing the user experience.

For example, if your business generates dynamic reports, invoices, or web pages in HTML and needs to provide them as downloadable PDFs, the Convert to PDF API tool makes this process straightforward. Developers can automate the conversion of HTML files to PDF directly from their PHP applications, saving time and ensuring consistent, professional results for document distribution and archiving.

Convert to PDF 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 processed, in this case, '/path/to/file'.
      'headers' => [
        'Content-Type' => '' // Set the Content-Type header for the file.
      ]
    ],
    [
      'name' => 'compression', // Specify the field name for the compression option.
      'contents' => 'lossless' // Set the value for the compression option (in this case, 'lossless').
    ],
    [
      'name' => 'downsample', // Specify the field name for the downsample option.
      'contents' => '300' // Set the value for the downsample option (in this case, '300').
    ],
    [
      'name' => 'tagged_pdf', // Specify the field name for the tagged PDF option.
      'contents' => 'off' // Set the value for the tagged PDF option (in this case, 'off').
    ],
    [
      'name' => 'output', // Specify the field name for the output option.
      'contents' => 'pdfrest_pdf' // Set the value for the output option (in this case, 'pdfrest_pdf').
    ]
  ]
];

$request = new Request('POST', 'https://api.pdfrest.com/pdf', $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 processed PDF content.

Source: GitHub

Breaking Down the Code

The provided code uses the Guzzle HTTP client to make an API call to the pdfRest Convert to PDF endpoint. Let's break down the code to understand its functionality:

require 'vendor/autoload.php';

This line includes the Composer autoload file, which loads the necessary classes for the Guzzle HTTP client.

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

These lines import the required namespaces for the Guzzle HTTP client, the PSR-7 Request class, and the Utils class for handling streams.

$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 contains the API key required 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' => 'compression',
      'contents' => 'lossless'
    ],
    [
      'name' => 'downsample',
      'contents' => '300'
    ],
    [
      'name' => 'tagged_pdf',
      'contents' => 'off'
    ],
    [
      'name' => 'output',
      'contents' => 'pdfrest_pdf'
    ]
  ]
];

The options array defines the multipart form data for the request. It includes the file to be converted, compression settings, downsampling options, and output preferences. Each 'name' field corresponds to a parameter recognized by the API, and 'contents' specifies the value for that parameter.

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

This line creates a new HTTP POST request to the pdfRest API endpoint, including the specified headers for authentication.

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

The asynchronous request is sent to the API, and the script waits for the response. This allows the application to perform other tasks while waiting for the API to process the request.

echo $res->getBody();

Finally, the response body, which contains the converted PDF content, is outputted to the screen.

Beyond the Tutorial

In this tutorial, you learned how to use PHP to convert HTML to PDF with the pdfRest Convert to PDF API tool, allowing you to convert files to PDF format programmatically. This powerful tool can be integrated into various applications to automate and streamline document conversion processes.

To explore more functionalities and experiment with different API tools, visit the API Lab. For detailed information on all available API options, refer to the API Reference Guide. This example demonstrates a multipart API call; for examples using JSON payloads, visit the GitHub repository.

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