The pdfRest Convert to PDF API Tool provides a straightforward way to convert various file formats into PDFs using a simple API call. This tutorial will demonstrate how to send an API call to the pdfRest Convert to PDF endpoint using PHP, a popular server-side scripting language. By integrating this functionality into PHP applications, developers can automate the conversion process, making it efficient and scalable.
You might need to convert documents to PDF to ensure compatibility across different platforms and devices. For instance, businesses often need to convert invoices, reports, or contracts to PDF format before sharing them with clients or storing them for record-keeping. The Convert to PDF API allows for these conversions to be done programmatically, saving time and reducing the risk of human error.
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.docx', 'r'), 'filename' => '/path/to/file.docx', 'headers' => [ 'Content-Type' => '' ] ], [ 'name' => 'compression', 'contents' => 'lossless' ], [ 'name' => 'downsample', 'contents' => '300' ], [ 'name' => 'tagged_pdf', 'contents' => 'off' ], [ 'name' => 'output', 'contents' => 'pdfrest_pdf' ] ] ]; $request = new Request('POST', 'https://api.pdfrest.com/pdf', $headers); $res = $client->sendAsync($request, $options)->wait(); echo $res->getBody();
Source code reference: GitHub - datalogics/pdf-rest-api-samples
This PHP script uses the Guzzle HTTP client library to make an HTTP POST request to the pdfRest Convert to PDF API endpoint. Here's a breakdown of the key components:
$headers = [ 'Api-Key' => 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' ];
This array contains the headers for the HTTP request, including the 'Api-Key' which is used for authentication with the pdfRest API.
$options = [ 'multipart' => [ // ... multipart form data ... ] ];
The $options
array is used to define the multipart form data that will be sent with the request. Each element of the 'multipart' array represents a different part of the form data:
'file'
: The file to be converted to PDF. The 'contents' key should point to the file stream, and the 'filename' key should specify the file path.'compression'
: The compression setting for the PDF.'downsample'
: The downsample resolution for images within the PDF.'tagged_pdf'
: Whether to create a tagged PDF, which is important for accessibility.'output'
: The output file name prefix for the converted PDF.$request = new Request('POST', 'https://api.pdfrest.com/pdf', $headers);
A new HTTP POST request is created with the API endpoint URL and the headers.
$res = $client->sendAsync($request, $options)->wait();
The request is sent asynchronously, and the script waits for the response. The response body, which should contain the processed PDF content, is then printed out.
This tutorial has shown how to use PHP to call the pdfRest Convert to PDF API. By sending a multipart request with the necessary parameters, you can convert files to PDF programmatically. To explore and test all of the pdfRest API Tools, visit 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 - datalogics/pdf-rest-api-samples (JSON Payload).
Create your FREE API Key to start processing PDFs in seconds, only possible with pdfRest.