pdfrw2 - PDF Reader/Writer Library

0.5.0 · active · verified Thu Apr 16

pdfrw2 is a maintenance fork of the unmaintained pdfrw library, providing a Pythonic way to read, write, and manipulate PDF files. It allows for tasks like merging, splitting, watermarking, and adding annotations to PDFs. The current version is 0.5.0, and it follows an as-needed release cadence for bug fixes and compatibility updates.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to read an existing PDF file and write its contents to a new PDF file using pdfrw2. It shows the basic usage of `PdfReader` and `PdfWriter` to copy a PDF. For a more robust example with actual PDF content, you would typically start with a real PDF file.

import os
from pdfrw import PdfReader, PdfWriter

# Create dummy PDF files for demonstration if they don't exist
# In a real scenario, you would have existing PDF files.
# This example just copies a (potentially empty) file to simulate input.
# For a true example, you'd need a PDF generator or existing files.

def create_dummy_pdf(filename):
    # This is a highly simplified 'creation' for demonstration purposes.
    # In a real app, you'd use a library like reportlab or have actual PDFs.
    with open(filename, 'w') as f:
        f.write('%PDF-1.4\n1 0 obj <</Type/Catalog/Pages 2 0 R>> endobj\n2 0 obj <</Type/Pages/Count 0>> endobj\nxref\n0 3\n0000000000 65535 f\n0000000009 00000 n\n0000000052 00000 n\ntrailer<</Size 3/Root 1 0 R>>startxref\n106\n%%EOF')

input_pdf_path = 'input.pdf'
output_pdf_path = 'output.pdf'

if not os.path.exists(input_pdf_path):
    print(f"Creating dummy {input_pdf_path} for quickstart...")
    create_dummy_pdf(input_pdf_path)

try:
    # Read an existing PDF
    trailer = PdfReader(input_pdf_path)

    # Create a new PDF writer
    writer = PdfWriter()

    # Add all pages from the input PDF to the writer
    writer.addpages(trailer.pages)

    # Write the combined PDF to a new file
    writer.write(output_pdf_path)

    print(f"Successfully copied '{input_pdf_path}' to '{output_pdf_path}'")

except Exception as e:
    print(f"An error occurred: {e}")
finally:
    # Clean up dummy files
    # os.remove(input_pdf_path) # Uncomment to remove after running
    # os.remove(output_pdf_path) # Uncomment to remove after running
    pass

view raw JSON →