
Automate Factur-X and ZUGFeRD e-Invoices in Python with PDF.co
15 MIN READ
This tutorial shows how to place a logo or another image over PDF pages using Python and the PDF.co Web API.
The watermark’s position and size can be customized, and a transparent PNG can be used for a less intrusive result.
You will need:
Install the Requests library: python -m pip install requests
Store your PDF.co API key in an environment variable named PDFCO_API_KEY.
Open the source document in the PDF.co PDF Inspector.
Use the Inspector to determine:
PDF.co measures coordinates from the top-left corner. Page indexes begin at 0, and 0- represents every page.
Create a file named app.py and add the following code:
import os
from pathlib import Path
import requests
API_URL = "https://api.pdf.co/v1/pdf/edit/add"
API_KEY = os.environ["PDFCO_API_KEY"]
SOURCE_PDF_URL = "https://example.com/source.pdf"
WATERMARK_IMAGE_URL = "https://example.com/logo.png"
OUTPUT_FILE = Path("watermarked.pdf")
payload = {
"url": SOURCE_PDF_URL,
"name": OUTPUT_FILE.name,
"async": False,
"images": [
{
"url": WATERMARK_IMAGE_URL,
"x": 400,
"y": 30,
"width": 140,
"height": 60,
"pages": "0-",
"keepAspectRatio": True
}
]
}
response = requests.post(
API_URL,
headers={
"x-api-key": API_KEY,
"Content-Type": "application/json"
},
json=payload,
timeout=60
)
response.raise_for_status()
result = response.json()
if result.get("error"):
raise RuntimeError(result.get("message", "PDF.co could not process the PDF."))
output_response = requests.get(result["url"], timeout=60)
output_response.raise_for_status()
OUTPUT_FILE.write_bytes(output_response.content)
print(f"Saved watermarked PDF to: {OUTPUT_FILE.resolve()}")Replace SOURCE_PDF_URL and WATERMARK_IMAGE_URL with direct links to your files.
The images object controls the watermark:
url — direct URL, PDF.co file token, or data URI for the imagex and y — image positionwidth and height — image dimensionspages — pages receiving the watermarkkeepAspectRatio — preserves the image’s proportionsFor example, use "pages": "0" to watermark only the first page or "pages": "0,2-4" to process selected pages.
See the PDF Add API documentation for all available image settings.
Run the script:
python app.pyPDF.co will add the watermark and return a temporary output URL. The script downloads that file and saves it locally as watermarked.pdf.
Open the generated PDF and verify the watermark’s position, size, and page selection. Adjust the coordinates with PDF Inspector if necessary.
In this demonstration, you learned how to add a logo watermark in a PDF using Python and PDF.co Web API. You learned how to customize the watermark by changing its height and width. You also learned how to place the watermark exactly where you want it using the PDF Inspector.
Create an account, get an API key and test a request.