git » alan.git » commit 82479a0

Require wkhtmltopdf for PDF rendering and update docs

author Alan Dipert
2025-12-04 16:16:35 UTC
committer Alan Dipert
2025-12-04 16:16:35 UTC
parent 748ae3a6aebdef542a2088aa95881f69723407b2

Require wkhtmltopdf for PDF rendering and update docs

README.md +2 -2
render_text.py +7 -21

diff --git a/README.md b/README.md
index 9e10b65..06ed33e 100644
--- a/README.md
+++ b/README.md
@@ -31,13 +31,13 @@ cat test_booklet.txt   # view the booklet
 cat answer_key.txt     # view the key
 ```
 
-- **Optional PDF output:** Requires `pandoc` plus a PDF engine (wkhtmltopdf or weasyprint recommended). Example:
+- **PDF output:** Requires `pandoc` **and** `wkhtmltopdf` (only supported engine). Example:
   ```bash
   python3 render_text.py --in generated_test.json \
     --test-out test_booklet.txt --key-out answer_key.txt \
     --test-pdf test_booklet.pdf --key-pdf answer_key.pdf
   ```
-  If no PDF engine is available, the script will skip PDF generation and report the issue. The booklet/key are rendered as Markdown, so bullets/headings convert cleanly to PDF when an engine is present.
+  If `pandoc` or `wkhtmltopdf` is missing, the script skips PDF generation and reports the issue. The booklet/key are rendered as Markdown, so bullets/headings convert cleanly to PDF when the tools are present.
 
 ## Generate Different Hardness Levels
 
diff --git a/render_text.py b/render_text.py
index 9fb88a4..2ab9f6e 100644
--- a/render_text.py
+++ b/render_text.py
@@ -93,32 +93,18 @@ def render_key(data: Dict[str, Any]) -> str:
 def _write_pdf(text: str, pdf_path: str, title: str) -> None:
     """Render plain text to PDF via pandoc if available."""
     pandoc = shutil.which("pandoc")
-    if not pandoc:
-        print("pandoc not found; skipping PDF generation.", file=sys.stderr)
+    wkhtml = shutil.which("wkhtmltopdf")
+    if not pandoc or not wkhtml:
+        print("pandoc + wkhtmltopdf required for PDF generation; skipping.", file=sys.stderr)
         return
     with tempfile.NamedTemporaryFile("w", delete=False, suffix=".txt") as tmp:
         tmp.write(f"{title}\n\n{text}")
         tmp_path = tmp.name
-    engines = [e for e in ["wkhtmltopdf", "weasyprint", "pdflatex"] if shutil.which(e)]
-    if not engines:
-        engines = [None]  # let pandoc pick default (may fail)
-    last_error = None
     try:
-        for eng in engines:
-            cmd = [pandoc, tmp_path, "-o", pdf_path, "--from", "gfm"]
-            if eng:
-                cmd.extend(["--pdf-engine", eng])
-            try:
-                subprocess.run(cmd, check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
-                last_error = None
-                break
-            except subprocess.CalledProcessError as e:
-                last_error = e
-        if last_error:
-            print(
-                "pandoc could not produce PDF; install a pdf engine such as wkhtmltopdf/weasyprint/pdflatex.",
-                file=sys.stderr,
-            )
+        cmd = [pandoc, tmp_path, "-o", pdf_path, "--from", "gfm", "--pdf-engine", "wkhtmltopdf"]
+        subprocess.run(cmd, check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
+    except subprocess.CalledProcessError:
+        print("pandoc + wkhtmltopdf failed to produce PDF.", file=sys.stderr)
     finally:
         try:
             import os