How to Add Images to PDF Files with PHP
Why Add Images to PDF with PHP?
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.
Add Images to PDF with PHP Code Example
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.
Breaking Down the Code
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:
- Headers: The 'Api-Key' header is used for authentication with the pdfRest API.
- Multipart: The 'multipart' array contains the payload for the request. It includes the main PDF file, the image file to be added, and parameters such as X and Y coordinates, page number, and output options.
- File: The 'file' part contains the main PDF file to be processed. The 'contents' key uses the
Utils::tryFopen
function to read the file, and 'filename' specifies the file path. - Image File: Similarly, the 'image_file' part includes the image to be added to the PDF. It also specifies the file path and content type.
- X and Y Coordinates: These define the position where the image will be placed on the PDF page.
- Page: This specifies the page number where the image should be added.
- Output: This option determines the naming convention for the processed PDF file.
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.
Beyond the Tutorial
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.