-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgenerate-pdf.mjs
More file actions
168 lines (137 loc) · 4.94 KB
/
Copy pathgenerate-pdf.mjs
File metadata and controls
168 lines (137 loc) · 4.94 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/usr/bin/env node
/**
* generate-pdf.mjs — HTML → PDF via Playwright
*
* Usage:
* node generate-pdf.mjs <input.html> <output.pdf> [--format=letter|a4]
* npm run pdf -- <input.html> <output.pdf> [--format=letter|a4]
* node generate-pdf.mjs --help
*
* Requires: the `playwright` package (see repo `package.json`; run `npx playwright install chromium`).
* Uses Chromium headless to render the HTML and produce a clean, ATS-parseable PDF.
*/
import { resolve, dirname } from 'path';
import { existsSync, mkdirSync } from 'fs';
import { readFile } from 'fs/promises';
import { fileURLToPath } from 'url';
const __dirname = dirname(fileURLToPath(import.meta.url));
const argvEarly = process.argv.slice(2);
if (argvEarly.includes('--help') || argvEarly.includes('-h')) {
console.log(`generate-pdf.mjs — HTML → PDF via Playwright (Chromium)
Renders an HTML file to a print-style PDF (default paper size A4; optional Letter).
Usage:
node generate-pdf.mjs <input.html> <output.pdf> [--format=letter|a4]
npm run pdf -- <input.html> <output.pdf> [--format=letter|a4]
Requires: the playwright package (see package.json) and a local browser build,
e.g. npx playwright install chromium
Self-hosted fonts: when repo-root fonts/ exists, ./fonts/ URLs in the HTML are
rewritten to absolute file:// paths. If fonts/ is missing, URLs are left unchanged.
Run from the repository root or any cwd; paths may be relative or absolute.
Creates the output directory (e.g. output/) when it does not exist yet.`);
process.exit(0);
}
async function generatePDF() {
const args = process.argv.slice(2);
// Parse arguments
let inputPath, outputPath, format = 'a4';
for (const arg of args) {
if (arg.startsWith('--format=')) {
format = arg.split('=')[1].toLowerCase();
} else if (!inputPath) {
inputPath = arg;
} else if (!outputPath) {
outputPath = arg;
}
}
if (!inputPath || !outputPath) {
console.error('Usage: node generate-pdf.mjs <input.html> <output.pdf> [--format=letter|a4]');
process.exit(1);
}
inputPath = resolve(inputPath);
outputPath = resolve(outputPath);
if (!existsSync(inputPath)) {
console.error(`Input file not found: ${inputPath}`);
process.exit(1);
}
// Validate format before creating output directories
const validFormats = ['a4', 'letter'];
if (!validFormats.includes(format)) {
console.error(`Invalid format "${format}". Use: ${validFormats.join(', ')}`);
process.exit(1);
}
const outputDir = dirname(outputPath);
if (!existsSync(outputDir)) {
mkdirSync(outputDir, { recursive: true });
console.log(`📁 Created directory: ${outputDir}`);
}
console.log(`📄 Input: ${inputPath}`);
console.log(`📁 Output: ${outputPath}`);
console.log(`📏 Format: ${format.toUpperCase()}`);
// Read HTML to inject font paths as absolute file:// URLs
let html = await readFile(inputPath, 'utf-8');
// Resolve font paths relative to repo-root fonts/ (skip if directory missing)
const fontsDir = resolve(__dirname, 'fonts');
if (existsSync(fontsDir)) {
html = html.replace(
/url\(['"]?\.\/fonts\//g,
`url('file://${fontsDir}/`
);
// Close any unclosed quotes from the replacement
html = html.replace(
/file:\/\/([^'")]+)\.woff2['"]\)/g,
`file://$1.woff2')`
);
} else {
console.warn(
`⚠️ fonts/ not found at ${fontsDir} — leaving @font-face URLs unchanged`
);
}
let chromium;
try {
({ chromium } = await import('playwright'));
} catch (e) {
if (e?.code === 'ERR_MODULE_NOT_FOUND') {
console.error(
'Missing dependency: run npm install in the repo root, then npx playwright install chromium'
);
process.exit(1);
}
throw e;
}
const browser = await chromium.launch({ headless: true });
const page = await browser.newPage();
// Set content with file base URL for any relative resources
await page.setContent(html, {
waitUntil: 'networkidle',
baseURL: `file://${dirname(inputPath)}/`,
});
// Wait for fonts to load
await page.evaluate(() => document.fonts.ready);
// Generate PDF
const pdfBuffer = await page.pdf({
format: format,
printBackground: true,
margin: {
top: '0.6in',
right: '0.6in',
bottom: '0.6in',
left: '0.6in',
},
preferCSSPageSize: false,
});
// Write PDF
const { writeFile } = await import('fs/promises');
await writeFile(outputPath, pdfBuffer);
// Count pages (approximate from PDF structure)
const pdfString = pdfBuffer.toString('latin1');
const pageCount = (pdfString.match(/\/Type\s*\/Page[^s]/g) || []).length;
await browser.close();
console.log(`✅ PDF generated: ${outputPath}`);
console.log(`📊 Pages: ${pageCount}`);
console.log(`📦 Size: ${(pdfBuffer.length / 1024).toFixed(1)} KB`);
return { outputPath, pageCount, size: pdfBuffer.length };
}
generatePDF().catch((err) => {
console.error('❌ PDF generation failed:', err.message);
process.exit(1);
});