The pdfRest Add to PDF API Tool is a powerful solution for developers looking to programmatically add text to PDF documents. This tutorial demonstrates how to send an API call to the Add to PDF endpoint using PHP. By leveraging this tool, users can automate the process of editing PDFs, making it an efficient option for applications that require dynamic PDF content generation.
In a real-world scenario, a business might use the Add to PDF API to automatically generate invoices or reports that include custom text, such as customer names, dates, or transaction details. This can streamline workflows by eliminating the need for manual PDF editing, ensuring that documents are consistently formatted and delivered in a timely manner.
require 'vendor/autoload.php'; // Require the autoload file to load Guzzle HTTP client. use GuzzleHttp\Client; // Import the Guzzle HTTP client namespace. use GuzzleHttp\Psr7\Request; // Import the PSR-7 Request class. use GuzzleHttp\Psr7\Utils; // Import the PSR-7 Utils class for working with streams. $client = new Client(); // Create a new instance of the Guzzle HTTP client. $headers = [ 'Api-Key' =--> 'xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' // Set the API key in the headers for authentication. ]; $options = [ 'multipart' => [ [ 'name' => 'file', // Specify the field name for the file. 'contents' => Utils::tryFopen('/path/to/file', 'r'), // Open the file specified by the '/path/to/file' for reading. 'filename' => '/path/to/file', // Set the filename for the file to be processed, in this case, '/path/to/file'. 'headers' => [ 'Content-Type' => '' // Set the Content-Type header for the file. ] ], [ 'name' => 'text_objects', // Specify the field name for the text options. 'contents' => '[{"font":"Times New Roman","max_width":"175","opacity":"1","page":"1","rotation":"0","text":"sample text in PDF","text_color_rgb":"0,0,0","text_size":"30","x":"72","y":"144"}]' // Set the value for the text_objects option. This is a JSON-formatted string consisting of an array with sets of text options. ], [ 'name' => 'output', // Specify the field name for the output option. 'contents' => 'pdfrest_pdf_with_added_text' // Set the value for the output option (in this case, 'pdfrest_pdf_with_added_text'). ] ] ]; $request = new Request('POST', 'https://api.pdfrest.com/pdf-with-added-text', $headers); // Create a new HTTP POST request with the API endpoint and headers. $res = $client->sendAsync($request, $options)->wait(); // Send the asynchronous request and wait for the response. echo $res->getBody(); // Output the response body, which contains the PDF with new text.
Source: GitHub Repository
The code begins by requiring the autoload file to load the Guzzle HTTP client, which is necessary for making HTTP requests:
require 'vendor/autoload.php';
Next, we import the necessary classes from the Guzzle library:
use GuzzleHttp\Client; use GuzzleHttp\Psr7\Request; use GuzzleHttp\Psr7\Utils;
We then create a new instance of the Guzzle HTTP client:
$client = new Client();
The headers for the request are set, including the API key for authentication:
$headers = [ 'Api-Key' => 'xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' ];
The options array is configured with multipart data, which includes the file to be processed, the text objects to add, and the output format:
$options = [ 'multipart' => [ [ 'name' => 'file', 'contents' => Utils::tryFopen('/path/to/file', 'r'), 'filename' => '/path/to/file', 'headers' => [ 'Content-Type' => '' ] ], [ 'name' => 'text_objects', 'contents' => '[{"font":"Times New Roman","max_width":"175","opacity":"1","page":"1","rotation":"0","text":"sample text in PDF","text_color_rgb":"0,0,0","text_size":"30","x":"72","y":"144"}]' ], [ 'name' => 'output', 'contents' => 'pdfrest_pdf_with_added_text' ] ] ];
A new HTTP POST request is created with the specified endpoint and headers:
$request = new Request('POST', 'https://api.pdfrest.com/pdf-with-added-text', $headers);
The request is sent asynchronously, and the response is awaited and outputted:
$res = $client->sendAsync($request, $options)->wait(); echo $res->getBody();
In this tutorial, we demonstrated how to use PHP to make an API call to the pdfRest Add to PDF endpoint, allowing you to add text to a PDF document programmatically. This can be a valuable tool for automating document processing tasks.
To further explore the capabilities of the pdfRest API, we encourage you to try out all the available tools in the API Lab. For more detailed information, refer to the API Reference Guide.
Note: This example demonstrates a multipart API call. Code samples using JSON payloads can be found at this GitHub repository.
Create your FREE API Key to start processing PDFs in seconds, only possible with pdfRest.