-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild_pdf.js
More file actions
85 lines (72 loc) · 2.7 KB
/
Copy pathbuild_pdf.js
File metadata and controls
85 lines (72 loc) · 2.7 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/env node
/**
* Build FundsXML_Book.pdf from FundsXML_Book_Complete.html.
*
* Uses Playwright's bundled Chromium in headless mode to render the book
* with the print CSS, A4 page size, margins, page numbers, and a running
* header. The cover page (@page cover { margin: 0 }) suppresses header/
* footer via the CSS named-page rule + headerTemplate/footerTemplate
* filtering.
*
* Run: node build_pdf.js
*/
const path = require("path");
const fs = require("fs");
(async () => {
const ROOT = __dirname;
const INPUT = path.join(ROOT, "FundsXML_Book_Complete.html");
const OUTPUT = path.join(ROOT, "FundsXML_Book.pdf");
if (!fs.existsSync(INPUT)) {
console.error(`error: ${INPUT} not found. Run build_complete.sh first.`);
process.exit(1);
}
let chromium;
try {
({ chromium } = require("playwright"));
} catch (err) {
console.error(
"error: 'playwright' package not available. Install with 'npm i -g playwright' or 'npx playwright install chromium'."
);
process.exit(1);
}
const startedAt = Date.now();
const browser = await chromium.launch();
const page = await browser.newPage();
const url = "file://" + INPUT;
await page.goto(url, { waitUntil: "networkidle" });
// Give the injected TOC-building and anchor scripts a moment to finish.
await page.waitForTimeout(500);
await page.emulateMedia({ media: "print" });
// Running header: book title left, section (doc title) right.
// Chromium substitutes <span class="title"> with the <title> tag content.
const headerTemplate = `
<div style="font-size:8pt;width:100%;padding:0 18mm;color:#555;font-family:Inter,Arial,sans-serif;display:flex;justify-content:space-between;align-items:baseline;border-bottom:0.5pt solid #cfd4d9;padding-bottom:2pt;">
<span>FundsXML — The European Standard for Fund Data Exchange</span>
<span class="title" style="font-style:italic;color:#0b5394;"></span>
</div>`;
const footerTemplate = `
<div style="font-size:8pt;width:100%;padding:0 18mm;color:#555;font-family:Inter,Arial,sans-serif;text-align:center;">
<span class="pageNumber"></span>  /  <span class="totalPages"></span>
</div>`;
await page.pdf({
path: OUTPUT,
format: "A4",
printBackground: true,
displayHeaderFooter: true,
margin: {
top: "22mm",
bottom: "20mm",
left: "18mm",
right: "18mm",
},
headerTemplate,
footerTemplate,
});
await browser.close();
const sizeMB = (fs.statSync(OUTPUT).size / (1024 * 1024)).toFixed(2);
const secs = ((Date.now() - startedAt) / 1000).toFixed(1);
console.log(`Done. ${OUTPUT} (${sizeMB} MB, ${secs}s)`);
})().catch((err) => {
console.error(err);
process.exit(1);
});