pypdf Documentation

repository·main·Indexed 27 days ago

https://github.com/py-pdf/pypdf

A free and open-source pure-python PDF library for manipulation tasks such as splitting, merging, cropping, transforming, and extracting text or metadata. The library includes the PdfReader class for parsing and text extraction, and the PdfWriter class for generating PDF files.

Tokens
36.6K
Snippets
78
Records
258
Agent score
95%

What's inside pypdf

  1. Overview of pypdf capabilities

    main

    pypdf is a free and open-source pure-python PDF library. It provides tools for common PDF manipulation tasks including:

    • File Manipulation: Splitting, merging, cropping, and transforming PDF pages.
    • Metadata & Content: Retrieving text and metadata from PDF files.
    • Enhancements: Adding custom data, viewing options, and passwords to PDF files.
    • Advanced Features: Handling attachments, annotations, forms, and watermarks.
  2. Understand the scope of pypdf capabilities

    main

    pypdf is a library designed to simplify interactions with PDF documents. Its core capabilities include:

    • Document manipulation: Splitting, merging, cropping, and transforming PDF pages.
    • Data Extraction: Extracting text and metadata from PDF documents.
    • Security: Decrypting and encrypting PDF documents.

    Note on OCR: pypdf does not perform Optical Character Recognition (OCR). It can only extract text that is already present in a text layer. If you cannot select or copy text in a PDF, it is likely an image, and you will need an OCR tool like tesseract to extract it.

  3. Understand how pypdf parses PDF files

    main

    pypdf uses the pypdf.PdfReader class to parse PDF files. The parsing process follows these steps:

    1. Finding and reading the cross-reference tables / trailer: pypdf locates the xref table (byte offsets for object locations) and the trailer (which contains the root Catalog and metadata Info object).
    2. Parsing the objects: pypdf parses various object types (dictionaries, arrays, streams, integers, strings) and stores them in PdfReader.resolved_objects. These are populated via cache_indirect_object.
    3. Decoding content streams: PDF content is stored in streams containing operators and operands. pypdf decodes these streams using specified filters (like FlateDecode or LZWDecode) only when an object is explicitly requested via PdfReader.get_object (which internally uses PdfReader._get_object_from_stream).
  4. Understand PDF/A compliance in pypdf

    main

    PDF/A is an ISO-standardized version of PDF designed for long-term archiving. It requires embedding all necessary fonts, images, and metadata (like XMP) within the document to ensure consistent reproduction without external dependencies.

    Important Note: Currently, pypdf does not make any guarantees regarding PDF/A compliance. If your workflow requires strict adherence to PDF/A standards, you should be aware that pypdf may not automatically satisfy all requirements (such as embedded fonts, device-independent color spaces, or specific MarkInfo objects) during document manipulation.

  5. Understand the PDF file structure

    main

    A PDF file is composed of four main sections that define its content and how to navigate it:

    1. Header: Specifies the PDF version (e.g., %PDF-1.7). By default, pypdf generates files with a PDF 1.3 header but strives to support the 1.7 specification.
    2. Body: A sequence of indirect objects containing the actual content.
    3. Cross-reference table (xref): A table that maps indirect objects to their byte offsets in the file for quick access.
    4. Trailer: Contains the trailer dictionary (including the /Root catalog and /Size of the xref table) and the startxref pointer, which indicates the byte location of the xref table. The file ends with the %%EOF marker.
  6. Understand the difference between pypdf and OCR

    main

    It is important to distinguish between text extraction and Optical Character Recognition (OCR):

    • pypdf is NOT OCR software: It extracts text that is already digitally present in the PDF's content stream. It cannot detect or extract text that exists only as an image.
    • Digitally-born PDFs: These contain actual text objects. pypdf is highly accurate here and will not confuse characters (e.g., o, O, 0, ö) because it reads the underlying encoding.
    • Scanned PDFs: These are just images. pypdf cannot extract text from these directly.
    • OCRed PDFs: These are scanned images that have had OCR software run over them, placing a hidden text layer in the background. pypdf can extract this hidden text layer, but the quality depends on the accuracy of the OCR software used during the scan.
  7. Understand the PdfDocCommon base class

    main

    PdfDocCommon is an abstract base class used by pypdf. It provides common functionality shared between PdfReader and PdfWriter.

    When interacting with the API, if a method or property is documented as accepting a PdfDocCommon type, you can provide either an instance of pypdf.PdfReader or pypdf.PdfWriter.

  8. Compare pypdf with other PDF libraries

    main

    When choosing a PDF library, consider the following comparisons with pypdf:

    vs PyMuPDF and PikePDF

    • pypdf: Pure Python with no C dependencies, making installation easier and avoiding potential security concerns related to C libraries. It has extensive community support and examples.
    • PyMuPDF / PikePDF: These are Python bindings to C libraries (MuPDF and QPDF respectively). They may have harder installation processes and potential licensing requirements (e.g., MuPDF may require a commercial license).

    vs pdfminer.six and pdfplumber

    • pypdf: Capable of both reading (text/metadata) and writing/modifying (splitting, merging, cropping, transforming) PDF files.
    • pdfminer.six: Specialized in extracting font size and weight, but cannot write or modify PDF files.
    • pdfplumber: Built on pdfminer.six and focused on data extraction. It can convert PDFs to images and draw on them, but it cannot export or modify PDF files.

    vs Document Generation tools

    • pypdf is a manipulation and extraction library, not a document generation tool. For creating PDFs from scratch, use dedicated PDF generation libraries.
  9. Understand the relationship between pypdf and PyPDF2

    main
    The pypdf library is the successor to PyPDF2. PyPDF2 was originally a fork of the original pyPdf, and after several years, the fork was merged back into the current pypdf project (which uses all lowercase naming).
  10. Use pypdf for PDF manipulation and extraction

    main

    Use pypdf if you need a pure-Python library to perform the following tasks:

    • Splitting and Merging: Divide a PDF into multiple files or combine several PDFs into one.
    • Cropping and Transforming: Adjust the page boundaries or apply transformations to PDF pages.
    • Metadata and Text Extraction: Retrieve text content and metadata from existing PDF files.
    • Customization: Add custom data, viewing options, or passwords to PDF files.
  11. Merge PDF forms with field name grouping

    main
    To prevent field name collisions when merging PDFs containing forms, use reader.add_form_topname("group_name") before appending or merging. This prefixes existing field names with the group name (e.g., field1 becomes group_name.field1).
  12. Understand CMap beginbfchar mapping

    main

    The beginbfchar directive in a CMap maps a specific byte to a specific Unicode character.

    Example:

    1 beginbfchar
    <1B> <FB00>
    endbfchar

    In this case, the byte 1B (hexadecimal for 27) maps to the Unicode character FB00 (the ligature ff).