How to Flatten Annotations in PDF Files with PHP
The pdfRest Flatten Annotations API Tool allows developers to programmatically merge annotations into a PDF's content. This is useful when you need to finalize a document so that comments, highlights, and other markups can no longer be modified or removed. This process is essential for creating a final, static version of a document, such as a legal contract or a finished report.
This tutorial will guide you through the process of using our PHP client to make an API call to the Flatten Annotations endpoint. This is particularly useful for PHP developers who need to automate document finalization within their applications.
Flatten Annotations 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' => 'example.pdf', 'headers' => [ 'Content-Type' => 'application/pdf' ] ], [ 'name' => 'output', 'contents' => 'pdfrest_flattened_pdf' ] ] ]; $request = new Request('POST', 'https://api.pdfrest.com/flattened-annotations-pdf', $headers); $res = $client->sendAsync($request, $options)->wait(); echo $res->getBody();
Source: GitHub - datalogics/pdf-rest-api-samples
Breaking Down the Code
This script uses the Guzzle HTTP client to communicate with the pdfRest API. The key components of the multipart request are designed to securely send your file and specify the flattening operation.
- The
'Api-Key'
in the headers is your unique key for authentication. - The
'name' => 'file'
part of the request carries the PDF document you want to process. The'contents'
key usesUtils::tryFopen
to open the file securely from the specified path. - The
'name' => 'output'
parameter, set to'pdfrest_flattened_pdf'
, instructs the API to perform the specific action of merging all annotations into the document content. - The request is sent to the
https://api.pdfrest.com/flattened-annotations-pdf
endpoint, which is dedicated to this particular tool.
Frequently Asked Questions
Below are some common questions about flattening PDF annotations.
- What types of annotations are supported?
- The API is designed to flatten all standard PDF annotation types, including text highlights, underlines, sticky notes, comments, shapes, and stamps. It merges them into the document's content layer so they become permanent.
- Does flattening a PDF affect hyperlinks?
- The flattening process does not affect hyperlinks within the document's content. These links will remain active and clickable after the process is complete.
- Can I unflatten a PDF after processing it?
- No, the flattening process is permanent. Once annotations are merged into the document content, they cannot be unflattened or edited as separate objects. It is always recommended to work with a copy of your original file.
Beyond the Tutorial
By following these steps, you have successfully implemented the pdfRest Flatten Annotations API. You can now integrate this functionality into your applications to automate the process of finalizing and securing PDF documents. To explore all of the available API tools and features, visit the API Lab and our comprehensive API documentation.