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)
File-system path to the PDF. Mutually exclusive with
stream.Password for encrypted PDFs. Pass
None if no password is required.Open from raw bytes in memory instead of a file. Mutually exclusive with
path.Document — The opened document object.
| Exception | Condition |
|---|---|
PasswordError | The document requires a password that was not provided or is incorrect. |
FileDataError | The file is corrupted or cannot be parsed as a valid PDF. |
Merge multiple PDF files
Merges multiple PDF files into a single output file, in the order provided. Signature:sopdf.merge(inputs, output)
Ordered list of PDF file paths to concatenate.
Destination file path for the merged PDF.
| Exception | Condition |
|---|---|
ValueError | inputs list is empty. |
PasswordError | One of the input files requires a password. |
FileDataError | One of the input files cannot be read. |
Render multiple pages to image bytes
Renders a list of pages to encoded image bytes. Signature:sopdf.render_pages(pages, *, dpi, format, alpha, parallel)
List of page objects to render, typically from
doc.pages.Rendering resolution in dots per inch. Common values: 72 (screen preview), 150 (high quality), 300 (print quality).
Output image format:
"png" or "jpeg".Whether to include an alpha (transparency) channel. Only effective for PNG.
Whether to use multiprocessing for rendering. Bypasses the GIL for significant speedup on multi-core machines.
| Scenario | Recommended parameters |
|---|---|
| Screen preview | dpi=72, format="png", alpha=False, parallel=False |
| High-quality export | dpi=150, format="png", alpha=False, parallel=False |
| Large-document throughput | dpi=300, format="png", alpha=False, parallel=True |
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)
List of page objects to render.
Output directory path. Created automatically if it does not exist.
Rendering resolution in dots per inch.
Output image format:
"png" or "jpeg".Whether to include an alpha channel (PNG only).
Whether to use multiprocessing for rendering.
| Scenario | Recommended parameters |
|---|---|
| Preview thumbnails | dpi=72, format="png", parallel=False |
| Archive snapshots | dpi=150, format="png", parallel=False |
| Multi-core batch output | dpi=300, format="png", parallel=True |
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).
| Exception | Condition |
|---|---|
PageError | Index is out of range. |
Iteration
Split
Split document by pages
Signature:doc.split(pages, output)
Document object.
List of 0-based page indices to extract. The output order matches the list order.
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.
Output directory path. Created automatically if it does not exist.
Merge
Append pages from another document
Signature:doc.append(other)
save() or to_bytes() to persist the change.
The document whose pages will be appended.
Save
Save to file
Signature:doc.save(path, *, compress, garbage, linearize)
Destination file path.
Whether to compress content streams. Can significantly reduce file size.
Whether to generate object streams for additional structural compression.
Whether to linearize the PDF for optimized sequential network access (Fast Web View).
Export as bytes
Signature:doc.to_bytes(*, compress)
Whether to compress content streams.
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).
| Exception | Condition |
|---|---|
PageError | Set to a value other than 0, 90, 180, or 270. |
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.
Output image format:
"png" or "jpeg".Whether to include an alpha (transparency) channel. Only effective for PNG; JPEG does not support transparency.
| Scenario | Recommended parameters |
|---|---|
| On-screen preview | dpi=72, format="png", alpha=False |
| Crisp snapshot | dpi=150, format="png", alpha=False |
| Print-grade image | dpi=300, format="png", alpha=False |
bytes — Encoded image bytes (PNG or JPEG).
Render and save image
Signature:page.render_to_file(path, *, dpi, format, alpha)
render().
Output file path (including extension).
Rendering resolution in dots per inch.
Output image format:
"png" or "jpeg".Whether to include an alpha channel (PNG only).
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)
The text string to search for.
Whether the search is case-sensitive. Case-insensitive by default.
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)
The text string to search for.
Whether the search is case-sensitive.
list[dict], each element contains:
| Key | Type | Description |
|---|---|---|
"text" | str | Full text content of the block containing the match. |
"rect" | Rect | Bounding rectangle of the containing text block. |
"match_rect" | Rect | Precise bounding rectangle of the matched keyword itself. |
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.| Parameter | Type | Description |
|---|---|---|
x0 | float | Left edge (x-coordinate of the top-left corner). |
y0 | float | Top edge (y-coordinate of the top-left corner). |
x1 | float | Right edge (x-coordinate of the bottom-right corner). |
y1 | float | Bottom edge (y-coordinate of the bottom-right corner). |
| Property | Type | Description |
|---|---|---|
x0 | float | Left edge. |
y0 | float | Top edge. |
x1 | float | Right edge. |
y1 | float | Bottom edge. |
width | float | Rectangle width, equal to x1 - x0. |
height | float | Rectangle height, equal to y1 - y0. |
Rect instances — the original is immutable.
TextBlock
Represents a single block of text on a page, together with its bounding box.| Attribute / Method | Type | Description |
|---|---|---|
text | str | The text content of the block. |
rect | Rect | Bounding rectangle of the block on the page. |
to_dict() | dict | Converts to {"text": ..., "rect": {"x0": ..., "y0": ..., "x1": ..., "y1": ...}}. |
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)
| Property | Type | Description |
|---|---|---|
title | str | None | Document title (/Title). Read/write. |
author | str | None | Author name (/Author). Read/write. |
subject | str | None | Document subject (/Subject). Read/write. |
keywords | str | None | Search keywords (/Keywords). Read/write. |
D:YYYYMMDDHHmmSSOHH'mm' (prefix D: and timezone optional).
OutlineItem
An immutable bookmark node in the document outline.| Attribute / Method | Type | Description |
|---|---|---|
title | str | Bookmark label as displayed in the reader TOC panel. |
page | int | 0-based target page index; -1 when the item has no destination. |
level | int | Nesting depth; 0 = top-level item. |
children | tuple[OutlineItem, ...] | Nested child items (frozen tuple). |
to_dict() | dict | Serialize to a plain dict (recursive). |
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.
| Member | Returns | Description |
|---|---|---|
items | list[OutlineItem] | Top-level outline items (each may have nested children). |
to_list() | list[dict] | Flat DFS traversal. Each entry: {"level": int, "title": str, "page": int}. Compatible with PyMuPDF get_toc() output. |
len(outline) | int | Total number of nodes across all nesting levels. |
iter(outline) | — | Iterate over top-level items. |
bool(outline) | bool | True when the document has at least one outline item. |
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.
| Exception | When Raised | Handling Guidance | Recoverable |
|---|---|---|---|
PDFError | Base class for all sopdf exceptions. Catch this to handle any sopdf error. | Use as a top-level fallback for logging and unified user messaging. | Depends on subtype |
PasswordError | Opening an encrypted PDF with a missing or incorrect password. | Prompt again for password and limit retries. | Yes |
FileDataError | PDF file is corrupted, has an invalid format, or cannot be parsed. | Ask user to re-upload or replace the source file. | No |
PageError | Page index is out of range, or rotation is set to an invalid value (not 0/90/180/270). | Validate index/range and rotation before calling. | Yes |
PasswordError / FileDataError / PageError), then PDFError as a final fallback.

