SPB Git

Repository Public

pdf-preview.mts 1.5 KB 24 lines typescript

1// Prints a published sheet to PDF with the same code as the export worker, for design work:2//   node_modules/.bin/tsx scripts/pdf-preview.mts <slug> <out.pdf>      (needs the web process running)3import "./_env";4import { writeFileSync } from "node:fs";5import { chromium } from "playwright-core";6import { sql } from "../src/lib/db";7import { env } from "../src/lib/env";8import { FORMAT_SIZES, mustParseSheet } from "../src/schema";9import { renderToken } from "../src/server/render-token";10import { printPdf } from "../src/worker/export";1112const [slug, out] = process.argv.slice(2);13const [row] = await sql<{ publication_id: string; doc: unknown }[]>`SELECT s.publication_id, p.doc FROM sheets s JOIN sheet_publications p ON p.id = s.publication_id WHERE s.slug = ${slug}`;14if (!row) throw new Error(`No published sheet ${slug}`);15const size = FORMAT_SIZES[mustParseSheet(row.doc).layout.format];16const browser = await chromium.launch();17const page = await (await browser.newContext({ viewport: { width: size.width + 96, height: 1200 }, reducedMotion: "reduce" })).newPage();18await page.goto(`${env.internalUrl}/render/${renderToken({ kind: "publication", id: row.publication_id }, 600)}`, { waitUntil: "networkidle" });19await page.evaluate(() => document.fonts.ready.then(() => undefined));20writeFileSync(out, await printPdf(page, size));21if (process.env.QS_DEBUG) console.log(await page.evaluate(() => document.body.dataset.qs));22await browser.close(); await sql.end();23console.log(`written ${out}`);24
Files main
Menu

Theme