-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathpdf.ts
39 lines (31 loc) · 981 Bytes
/
pdf.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import puppeteer from "puppeteer";
import { writeFileSync } from "node:fs";
import { fileURLToPath } from "node:url";
import { createServer } from "vite";
(async () => {
const __dirname = fileURLToPath(new URL(".", import.meta.url));
console.log("⏳ Starting Vite server");
const server = await createServer({
configFile: "vite.config.ts",
root: __dirname,
});
await server.listen();
console.log("🐾 Opening Puppeteer");
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto(server.resolvedUrls?.local[0] as string, {
waitUntil: "networkidle0",
});
console.log("🖨️ Generating PDF");
const pdf = await page.pdf({
format: "A4",
margin: { top: 0, bottom: 0, left: 0, right: 0 },
printBackground: true,
scale: 0.8,
});
console.log("💾 Saving PDF");
writeFileSync("output.pdf", pdf);
await browser.close();
await server.close();
console.log("🏁 Done");
})();