How to Flatten PDF Forms with PHP
Why Flatten PDF Forms with PHP?
The pdfRest Flatten Forms API Tool is a powerful resource for developers looking to programmatically flatten form fields within a PDF. Flattening forms means converting the editable form fields into static text, which is part of the PDF content and can no longer be edited. This tutorial will guide you through the process of sending an API call to the Flatten Forms endpoint using PHP, which is a common server-side scripting language used in web development.
In the real world, flattening forms can be crucial for preserving the content of filled-out forms, such as applications, contracts, or any other documents that require user input. Once a form is flattened, the information entered by the user is secured, and the document can be distributed without the risk of further modifications. This is particularly useful for finalizing documents before archiving or sharing them with parties that should not alter the content.
Flatten PDF Forms with PHP Code Example
'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' ]; $options = [ 'multipart' => [ [ 'name' => 'file', 'contents' => Utils::tryFopen('/path/to/file', 'r'), 'filename' => '/path/to/file', 'headers' => [ 'Content-Type' => '' ] ], [ 'name' => 'output', 'contents' => 'pdfrest_flattened_pdf' ] ]]; $request = new Request('POST', 'https://api.pdfrest.com/flattened-forms-pdf', $headers); $res = $client->sendAsync($request, $options)->wait(); echo $res->getBody();
Source of the provided code: GitHub - datalogics/pdf-rest-api-samples
Breaking Down the Code
The code begins by loading the required libraries for making HTTP requests. The GuzzleHttp\Client
class is used to send requests to the server, and the GuzzleHttp\Psr7\Request
and GuzzleHttp\Psr7\Utils
classes are used to create and manage the request and file streams, respectively.
require 'vendor/autoload.php'; use GuzzleHttp\Client; use GuzzleHttp\Psr7\Request; use GuzzleHttp\Psr7\Utils;
Next, an API key is set in the headers to authenticate the request:
$headers = [ 'Api-Key' => 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' ];
The options array is where the multipart form data is defined. This includes the file to be flattened and the desired output name:
'options' => [ 'multipart' => [ [ 'name' => 'file', 'contents' => Utils::tryFopen('/path/to/file', 'r'), 'filename' => '/path/to/file', 'headers' => [ 'Content-Type' => '' ] ], [ 'name' => 'output', 'contents' => 'pdfrest_flattened_pdf' ] ]];
The request is then created and sent asynchronously, with the response being outputted to the user:
$request = new Request('POST', 'https://api.pdfrest.com/flattened-forms-pdf', $headers); $res = $client->sendAsync($request, $options)->wait(); echo $res->getBody();
Beyond the Tutorial
By following this tutorial, you have learned how to send a multipart API call to the pdfRest Flatten Forms API using PHP. This enables you to flatten form fields in a PDF document, securing the data entered by users and preparing the document for distribution or archiving. You're encouraged to explore and demo all of the pdfRest API Tools in the API Lab and refer to the API Reference Guide for further information.
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.