RJBULK-APP/scripts/render-pdfs.mjs

38 lines
1.1 KiB
JavaScript
Raw Normal View History

/**
* Render PDF files in /tmp/rjbulk-pdfs to PNGs for visual review.
* Usage: node scripts/render-pdfs.mjs
*/
import puppeteer from "puppeteer-core";
import { readdirSync } from "fs";
import { join } from "path";
const CHROME = "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome";
const PDF_DIR = "/tmp/rjbulk-pdfs";
const OUT_DIR = "/tmp/rjbulk-pdf-images";
async function main() {
const browser = await puppeteer.launch({
executablePath: CHROME,
headless: "new",
args: ["--no-sandbox", "--disable-gpu"],
});
const files = readdirSync(PDF_DIR).filter((f) => f.endsWith(".pdf"));
for (const f of files) {
const page = await browser.newPage();
await page.setViewport({ width: 900, height: 1200 });
await page.goto(`file://${PDF_DIR}/${f}`, { waitUntil: "networkidle2", timeout: 30000 });
await new Promise((r) => setTimeout(r, 500));
const name = f.replace(".pdf", ".png");
await page.screenshot({ path: join(OUT_DIR, name), fullPage: true });
console.log(` ✓ ${name}`);
await page.close();
}
await browser.close();
}
main().catch((e) => {
console.error(e);
process.exit(1);
});