Skip to content

Commit b5eca3c

Browse files
committed
add pdf export helper
1 parent 1166a46 commit b5eca3c

1 file changed

Lines changed: 37 additions & 0 deletions

File tree

‎scripts/export-pdf.ts‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { execFileSync } from 'child_process';
2+
import { existsSync, statSync } from 'fs';
3+
import { join } from 'path';
4+
5+
const htmlPath = join(process.cwd(), 'prospectus.html');
6+
const pdfPath = join(process.cwd(), 'prospectus.pdf');
7+
const fileUrl = `file:///${htmlPath.replace(/\\/g, '/')}`;
8+
9+
const edgePath = 'C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedge.exe';
10+
const chromePath = 'C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe';
11+
12+
const browserPath = existsSync(edgePath) ? edgePath : chromePath;
13+
14+
console.log(`Using browser: ${browserPath}`);
15+
console.log(`Input HTML: ${fileUrl}`);
16+
console.log(`Output PDF: ${pdfPath}`);
17+
18+
const args = [
19+
'--headless',
20+
'--disable-gpu',
21+
'--no-pdf-header-footer',
22+
'--run-all-compositor-stages-before-draw',
23+
`--print-to-pdf=${pdfPath}`,
24+
fileUrl
25+
];
26+
27+
try {
28+
execFileSync(browserPath, args, { stdio: 'inherit' });
29+
if (existsSync(pdfPath)) {
30+
const stat = statSync(pdfPath);
31+
console.log(`SUCCESS: Created ${pdfPath} (${(stat.size / 1024).toFixed(1)} KB)`);
32+
} else {
33+
console.error('ERROR: PDF file was not created');
34+
}
35+
} catch (err) {
36+
console.error('Execution error:', err);
37+
}

0 commit comments

Comments
 (0)