The pdfRest Merge PDFs API Tool allows users to combine multiple PDF files into a single PDF document programmatically. This tutorial will guide you through the process of making an API call to the Merge PDFs endpoint using PHP.
This can be particularly useful in scenarios where you need to consolidate reports, invoices, or any documents that are better served as a single file for easier distribution or archiving.
'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' ]; $options = [ 'multipart' => [ // ... (multipart data here) ] ]; $request = new Request('POST', 'https://api.pdfrest.com/merged-pdf', $headers); $res = $client->sendAsync($request, $options)->wait(); echo $res->getBody(); ?>
Source of the provided code: GitHub - datalogics/pdf-rest-api-samples
The code begins by loading the required libraries and setting up the Guzzle HTTP client:
require 'vendor/autoload.php'; use GuzzleHttp\Client; $client = new Client();
Next, we define the headers with the API key for authentication:
$headers = [ 'Api-Key' => 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' ];
The options array is where the magic happens. It contains the 'multipart' data which represents each file and its options:
'options' => [ 'multipart' => [ // ... (multipart data here) ] ];
Each element in the 'multipart' array corresponds to a field in the form data that the API expects. The 'name' key specifies the field name, 'contents' is the value, and 'filename' is the name of the file when uploading a file.
The 'pages[]' and 'type[]' fields are used to specify which pages to include from each file and the type of the input (file or ID).
The 'output' field defines the name of the resulting merged PDF:
[ 'name' => 'output', 'contents' => 'pdfrest_merged_pdf' ]
Finally, we send the request and output the response:
$request = new Request('POST', 'https://api.pdfrest.com/merged-pdf', $headers); $res = $client->sendAsync($request, $options)->wait(); echo $res->getBody();
In this tutorial, we've successfully walked through the process of making an API call to the pdfRest Merge PDFs endpoint using PHP. By sending the appropriate multipart form data, we can merge multiple PDF files into a single document.
To explore and demo all of the pdfRest API Tools, visit the API Lab. For more detailed information, refer to the API Reference documentation.
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.
Create your FREE API Key to start processing PDFs in seconds, only possible with pdfRest.