The pdfRest Convert to PDF API Tool allows developers to easily convert Excel spreadsheets into PDF format using a simple API call. This tool is particularly useful when you need to standardize documents in a universally accessible format, ensuring compatibility across different systems and devices. This tutorial will demonstrate how to send an API call to Convert to PDF using PHP, which is a widely-used server-side scripting language that is especially suited for web development and can be embedded into HTML.
You might need to convert Excel files to PDF for archival, sharing, or printing purposes. For instance, a business might need to convert invoices, reports, or contracts to PDF to ensure that they can be easily viewed and remain unaltered when sent to clients or partners. By using the Convert to PDF API, the conversion process can be automated and integrated into existing PHP applications, streamlining the workflow and saving time.
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.xlsx', 'r'), 'filename' => '/path/to/file.xlsx', '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();
This code is sourced from the pdf-rest-api-samples repository on GitHub.
The code begins by loading the required libraries and setting up the Guzzle HTTP client, which is a PHP HTTP client that makes it easy to send HTTP requests and integrate with web services.
require 'vendor/autoload.php'; use GuzzleHttp\Client; use GuzzleHttp\Psr7\Request; use GuzzleHttp\Psr7\Utils; $client = new Client();
Next, the API key is set in the headers for authentication purposes:
$headers = [ 'Api-Key' => 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' ];
The options array is constructed with the 'multipart' key, which contains the details of the file to be converted and the conversion options:
$options = [ // ...multipart form data... ];
Each element of the 'multipart' array represents a different part of the form data being sent. The 'file' part includes the file contents and the Content-Type header, while the other parts include options such as 'compression', 'downsample', 'tagged_pdf', and 'output', which correspond to the conversion settings.
The request is then created and sent asynchronously:
$request = new Request('POST', 'https://api.pdfrest.com/pdf', $headers); $res = $client->sendAsync($request, $options)->wait();
Finally, the response body is echoed, which contains the processed PDF content.
In this tutorial, we've successfully made an API call to the pdfRest Convert to PDF API using PHP. The code provided sends a file along with conversion options to pdfRest, and the API returns the converted PDF. This process can be integrated into larger PHP applications to automate the conversion of documents to PDF format.
To explore and demo all of the pdfRest API Tools, visit the API Lab. For more detailed information on the API and its capabilities, 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 the pdf-rest-api-samples repository on GitHub.
Create your FREE API Key to start processing PDFs in seconds, only possible with pdfRest.