Quick navigation
Top-Level Functions
Start here when you need quick file-level tasks: open, merge, and batch rendering.Open a PDF document
Opens a PDF document and returns aDocument instance.
Signature: sopdf.open(path, password, *, stream)
Open from raw bytes in memory instead of a file. Mutually exclusive with
path.Document — The opened document object.
Merge multiple PDF files
Merges multiple PDF files into a single output file, in the order provided. Signature:sopdf.merge(inputs, output)
Render multiple pages to image bytes
Renders a list of pages to encoded image bytes. Signature:sopdf.render_pages(pages, *, dpi, format, alpha, parallel)
Rendering resolution in dots per inch. Common values: 72 (screen preview), 150 (high quality), 300 (print quality).
Whether to use multiprocessing for rendering. Bypasses the GIL for significant speedup on multi-core machines.
Returns:
list[bytes] — A list of encoded image bytes, one entry per page, in the same order as pages.
Render multiple pages and write files
Renders pages and writes the results to a directory aspage_0.png, page_1.png, etc.
Signature: sopdf.render_pages_to_files(pages, output_dir, *, dpi, format, alpha, parallel)
Document Object Operations
Focus on this section after you have aDocument instance and need page management, splitting, merging, or saving.
Document represents an open PDF document. It should never be constructed directly — always obtain one via sopdf.open().
Properties
Total page count
Member:doc.page_count or len(doc)
len(doc) is equivalent to doc.page_count.
Metadata
Document outline
len == 0 when the document has no bookmarks. Uses pypdfium2 — no pikepdf cost for read-only access.
Encryption status
True even when the correct password has been provided and the document opened successfully.
Page sequence
render_pages().
Page Access
Access page by index
Signature:doc[index] / doc.load_page(index)
doc[-1] returns the last page).
Iteration
Split
Split document by pages
Signature:doc.split(pages, output)
Document object.
If provided, the new document is also written to this path. Otherwise, it is returned in memory only.
Document — A new document containing the specified pages.
Split into single-page files
Signature:doc.split_each(output_dir)
page_0.pdf, page_1.pdf, etc.
Merge
Append pages from another document
Signature:doc.append(other)
save() or to_bytes() to persist the change.
Save
Save to file
Signature:doc.save(path, *, compress, garbage, linearize)
Whether to linearize the PDF for optimized sequential network access (Fast Web View).
Export as bytes
Signature:doc.to_bytes(*, compress)
bytes — The complete PDF file contents as bytes.
Lifecycle
Close document
Signature:doc.close()
with statement is recommended over calling this directly.
Context Manager
Page Object Operations
Use this section for single-page workflows such as rendering, text extraction, and text search.Page represents a single page within a document. Obtained via doc[i] or doc.load_page(i) — never constructed directly.
Properties
Page index
Member:page.number
Page dimensions
Member:page.rect
Rect in PDF points (1 pt = 1/72 inch) (read-only). Use rect.width and rect.height to get the page size.
Page rotation
Member:page.rotation
0, 90, 180, 270 (read/write).
Rendering
Render to image bytes
Signature:page.render(*, dpi, format, alpha)
Rendering resolution in dots per inch. Use 72 for screen preview, 300 for print quality.
Whether to include an alpha (transparency) channel. Only effective for PNG; JPEG does not support transparency.
Returns:
bytes — Encoded image bytes (PNG or JPEG).
Render and save image
Signature:page.render_to_file(path, *, dpi, format, alpha)
render().
Text Extraction
Extract plain text
Signature:page.get_text(*, rect)
Restrict extraction to this rectangular region. Extracts the full page when
None.str — The extracted plain text.
Extract text blocks
Signature:page.get_text_blocks(*, rect, format)
Restrict extraction to this rectangular region. Extracts the full page when
None.Return format.
"list" returns a list of TextBlock objects; "dict" returns a list of plain dictionaries with "text" and "rect" keys.format="list" → list[TextBlock]; format="dict" → list[dict], each of the form {"text": "...", "rect": {"x0": ..., "y0": ..., "x1": ..., "y1": ...}}
Text Search
Search text positions
Signature:page.search(query, *, match_case)
list[Rect] — Bounding rectangles for each match. Returns an empty list if no matches are found.
Search text with context blocks
Signature:page.search_text_blocks(query, *, match_case)
list[dict], each element contains:
Data Types
Refer to this section when you need to understand response structures (for exampleRect, TextBlock, and Metadata) for downstream processing.
Rect
Represents a rectangular region. Coordinates are in PDF points (pt, where 1 pt = 1/72 inch). The coordinate system has its origin at the top-left corner of the page, with x increasing rightward and y increasing downward.
Core properties (common)
All geometric operations return new
Rect instances — the original is immutable.
TextBlock
Represents a single block of text on a page, together with its bounding box.Metadata
Read/write proxy for the PDF Document Info dictionary. Obtained viadoc.metadata — never constructed directly.
Read path (zero pikepdf cost): each property calls pypdfium2.get_metadata_dict() after auto-syncing.
Write path (lazy pikepdf init): each setter calls _ensure_pike(), writes to pike_doc.docinfo, and marks the document dirty. The next read auto-syncs.
Core fields (common)
PDF date string format:
D:YYYYMMDDHHmmSSOHH'mm' (prefix D: and timezone optional).
OutlineItem
An immutable bookmark node in the document outline.Outline
Read-only bookmark tree manager. Obtained viadoc.outline — never constructed directly. The tree is built once on first access using pypdfium2’s TOC data — no pikepdf initialisation needed.
Exceptions
Read this section first when integrating PDF processing into production services and designing robust error handling. All exceptions inherit fromPDFError, which inherits from the built-in RuntimeError.
Recommended catch order: catch specific exceptions first (
PasswordError / FileDataError / PageError), then PDFError as a final fallback.

