The pdfRest Flatten Annotations API Tool is designed to permanently merge annotations into the PDF content layer. This is useful when you want to ensure that comments, form fields, or markup added to a PDF are preserved when the document is printed or viewed on systems where annotations may not be displayed by default. For instance, if you've collected feedback on a document and want to create a version with all comments embedded for archival purposes, flattening annotations would be the way to go.
This tutorial will show you how to send an API call to Flatten Annotations using Python.
from requests_toolbelt import MultipartEncoder import requests import json flattened_annotations_pdf_endpoint_url = 'https://api.pdfrest.com/flattened-annotations-pdf' mp_encoder_flattenedPDF = MultipartEncoder( fields={ 'file': ('file_name.pdf', open('/path/to/file', 'rb'), 'application/pdf'), 'output' : 'example_out' } ) headers = { 'Accept': 'application/json', 'Content-Type': mp_encoder_flattenedPDF.content_type, 'Api-Key': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' # place your api key here } print("Sending POST request to flattened-annotations-pdf endpoint...") response = requests.post(flattened_annotations_pdf_endpoint_url, data=mp_encoder_flattenedPDF, headers=headers) print("Response status code: " + str(response.status_code)) if response.ok: response_json = response.json() print(json.dumps(response_json, indent = 2)) else: print(response.text)
Reference: pdf-rest-api-samples on GitHub
This code snippet demonstrates how to use the pdfRest Flatten Annotations API with Python:
from requests_toolbelt import MultipartEncoder import requests import json
These lines import the necessary libraries. MultipartEncoder
from requests_toolbelt
is used for encoding multipart form data. The requests
library is used to send HTTP requests, and json
is used for working with JSON data.
mp_encoder_flattenedPDF = MultipartEncoder( fields={ 'file': ('file_name.pdf', open('/path/to/file', 'rb'), 'application/pdf'), 'output' : 'example_out' } )
This section creates a MultipartEncoder
object with the PDF file to flatten and the desired output name. Replace '/path/to/file'
with the actual file path and 'file_name.pdf'
with the actual file name.
headers = { 'Accept': 'application/json', 'Content-Type': mp_encoder_flattenedPDF.content_type, 'Api-Key': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' # place your api key here }
Here, the headers for the HTTP request are defined. The 'Api-Key'
should be replaced with your actual API key from pdfRest.
response = requests.post(flattened_annotations_pdf_endpoint_url, data=mp_encoder_flattenedPDF, headers=headers)
This line sends the POST request to the pdfRest API endpoint with the encoded PDF data and headers.
By following the steps above, you've learned how to call the pdfRest Flatten Annotations API using Python. This can be a powerful tool in your document processing workflow, ensuring annotations are preserved in the final document version. You're encouraged to demo all of the pdfRest API Tools in the API Lab at https://pdfrest.com/apilab/ and refer to the API Reference documentation at https://pdfrest.com/documentation/ for more information.
Note: This is an example of a multipart API call. Code samples using JSON payloads can be found at https://github.com/datalogics/pdf-rest-api-samples.
Create your FREE API Key to start processing PDFs in seconds, only possible with pdfRest.