Files
stack/src/llm/convert.py
kert 523be76fc6
Some checks failed
CI / notebooks-smoke (push) Successful in 1m31s
Deploy / notebooks (push) Has been skipped
Deploy / zotero (push) Has been skipped
Deploy / docs (push) Has been skipped
CI / test (push) Failing after 2m23s
Deploy / mc (push) Has been skipped
Infra CI / notebooks (push) Successful in 54s
Deploy / llm (push) Successful in 1m32s
Infra CI / llm (push) Successful in 14s
Infra CI / mc (push) Failing after 34s
Deploy / report (push) Successful in 14s
Deploy / api (push) Has been skipped
Infra CI / zotero (push) Successful in 19s
Infra CI / docs (push) Successful in 18s
Infra CI / api (push) Successful in 1m6s
CI / lint (push) Successful in 36s
feat(llm): Word-exact pages — LibreOffice in the llm image converts .docx to PDF on demand (cached) for the document viewer
llm.convert.to_pdf runs headless Writer (soffice --convert-to
pdf:writer_pdf_Export, its own profile dir, one at a time) and caches
the result by the source's path/size/mtime under .state/llm/pdfcache
(compose mounts it read-write). The image adds
libreoffice-writer-nogui plus Carlito/Caladea/Liberation/DejaVu so
Calibri/Cambria/Times/Arial letters paginate like Word.

/pdf/<key> now returns, per file, the url the viewer should load and
the download url of the original; a Word file lists as renderer pdf
with url …&as=pdf when the converter is present, so the PDF.js path
(real pages, text layer, find, page links) shows it; /pdf/<key>/file
?as=pdf serves the conversion inline (502 on failure, 404 without the
converter). Browser-side docx-preview remains the fallback.
2026-09-24 19:12:14 -04:00

105 lines
3.5 KiB
Python

"""Word → PDF with LibreOffice, for Word-exact pages in the viewer.
``soffice --headless --convert-to pdf`` lays the document out the way
Writer does (real pagination, styles, metric-compatible fonts for
Calibri/Cambria/Times/Arial via Carlito/Caladea/Liberation), which the
browser-side docx-preview cannot. Conversions are cached by the source
file's identity (path, size, mtime) under ``cache_dir`` so a document is
converted once, and serialised behind one lock because a shared
LibreOffice profile cannot run two instances at a time.
available() → soffice is on PATH
to_pdf(src, cache_dir=…) → cached PDF path (converts on a miss)
"""
from __future__ import annotations
import hashlib
import logging
import os
import shutil
import subprocess
import tempfile
import threading
from pathlib import Path
log = logging.getLogger(__name__)
_LOCK = threading.Lock()
_TIMEOUT = 180
CONVERTIBLE = (".docx", ".doc", ".rtf", ".odt")
class ConversionError(RuntimeError):
pass
def soffice() -> str | None:
return shutil.which("soffice") or shutil.which("libreoffice")
def available() -> bool:
return soffice() is not None
def cache_key(src: Path) -> str:
st = src.stat()
raw = f"{src.resolve()}|{st.st_size}|{st.st_mtime_ns}"
return hashlib.sha1(raw.encode()).hexdigest() # noqa: S324 — cache identity
def to_pdf(src: Path, *, cache_dir: Path, runner=None) -> Path:
"""The PDF rendering of *src*, converting with LibreOffice when the
cache has no current copy. *runner* (tests) replaces ``subprocess.run``.
Raises ``ConversionError`` when soffice is missing, times out, fails or
produces nothing."""
src = Path(src)
if not src.is_file():
raise ConversionError(f"{src.name}: not on file")
cache_dir = Path(cache_dir)
cache_dir.mkdir(parents=True, exist_ok=True)
out = cache_dir / f"{cache_key(src)}.pdf"
if out.is_file() and out.stat().st_size > 0:
return out
exe = soffice()
if exe is None:
raise ConversionError("LibreOffice (soffice) is not installed in this image")
run = runner or subprocess.run
profile = cache_dir / ".lo-profile"
profile.mkdir(exist_ok=True)
with _LOCK, tempfile.TemporaryDirectory(prefix="lo-", dir=cache_dir) as tmp:
cmd = [
exe,
f"-env:UserInstallation=file://{profile}",
"--headless",
"--norestore",
"--nolockcheck",
"--convert-to",
"pdf:writer_pdf_Export",
"--outdir",
tmp,
str(src),
]
env = {**os.environ, "HOME": str(profile)}
try:
proc = run(cmd, capture_output=True, text=True, timeout=_TIMEOUT, env=env)
except subprocess.TimeoutExpired as exc:
raise ConversionError(
f"{src.name}: conversion timed out after {_TIMEOUT}s"
) from exc
produced = Path(tmp) / (src.stem + ".pdf")
if (
proc.returncode != 0
or not produced.is_file()
or produced.stat().st_size == 0
):
detail = (proc.stderr or proc.stdout or "").strip()[-300:]
raise ConversionError(
f"{src.name}: LibreOffice failed ({proc.returncode}) {detail}"
)
tmp_out = out.with_suffix(".pdf.part")
shutil.move(str(produced), tmp_out)
os.replace(tmp_out, out)
log.info("converted %s → %s (%d bytes)", src.name, out.name, out.stat().st_size)
return out