Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,38 @@ options:
-p PDF, --pdf PDF Path to the PDF file
```

## PDF reading and editing

`PdfReader`, `PdfWriter` and `PdfEditor` read, assemble and change PDF files.
Reader and Writer adapt the implemented pypdf workflows; Editor provides named
arguments, one-based page numbers and rendering.

```python
from docling_parse import PdfEditor, PdfReader, PdfWriter

# read
with PdfReader("document.pdf") as reader:
print(len(reader.pages), reader.metadata.get("Title"))
text = reader.pages[0].extract_text()

# merge
writer = PdfWriter()
writer.append("file1.pdf")
writer.append("file2.pdf", pages=[0, 2])
writer.write("merged.pdf")

# edit and save
with PdfEditor(source="document.pdf") as editor:
editor.get_page(page_no=1).rotate(angle=90)
editor.remove_page(page_no=4)
editor.render_page(page_no=1, scale=2.0).save("preview.png")
editor.write(target="changed.pdf")
```

See [the editing guide](./docs/editing.md) for API coverage, numbering rules and
pypdf migration differences, with recipes for [pages](./docs/editing/pages.md),
[forms](./docs/editing/forms.md) and [rendering](./docs/editing/rendering.md).

## System fonts

A PDF may name a font without embedding it. There is then nothing in the file
Expand Down
8 changes: 8 additions & 0 deletions app/pybind_parse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
// Include edit headers for the lightweight, document-less helpers
#include <edit.h>

// pdf_reader, pdf_writer and pdf_editor: all of their bindings live in this
// header, so this file only has to register them
#include <pybind/edit_bindings.h>

namespace
{
const char* pixel_format_name(pdflib::pixel_format fmt)
Expand Down Expand Up @@ -1565,4 +1569,8 @@ PYBIND11_MODULE(pdf_parsers, m) {

Returns:
_PageRenderResult: The result of a page rendering task.)");

// ============= PDF Reader / Writer / Editor =============

docling::register_edit_bindings(m);
}
20 changes: 20 additions & 0 deletions docling_parse/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,23 @@ def _ensure_safe_numeric_locale() -> None:


_ensure_safe_numeric_locale()

# PdfReader, PdfWriter and PdfEditor are importable from the package, but only
# loaded on first use: `import docling_parse` stays as cheap as before.
_LAZY_EXPORTS = {
"PdfReader": "docling_parse.pdf_reader",
"PdfWriter": "docling_parse.pdf_writer",
"PdfEditor": "docling_parse.pdf_editor",
}


def __getattr__(name: str):
module_name = _LAZY_EXPORTS.get(name)
if module_name is None:
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")

import importlib

value = getattr(importlib.import_module(module_name), name)
globals()[name] = value
return value
Loading
Loading