"""Compile a test manuscript's LaTeX output (texlive.net, test content only) and save its pages as PNGs, to look at. python devtools/fmt_pages.py ieee_journal [numeric] [pages] -> devtools/logs/pages/__p.png""" import io import os import subprocess import sys import tempfile import zipfile from pathlib import Path ROOT = Path(__file__).resolve().parent.parent sys.path.insert(0, str(ROOT)) sys.path.insert(0, str(Path(__file__).resolve().parent)) os.environ.setdefault("SKIP_MODEL_LOAD", "1") import fmt_corpus as C # noqa: E402 from formatmatch.engine import FormatSource, build_spec, format_manuscript # noqa: E402 preset = sys.argv[1] kind = sys.argv[2] if len(sys.argv) > 2 else "numeric" pages = int(sys.argv[3]) if len(sys.argv) > 3 else 2 b = io.BytesIO() C.build(kind).save(b) r = format_manuscript(f"{kind}.docx", b.getvalue(), build_spec(FormatSource(preset=preset))) z = zipfile.ZipFile(io.BytesIO(r.latex_zip)) out_dir = ROOT / "devtools" / "logs" / "pages" out_dir.mkdir(parents=True, exist_ok=True) with tempfile.TemporaryDirectory() as td: args = ["curl", "-s", "-L", "--max-time", "200", "-o", str(Path(td) / "out.pdf")] for n in z.namelist(): if n.endswith((".tex", ".bib", ".cls", ".sty", ".bst")): body = z.read(n).decode("utf-8") if n == "main.tex": body, n = "\\PassOptionsToPackage{demo}{graphicx}\n" + body, "document.tex" p = Path(td) / n.replace("/", "_") p.write_text(body, encoding="utf-8", newline="\n") args += ["-F", f"filecontents[]=<{p}", "-F", f"filename[]={n}"] args += ["-F", "engine=pdflatex", "-F", "return=pdf", "https://texlive.net/cgi-bin/latexcgi"] subprocess.run(args, capture_output=True, timeout=260) data = (Path(td) / "out.pdf").read_bytes() if data[:4] != b"%PDF": print("no PDF:", data[-600:].decode("utf-8", "replace")) sys.exit(1) import pymupdf # noqa: E402 d = pymupdf.open(stream=data, filetype="pdf") print(preset, kind, len(d), "pages") for i in range(min(pages, len(d))): out = out_dir / f"{preset}_{kind}_p{i + 1}.png" d[i].get_pixmap(dpi=70).save(str(out)) print(out)