Skip to content

Instantly share code, notes, and snippets.

@buo
Last active May 22, 2023 06:25
Show Gist options
  • Save buo/b368047803f287e29d815e1bcad2636b to your computer and use it in GitHub Desktop.
Save buo/b368047803f287e29d815e1bcad2636b to your computer and use it in GitHub Desktop.
PDF Cropping Script - Python Script for Cropping PDF Pages to A4 Size

PDF Cropping Script

This Python script uses the PyPDF2 library to crop PDF pages to A4 size. It allows you to specify an input PDF file and generates an output PDF file with all pages cropped to A4 dimensions.

Background

Sometimes, PDF files may contain pages that are not in the standard A4 size (595.276 x 841.890 points). This script helps you standardize the page size by cropping all pages to A4 dimensions.

Quick Start

  1. Ensure you have Python installed on your system.

  2. Install the required PyPDF2 library by running the following command:

    pip install PyPDF2
  3. Download the crop.py file from this repository.

  4. In the script, specify the paths of your input PDF file and the desired output file.

    input_path = "/path/to/input.pdf"
    output_path = "/path/to/output.pdf"
  5. Run the script:

    python crop.py
  6. After the script execution, you will have a new PDF file at the specified output path, where all pages are cropped to A4 size.

Further Information

  • This script uses the PyPDF2 library. For more information about PyPDF2 and its functionalities, refer to the official documentation: PyPDF2 Documentation.

  • For more details on the A4 paper size and its dimensions, you can refer to standard paper size references or online resources.

Feel free to explore and modify the script according to your specific requirements!

from PyPDF2 import PdfWriter, PdfReader
def crop_to_a4(input_path, output_path):
# Create a PdfWriter object to store the modified PDF
pdf_writer = PdfWriter()
with open(input_path, "rb") as input_file:
# Create a PdfReader object to read the input PDF
pdf_reader = PdfReader(input_file)
for page_num in range(len(pdf_reader.pages)):
# Get the current page
page = pdf_reader.pages[page_num]
# Set the crop box of the page to A4 size
page.cropbox.lower_left = (0, 0)
page.cropbox.upper_right = (595.276, 841.890) # A4 size in points (72 points = 1 inch)
# Add the modified page to the PdfWriter
pdf_writer.add_page(page)
with open(output_path, "wb") as output_file:
# Write the modified PDF to the output file
pdf_writer.write(output_file)
# Specify the paths of the input and output PDF files
input_path = "/path/to/input.pdf"
output_path = "/path/to/output.pdf"
# Call the crop_to_a4 function to crop pages to A4 size
crop_to_a4(input_path, output_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment