The pdfRest Add to PDF API Tool provides a simple and powerful way to manipulate PDF documents programmatically. This tutorial will demonstrate how to send an API call to the Add to PDF endpoint using PHP, which allows developers to seamlessly integrate PDF processing capabilities into their applications. The Add to PDF API Tool is particularly useful for automating the task of adding images or other content to PDF files without the need for manual intervention or complex software.
A real-world example of why a user might use the Add to PDF API could be an online printing service that needs to add branding or watermarks to PDF documents before sending them to print. Another scenario could be an invoicing system where company logos or product images need to be dynamically added to PDF invoices based on customer orders. Utilizing the Add to PDF API, these tasks can be automated, saving time and reducing errors.
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', 'r'), 'filename' => '/path/to/file', 'headers' => [ 'Content-Type' => '' ] ], [ 'name' => 'image_file', 'contents' => Utils::tryFopen('/path/to/file', 'r'), 'filename' => '/path/to/file', 'headers' => [ 'Content-Type' => ' ' ] ], [ 'name' => 'x', 'contents' => '72' ], [ 'name' => 'y', 'contents' => '72' ], [ 'name' => 'page', 'contents' => '1' ], [ 'name' => 'output', 'contents' => 'pdfrest_pdf_with_added_image' ] ] ]; $request = new Request('POST', 'https://api.pdfrest.com/pdf-with-added-image', $headers); $res = $client->sendAsync($request, $options)->wait(); echo $res->getBody();
This code is sourced from the pdf-rest-api-samples repository on GitHub.
The provided PHP code uses the Guzzle HTTP client to make a POST request to the https://api.pdfrest.com/pdf-with-added-image
endpoint. The request is constructed with several key components:
Utils::tryFopen
function to read the file, and 'filename' specifies the file path.The Request
object is created with the 'POST' method, the API endpoint, and the headers. The $client->sendAsync
method sends the request asynchronously and waits for the response, which is then outputted to the screen.
In this tutorial, we've accomplished sending a multipart API call to the Add to PDF endpoint of the pdfRest API using PHP. The code example demonstrates how to add an image to a PDF document programmatically. Users can now explore other capabilities of the pdfRest API and integrate them into their applications for a wide range of PDF processing needs.
To learn more and experiment with the full range of pdfRest API Tools, visit the API Lab. For detailed documentation and additional resources, 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.