DEV Community

Jeremy K.
Jeremy K.

Posted on

Changing PDF Page Size with Python

In practice, a PDF's page size often determines whether it prints cleanly, archives correctly, or embeds smoothly in another system. A report designed for A3 may need to be reduced to A4 for distribution, or scanned pages may need to be standardized to a custom size. Adjusting pages one by one is slow and error-prone; a Python script is a more reliable way to handle the job in batch.

Free Spire.PDF for Python is a lightweight, capable tool for this task. It has a small footprint and a straightforward API, which makes it well-suited to page-level restructuring. Below, we cover several common ways to change PDF page size with it, from the underlying idea to working code.


Prerequisites

Install it with pip:

pip install spire.pdf.free
Enter fullscreen mode Exit fullscreen mode

The free version limits the number of pages per document (usually to 10 or fewer), so it is best suited to small jobs and proofs of concept.


Core Idea: Why You Can't Just "Change the Size"

PDF page size (MediaBox) is technically an editable property, but changing it in place will throw off text and images that were laid out for the old dimensions. Content may become misaligned or even clipped. A more reliable approach is to rebuild each page:

  1. Load the original document and create a page template for each page, capturing a snapshot of all its content.
  2. Create a new document with blank pages at the target size.
  3. Draw the template onto the new page, using drawing coordinates and a scaling layout to fit the content.
  4. Save the new document and release resources.

Free Spire.PDF's PdfPageBase.CreateTemplate() and PdfTemplate.Draw() are designed for exactly this workflow. The same logic works whether the target size is larger, smaller, or has a different aspect ratio.


Scenario 1: Change to Standard Paper Sizes (A3, B4, Letter, etc.)

Free Spire.PDF includes enums for common standard paper sizes, such as PdfPageSize.A3(), PdfPageSize.B4(), and PdfPageSize.Letter(). The following code converts every page of a PDF to B4:

from spire.pdf.common import *
from spire.pdf import *

input_file = "source.pdf"
output_file = "resized_b4.pdf"

original_pdf = PdfDocument()
original_pdf.LoadFromFile(input_file)

new_pdf = PdfDocument()

for i in range(original_pdf.Pages.Count):
    page = original_pdf.Pages.get_Item(i)

    # Create a new B4-sized page with zero margins to avoid extra whitespace
    new_page = new_pdf.Pages.Add(PdfPageSize.B4(), PdfMargins(0.0))

    # Key: set the text layout to OnePage so the original content is scaled
    # as a whole to fit the new page
    layout = PdfTextLayout()
    layout.Layout = PdfLayoutType.OnePage

    template = page.CreateTemplate()
    template.Draw(new_page, PointF.Empty(), layout)

new_pdf.SaveToFile(output_file)
new_pdf.Close()
original_pdf.Close()
Enter fullscreen mode Exit fullscreen mode

The important setting here is PdfLayoutType.OnePage. Without it, content is drawn at its original scale, and anything beyond the new page bounds is clipped rather than automatically scaled. With it, the template is scaled to fit the available area of the new page, keeping the content fully visible.


Scenario 2: Custom Sizes (Millimeters, Inches, or Any Other Unit)

Real-world targets are rarely standard paper sizes. A print job might call for 200 mm × 280 mm, or a system might require pages to be 6 × 9 inches. Free Spire.PDF uses points (1 point = 1/72 inch). Use PdfUnitConvertor to convert physical units to points, then create a SizeF:

from spire.pdf.common import *
from spire.pdf import *

input_file = "source.pdf"
output_file = "custom_size.pdf"

# Target size: 200 mm × 280 mm
converter = PdfUnitConvertor()
width_pt = converter.ConvertUnits(200, PdfGraphicsUnit.Millimeter, PdfGraphicsUnit.Point)
height_pt = converter.ConvertUnits(280, PdfGraphicsUnit.Millimeter, PdfGraphicsUnit.Point)

original_pdf = PdfDocument()
original_pdf.LoadFromFile(input_file)

new_pdf = PdfDocument()

for i in range(original_pdf.Pages.Count):
    page = original_pdf.Pages.get_Item(i)

    new_page = new_pdf.Pages.Add(SizeF(width_pt, height_pt), PdfMargins(0.0))

    layout = PdfTextLayout()
    layout.Layout = PdfLayoutType.OnePage

    template = page.CreateTemplate()
    template.Draw(new_page, PointF.Empty(), layout)

new_pdf.SaveToFile(output_file)
new_pdf.Close()
original_pdf.Close()
Enter fullscreen mode Exit fullscreen mode

The first argument of ConvertUnits is the value, the second is the source unit, and the third is the target unit. Supported units include Millimeter, Inch, Pixel, Point, and others. If you are unsure of the original page size, read it first for reference:

page = original_pdf.Pages[0]
print(f"Original page size: {page.Size.Width} × {page.Size.Height} points")
Enter fullscreen mode Exit fullscreen mode

Scenario 3: Proportional Scaling (Enlarge or Shrink)

Sometimes you don't have a fixed target size; you just want to enlarge everything by 1.2x or shrink it to 80%. In that case, multiply the original dimensions by a factor—simpler than the previous two approaches:

from spire.pdf.common import *
from spire.pdf import *

input_file = "source.pdf"
output_file = "scaled_120.pdf"

scale = 1.2

original_pdf = PdfDocument()
original_pdf.LoadFromFile(input_file)

new_pdf = PdfDocument()

for i in range(original_pdf.Pages.Count):
    page = original_pdf.Pages.get_Item(i)

    new_width = float(page.Size.Width * scale)
    new_height = float(page.Size.Height * scale)
    new_page = new_pdf.Pages.Add(SizeF(new_width, new_height), PdfMargins(0.0))

    layout = PdfTextLayout()
    layout.Layout = PdfLayoutType.OnePage

    template = page.CreateTemplate()
    template.Draw(new_page, PointF.Empty(), layout)

new_pdf.SaveToFile(output_file)
new_pdf.Close()
original_pdf.Close()
Enter fullscreen mode Exit fullscreen mode

With proportional scaling, the aspect ratio stays the same, so content is not stretched or distorted. PdfLayoutType.OnePage mainly acts as a safeguard here: even if one page's original size differs slightly from the others, the content still fits fully into the new page.


Summary

Redrawing pages through templates is a stable, reliable way to resize PDFs, especially for batch normalization. The core workflow is four steps: read the original page → create a template → create a new page → draw the template onto it. From there, you can adjust the scaling logic, margins, and alignment to fit your needs.

Top comments (0)