Files
STCourses/packages/feature-import/src/pdf.ts
T

24 lines
890 B
TypeScript

import { join } from "node:path";
import { run } from "./util/exec";
export interface DeckRender { pages: number; title: string }
const TOOL = join(import.meta.dir, "..", "py", "pdf_tool.py");
/**
* Renders page images (p-001.jpg ...) and text.json into `outDir` with PyMuPDF in a subprocess,
* so the server process never holds a PDF engine. `python` is STC_PYTHON (default python3).
*/
export async function renderPdf(python: string, pdf: string, outDir: string, opts: { onPage?: (page: number, total: number) => void; signal?: AbortSignal } = {}): Promise<DeckRender> {
let last = "";
await run([python, "-I", TOOL, "render", pdf, outDir], {
signal: opts.signal,
onLine(l) {
const m = /^page (\d+)\/(\d+)$/.exec(l);
if (m) opts.onPage?.(Number(m[1]), Number(m[2]));
else if (l.trim()) last = l;
},
});
return JSON.parse(last) as DeckRender;
}