How to Watermark PDF with Python

Learn how to apply image watermarks to a PDF using Python to call the Watermark PDF API Tool from pdfRest.
Share this page

Why Watermark PDF with Python?

The pdfRest Watermark PDF API Tool is a powerful resource that allows users to add watermarks to PDF documents programmatically. This tutorial will guide you through the process of making an API call to the Watermark PDF endpoint using Python.

This functionality is particularly useful for businesses or individuals who need to protect their documents or brand them with a company logo or text before distribution.

Watermark PDF with Python Code Example

from requests_toolbelt import MultipartEncoder
import requests
import json

watermarked_pdf_endpoint_url = 'https://api.pdfrest.com/watermarked-pdf'

mp_encoder_watermarkedPDF = MultipartEncoder(
    fields={
        'file': ('file_name.pdf', open('/path/to/file', 'rb'), 'application/pdf'),
        'watermark_text': 'watermark',
        'output' : 'example_out'
    }
)

headers = {
    'Accept': 'application/json',
    'Content-Type': mp_encoder_watermarkedPDF.content_type,
    'Api-Key': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' # place your api key here
}

print("Sending POST request to watermarked-pdf endpoint...")
response = requests.post(watermarked_pdf_endpoint_url, data=mp_encoder_watermarkedPDF, 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)

This code is sourced from the pdf-rest-api-samples repository on GitHub.

Breaking Down the Code

The provided Python code performs the following steps to call the pdfRest Watermark PDF API:

from requests_toolbelt import MultipartEncoder
import requests
import json

This section imports the necessary libraries. requests_toolbelt is used for creating a multipart encoder object, requests for making HTTP requests, and json for handling JSON data.

mp_encoder_watermarkedPDF = MultipartEncoder(
    fields={
        'file': ('file_name.pdf', open('/path/to/file', 'rb'), 'application/pdf'),
        'watermark_text': 'watermark',
        'output' : 'example_out'
    }
)

Here, a MultipartEncoder object is created with the PDF file to be watermarked, the text of the watermark, and the desired output filename.

headers = {
    'Accept': 'application/json',
    'Content-Type': mp_encoder_watermarkedPDF.content_type,
    'Api-Key': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
}

The headers for the API request are set, including the API key which you need to replace with your own.

response = requests.post(watermarked_pdf_endpoint_url, data=mp_encoder_watermarkedPDF, headers=headers)

An HTTP POST request is sent to the pdfRest API endpoint with the multipart data and headers.

if response.ok:
    response_json = response.json()
    print(json.dumps(response_json, indent = 2))
else:
    print(response.text)

The response is checked for success (status code 200). If successful, the response is printed in a JSON formatted string; otherwise, the error text is printed.

Beyond the Tutorial

By following the steps outlined in this tutorial, you have learned how to use Python to send an API call to the pdfRest Watermark PDF endpoint. This allows you to add watermarks to your PDF documents with ease. You are encouraged to demo all of the pdfRest API Tools in the API Lab and refer to the API Reference documentation for more details.

Note: This is an example of a multipart API call. Code samples using JSON payloads can be found at GitHub.

Generate a self-service API Key now!

Create your FREE API Key to start processing PDFs in seconds, only possible with pdfRest.

Compare Plans
Contact Us