PyPDFium2

5.6.0 · active · verified Sun Mar 29

pypdfium2 is an ABI-level Python 3 binding to PDFium, a powerful and liberal-licensed library for PDF rendering, inspection, manipulation, and creation. It provides both low-level ctypes-based access to the raw PDFium API and a higher-level Pythonic support model for common use cases. The library is actively maintained, with frequent updates often tied to new PDFium versions, and the current version is 5.6.0.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create a new PDF document with an empty A4 page and save it. It then shows how to open an existing PDF and retrieve basic information. This example is fully runnable without external files, as it creates and cleans up its own dummy PDF.

import pypdfium2 as pdfium
import os

# Create a new, empty PDF document with one A4 page
pdf = pdfium.PdfDocument.new()
page = pdf.new_page(595, 842) # A4 size in points (width, height)
page.close()

output_filename = 'example_output.pdf'
pdf.save(output_filename, version=17)
pdf.close()

print(f"Created PDF: {output_filename}")

# Example of opening an existing PDF and getting info (requires a dummy file)
# To make this runnable, we'll open the one we just created.
if os.path.exists(output_filename):
    existing_pdf = pdfium.PdfDocument(output_filename)
    print(f"Opened existing PDF with {len(existing_pdf)} pages.")
    existing_pdf.close()
    os.remove(output_filename) # Clean up the dummy file
    print(f"Cleaned up {output_filename}")
else:
    print(f"Error: {output_filename} not found for reading example.")

view raw JSON →