patristic-be / src /lib /pdf_export /__init__.py
Mario33333's picture
deploy: reader-access security hardening (feature/audiobook@06a5ed8)
7c2d250 verified
Raw
History Blame Contribute Delete
2.15 kB
"""TTS / screen-reader-friendly PDF builders.
Two flavors, both keyed by `book_id` and both backed by PyMuPDF:
make_text_pdf(book_id) -> bytes
A fresh PDF where each page contains only the cleaned Arabic text,
rendered RTL via `insert_htmlbox`. Small file, no images. Best for
TTS / screen readers / copy-paste.
make_searchable_pdf(book_id) -> bytes
The original PDF with an *invisible* text layer added to every
page (PDF render-mode 3). Visual fidelity is preserved; Cmd+F and
TTS extract the cleaned Arabic Unicode. CAVEAT: this ADDS a layer
without removing the page's existing text β€” for a PDF that already
ships a (garbage) text layer the two blend. Use make_clean_ocr_pdf
for those.
make_clean_ocr_pdf(book_id) -> bytes
Like make_searchable_pdf, but on any page that has an embedded text
layer it first rasterizes the page to an image β€” destroying the
original (often garbage) text β€” so the inserted OCR text is the
*single source of truth*. Pure-scan pages pass through untouched.
This is the "download to read aloud" build. Tunable via the
`pdf_export` config section.
Source text precedence: cleaned text from `data/clean/{book_id}/page_NNNN.json`
if present (statuses 'clean' / 'indexed'); else raw OCR text from
`data/ocr/{book_id}/page_NNNN.json`. Pages with no JSON at all are emitted
as blank text β€” never crash the build.
Font note: PyMuPDF's `insert_htmlbox` falls back through HarfBuzz / system
fonts to find an Arabic-capable face. On Windows / typical desktop Linux
this Just Works. If the runtime has no Arabic font installed, text-only
PDFs may render glyph boxes β€” but the Unicode in the content stream
remains extractable, so TTS still works. Bundling a TTF (e.g. Amiri OFL)
is the obvious next step if cloud deploys turn out to lack Arabic fonts.
"""
from __future__ import annotations
from .builder import make_clean_ocr_pdf, make_searchable_pdf, make_text_pdf
__all__ = ["make_text_pdf", "make_searchable_pdf", "make_clean_ocr_pdf"]