Skip to content

Add PDFium backend and PDF operations API#9

Merged
magic-alt merged 1 commit into
mainfrom
research/pdf-engine-evaluation
May 22, 2026
Merged

Add PDFium backend and PDF operations API#9
magic-alt merged 1 commit into
mainfrom
research/pdf-engine-evaluation

Conversation

@magic-alt

Copy link
Copy Markdown
Owner

Summary

Add the PDFium evaluation and document-operations work needed to support extract/delete/reorder/rotate flows end to end.

  • add a PDFium-backed engine implementation and PDF operations support
  • expose the new operation result contract through API routes, docs, and web types
  • document the PDF engine evaluation and add focused coverage for the new engine and web routes

Testing

  • python -m pytest tests/test_pdfium_engine.py tests/test_routes_web.py

Copilot AI review requested due to automatic review settings May 22, 2026 00:31
@magic-alt magic-alt merged commit c7de9c5 into main May 22, 2026
5 checks passed

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds an optional PDFium (pypdfium2) backend and a page-level PDF operations flow (extract/delete/reorder/rotate) exposed end-to-end via backend routes, frontend API types, and tests. This fits into the existing src/pdf/ modularization by introducing an engine abstraction and adding derived-PDF artifacts alongside raw.pdf.

Changes:

  • Introduce src/pdf/engine.py abstraction with PyMuPDF (default) and PDFium implementations; refactor extraction/rendering to use the configured engine.
  • Add PDF operations module and new API routes to create and download derived PDFs with manifests.
  • Add web API typings/helpers plus focused pytest coverage and supporting documentation/env/packaging updates.

Reviewed changes

Copilot reviewed 13 out of 13 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
web/src/api/types.ts Adds PdfOperationResult type for derived PDF operation responses.
web/src/api/docs.ts Adds client helpers for creating PDF operations and downloading derived PDFs.
tests/test_routes_web.py Adds route-level tests for creating and fetching derived PDFs.
tests/test_pdfium_engine.py Adds PDFium engine + operations unit tests (skipped if pypdfium2 missing).
src/pdf/render.py Refactors rendering to delegate to the configured PDF engine.
src/pdf/operations.py Implements PDFium-backed page-level operations (extract/delete/reorder/rotate/merge).
src/pdf/extractor.py Refactors extraction to delegate to the configured PDF engine; keeps compatibility wrapper.
src/pdf/engine.py New engine abstraction + implementations for PyMuPDF and PDFium.
src/api/routes.py Adds PDF operations and derived-PDF download routes and request model.
pyproject.toml Adds optional dependency group pdfium and includes it in all.
docs/pdf_engine_evaluation.md Adds evaluation/decision doc for PDF engines and editing approach.
docs/architecture_and_capabilities.md Updates architecture docs to include engine abstraction and operations routes/module.
.env.example Documents new PDF_ENGINE environment variable.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/pdf/engine.py
Comment on lines +191 to +194
if render_path:
img_path = render_path / f"page_{idx + 1:04d}.png"
self._save_page_bitmap(page, img_path, dpi=200)
images.append(str(img_path))
Comment thread src/pdf/engine.py
Comment on lines +254 to +266
def _save_page_bitmap(page, img_path: Path, *, dpi: int) -> None:
bitmap = page.render(
scale=dpi / 72.0,
draw_annots=True,
fill_color=(255, 255, 255, 255),
optimize_mode="lcd",
)
try:
bitmap.to_pil().save(str(img_path))
finally:
close = getattr(bitmap, "close", None)
if close is not None:
close()
Comment thread src/api/routes.py
Comment on lines +341 to +342
if not str(derived_dir).startswith(str(root)):
return _err("bad_request", "Invalid derived artifact path")

### PDFium Backend PoC

- Add optional dependency group, for example `pdfium = ["pypdfium2>=4"]`.
**3. PDF 处理层**
- `src/pdf/extractor.py` — PyMuPDF 文本抽取 + PDF 格式校验(`%PDF-` 头部检测)
- `src/pdf/engine.py` — PDF 引擎抽象,默认 PyMuPDF,可通过 `PDF_ENGINE=pdfium` 切换到 pypdfium2/PDFium
- `src/pdf/extractor.py` — 基于配置引擎的文本抽取 + PDF 格式校验(`%PDF-` 头部检测)
magic-alt added a commit that referenced this pull request Jun 6, 2026
Add PDFium backend and PDF operations API
@magic-alt magic-alt deleted the research/pdf-engine-evaluation branch June 6, 2026 16:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants