This Python script allows you to modify PDF documents by:
- Adding custom text to PDF pages at specified positions
- Inserting images into PDF pages at specified coordinates
The script requires Python and the following libraries:
fitz(PyMuPDF)argparse
To install the required packages, use the following command:
pip install pymupdfThe script supports two main commands: text and image.
To add text to a PDF:
python convert.py input.pdf output.pdf text "Text to add" --x 100 --y 50input_pdf: Path to the input PDF fileoutput_pdf: Path to the output PDF file where the modified PDF will be savedtext: The text to add to each page of the PDF--x: (Optional) X coordinate for the text. Default is 60% of the page width--y: (Optional) Y coordinate for the text. Default is 30 pixels from the bottom of the page
python convert.py input.pdf output.pdf text "Registration number: 2024/10/12" --x 100 --y 50To add an image to a PDF:
python convert.py input.pdf output.pdf image path/to/image.jpg --x 100 --y 100 --width 200 --height 150 --pages 1 2 3input_pdf: Path to the input PDF fileoutput_pdf: Path to the output PDF fileimage_path: Path to the image file to insert--x: (Required) X coordinate for the image placement--y: (Required) Y coordinate for the image placement--width: (Optional) Desired width of the image in points--height: (Optional) Desired height of the image in points--pages: (Optional) List of page numbers to add the image to. If not specified, adds to all pages
# Add image to specific pages with custom dimensions
python convert.py input.pdf output.pdf image logo.png --x 50 --y 50 --width 100 --height 100 --pages 1 2 3
# Add image to all pages using default dimensions
python convert.py input.pdf output.pdf image watermark.png --x 200 --y 300You can view the help message for each command:
# General help
python convert.py --help
# Text command help
python convert.py input.pdf output.pdf text --help
# Image command help
python convert.py input.pdf output.pdf image --helpThe PDF coordinate system starts from the bottom-left corner of the page:
- X coordinates increase from left to right
- Y coordinates increase from bottom to top
- Units are in points (1/72 of an inch)
- Adding a watermark text to all pages:
python convert.py input.pdf output.pdf text "CONFIDENTIAL" --x 300 --y 400- Adding a logo to the first three pages:
python convert.py input.pdf output.pdf image company_logo.png --x 50 --y 750 --width 200 --height 100 --pages 1 2 3- Adding a small image to all pages:
python convert.py input.pdf output.pdf image stamp.png --x 500 --y 20 --width 50 --height 50This project is open-source and available under the MIT License.