How to Add Vector Lines and Rectangles to PDF Files with PHP
Why Add Vector Lines and Rectangles to PDF Files with PHP?
The pdfRest Add to PDF API Tool is a powerful resource for developers who need to programmatically add shapes to PDF documents. This tutorial will guide you through the process of making an API call to the Add to PDF endpoint using PHP, allowing you to enhance your PDF files with custom shapes and annotations.
Imagine you're a developer working on a document management system that requires adding review panels or annotations to PDFs for collaborative editing. The Add to PDF API can streamline this process by enabling you to programmatically insert shapes, such as lines and rectangles, into your PDFs, thus enhancing the document's utility and readability.
Add Vector Lines and Rectangles to PDF Files with PHP Code Example
require 'vendor/autoload.php';
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Utils;
// By default, we use the US-based API service. This is the primary endpoint for global use.
$apiUrl = 'https://api.pdfrest.com';
/* For GDPR compliance and enhanced performance for European users, you can switch to the EU-based service by uncommenting the URL below.
* For more information visit https://pdfrest.com/pricing#how-do-eu-gdpr-api-calls-work
*/
// $apiUrl = 'https://eu-api.pdfrest.com';
// Add a lightly shaded review panel and a divider line to page one.
// Coordinates are measured from the lower-left corner in PDF units (72 units = 1 inch).
$shapeObjects = [
[
'type' =--> 'rectangle', 'page' => 1, 'x' => 54, 'y' => 540, 'width' => 504, 'height' => 108,
'fill_color_rgb' => '245,247,250', 'stroke_color_rgb' => '26,72,112', 'stroke_width' => 1,
'tag_is_artifact' => true,
],
[
'type' => 'line', 'page' => 1, 'x1' => 72, 'y1' => 576, 'x2' => 540, 'y2' => 576,
'stroke_color_rgb' => '26,72,112', 'stroke_width' => 1.5,
'tag_actual_text' => 'Review section divider', 'tag_structure_type' => 'Figure',
],
];
$request = new Request('POST', $apiUrl . '/pdf-with-added-shapes', [
'Api-Key' => 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx', // Replace with your API key.
]);
$response = (new Client(['http_errors' => false]))->send($request, [
'multipart' => [
['name' => 'file', 'contents' => Utils::tryFopen('/path/to/input.pdf', 'r'), 'filename' => 'input.pdf'],
['name' => 'shape_objects', 'contents' => json_encode($shapeObjects)],
['name' => 'tag_enabled', 'contents' => 'true'],
['name' => 'output', 'contents' => 'review-panel'],
],
]);
echo $response->getBody() . PHP_EOL;
// To download the returned file, use the outputId with the get-resource-id endpoint sample.
Source: GitHub
Breaking Down the Code
The code begins by importing necessary libraries using require 'vendor/autoload.php'; and setting up the Guzzle HTTP client for making HTTP requests.
The API endpoint URL is set with $apiUrl = 'https://api.pdfrest.com';. This is the default US-based endpoint, but an EU-based endpoint is available for GDPR compliance.
The $shapeObjects array defines the shapes to be added to the PDF. Each shape is specified with attributes like type, page, x, y, width, height, and color specifications. These attributes define the appearance and placement of the shapes on the PDF.
A Request object is created with the HTTP method 'POST' and the endpoint /pdf-with-added-shapes. The request headers include an Api-Key for authentication.
The send method of the Client object sends the request with a multipart payload. This payload includes the PDF file, shape objects, and additional parameters like tag_enabled and output.
The response from the API call is captured and printed using echo $response->getBody() . PHP_EOL;.
Beyond the Tutorial
In this tutorial, you learned how to use PHP to call the pdfRest Add to PDF API and add shapes to a PDF document. This functionality can be leveraged to enhance PDFs with annotations, review panels, or other graphical elements.
To explore more features of the pdfRest API, visit the API Lab. For detailed documentation, refer to the API Reference Guide.
Note: This example demonstrates a multipart API call. For code samples using JSON payloads, visit GitHub.