How to Rasterize PDF Files with PHP
Why Rasterize PDF Files with PHP?
The pdfRest Rasterize PDF API Tool provides a powerful way to convert PDF documents into rasterized images using a simple API call. This tutorial will guide you through the process of sending an API call to the Rasterize PDF endpoint using PHP, allowing you to integrate this functionality into your own applications seamlessly.
You might need to rasterize a PDF to ensure that it displays consistently across different platforms and devices, as rasterized images are less prone to rendering issues. For instance, a company might use this to prepare PDF documents for a web application, ensuring that users see the same content regardless of their browser or device capabilities.
Rasterize 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' => 'filename.pdf', // 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' => 'output', // Specify the field name for the output option. 'contents' => 'pdfrest_rasterize' // Set the value for the output option (in this case, 'pdfrest_rasterize'). ] ] ]; $request = new Request('POST', 'https://api.pdfrest.com/rasterized-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 rasterized PDF.
Source: GitHub Repository
Breaking Down the Code
The code begins by requiring the 'vendor/autoload.php' file, which is essential for loading the Guzzle HTTP client, a popular PHP HTTP client used for making requests.
require 'vendor/autoload.php';
Next, the code imports necessary classes from the Guzzle library. These include the 'Client', 'Request', and 'Utils' classes, which are used to create an HTTP client, form requests, and handle file streams, respectively.
use GuzzleHttp\Client; use GuzzleHttp\Psr7\Request; use GuzzleHttp\Psr7\Utils;
An instance of the Guzzle HTTP client is created, which will be used to send the API request.
$client = new Client();
The headers array contains the 'Api-Key', which is necessary for authenticating the request. Replace 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' with your actual API key.
$headers = [ 'Api-Key' => 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' ];
The 'options' array is configured for a multipart request. The 'file' part specifies the PDF file to be rasterized, including its path and filename. The 'output' part specifies the desired output format, 'pdfrest_rasterize'.
$options = [ 'multipart' => [ [ 'name' => 'file', 'contents' => Utils::tryFopen('/path/to/file', 'r'), 'filename' => 'filename.pdf', 'headers' => [ 'Content-Type' => '' ] ], [ 'name' => 'output', 'contents' => 'pdfrest_rasterize' ] ] ];
A new HTTP POST request is created with the specified endpoint and headers.
$request = new Request('POST', 'https://api.pdfrest.com/rasterized-pdf', $headers);
The request is sent asynchronously, and the response is awaited. The response body, which contains the rasterized PDF, is then printed to the console.
$res = $client->sendAsync($request, $options)->wait(); echo $res->getBody();
Beyond the Tutorial
In this tutorial, you learned how to use PHP to make an API call to the pdfRest Rasterize PDF endpoint, allowing you to convert all pages of a PDF into rasterized images. This can be particularly useful for ensuring consistent display across various platforms.
To explore more about pdfRest API Tools, visit the API Lab. For detailed documentation, refer to the API Reference Guide. This example demonstrates a multipart API call; for JSON payload examples, visit the GitHub Repository.