The pdfRest Convert to PDF API Tool is a powerful resource for developers looking to automate the conversion of various file formats into PDFs. This tool is particularly useful when integrating PDF generation capabilities into web applications or services. By using this API, developers can programmatically convert files to PDF format without the need for manual intervention or desktop PDF software. This tutorial will demonstrate how to send an API call to Convert to PDF with PHP, leveraging the capabilities of the pdfRest API.
Imagine you are running a document management system where users upload PowerPoint presentations. You want to ensure consistency and easy sharing across your platform. Using the Convert to PDF API, you can automatically convert these presentation files into PDFs as soon as they are uploaded. This creates a standardized format for all presentations, making them easy to share, archive, and print for users.
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.pptx', 'r'), 'filename' => '/path/to/file.pptx', '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();
Reference to the code: GitHub Repository
The code above demonstrates how to make a multipart/form-data POST request to the pdfRest API to convert a file to PDF. Let's break down the key components:
require 'vendor/autoload.php'; use GuzzleHttp\Client; use GuzzleHttp\Psr7\Request; use GuzzleHttp\Psr7\Utils;
This section includes the necessary classes from the Guzzle HTTP client library, which is used to handle HTTP requests and responses.
$headers = [ 'Api-Key' => 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' ];
The headers array contains the API key required for authentication with the pdfRest API.
$options = [ 'multipart' => [ // File and other options here... ] ];
The options array is where you define the multipart payload for the request. It includes the file to be converted and additional parameters such as compression, downsample, and output options.
$request = new Request('POST', 'https://api.pdfrest.com/pdf', $headers);
This line creates a new HTTP POST request with the specified API endpoint and headers.
$res = $client->sendAsync($request, $options)->wait(); echo $res->getBody();
Finally, 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.
By following the steps above, you have learned how to use PHP to call the pdfRest Convert to PDF API, sending a file and additional options for conversion. This capability can be integrated into your applications to automate the process of converting files to PDF format, streamlining workflows and improving productivity.
To further explore the capabilities of the pdfRest API, you can demo all of the pdfRest API Tools in the API Lab. For comprehensive documentation and more details on the API's functionality, 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 Repository.
Create your FREE API Key to start processing PDFs in seconds, only possible with pdfRest.