forked from career-ops-hq/career-ops
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate-pdf.mjs
More file actions
342 lines (297 loc) · 12.2 KB
/
Copy pathgenerate-pdf.mjs
File metadata and controls
342 lines (297 loc) · 12.2 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#!/usr/bin/env node
/**
* generate-pdf.mjs — HTML → PDF via Playwright
*
* Usage:
* node career-ops/generate-pdf.mjs <input.html> <output.pdf> [--format=letter|a4]
*
* Requires: @playwright/test (or playwright) installed.
* Uses Chromium headless to render the HTML and produce a clean, ATS-parseable PDF.
*/
import { chromium } from 'playwright';
import { resolve, dirname, relative, isAbsolute } from 'path';
import { readFile } from 'fs/promises';
import { mkdirSync } from 'fs';
import { fileURLToPath, pathToFileURL } from 'url';
const __dirname = dirname(fileURLToPath(import.meta.url));
// Ensure output directory exists (fresh setup)
mkdirSync(resolve(__dirname, 'output'), { recursive: true });
/**
* Normalize text for ATS compatibility by converting problematic Unicode.
*
* ATS parsers and legacy systems often fail on em-dashes, smart quotes,
* zero-width characters, and non-breaking spaces. These cause mojibake,
* parsing errors, or display issues. See issue #1.
*
* Only touches body text — preserves CSS, JS, tag attributes, and URLs.
* Returns { html, replacements } so the caller can log what was changed.
*/
function normalizeTextForATS(html) {
const replacements = {};
const bump = (key, n) => { replacements[key] = (replacements[key] || 0) + n; };
const masks = [];
const masked = html.replace(
/<(style|script)\b[^>]*>[\s\S]*?<\/\1>/gi,
(match) => {
const token = `\u0000MASK${masks.length}\u0000`;
masks.push(match);
return token;
}
);
let out = '';
let i = 0;
while (i < masked.length) {
const lt = masked.indexOf('<', i);
if (lt === -1) { out += sanitizeText(masked.slice(i)); break; }
out += sanitizeText(masked.slice(i, lt));
const gt = masked.indexOf('>', lt);
if (gt === -1) { out += masked.slice(lt); break; }
out += masked.slice(lt, gt + 1);
i = gt + 1;
}
const restored = out.replace(/\u0000MASK(\d+)\u0000/g, (_, n) => masks[Number(n)]);
return { html: restored, replacements };
function sanitizeText(text) {
if (!text) return text;
let t = text;
t = t.replace(/\u2014/g, () => { bump('em-dash', 1); return '-'; });
t = t.replace(/\u2013/g, () => { bump('en-dash', 1); return '-'; });
t = t.replace(/[\u201C\u201D\u201E\u201F]/g, () => { bump('smart-double-quote', 1); return '"'; });
t = t.replace(/[\u2018\u2019\u201A\u201B]/g, () => { bump('smart-single-quote', 1); return "'"; });
t = t.replace(/\u2026/g, () => { bump('ellipsis', 1); return '...'; });
t = t.replace(/[\u200B\u200C\u200D\u2060\uFEFF]/g, () => { bump('zero-width', 1); return ''; });
t = t.replace(/\u00A0/g, () => { bump('nbsp', 1); return ' '; });
// Arrows often stripped by PDF text extractors \u2014 replace with ASCII for ATS safety.
// Consume surrounding whitespace to avoid double-spacing in output.
t = t.replace(/\s*\u2192\s*/g, () => { bump('right-arrow', 1); return ' to '; });
t = t.replace(/\s*\u2190\s*/g, () => { bump('left-arrow', 1); return ' from '; });
t = t.replace(/\s*[\u2191\u2193]\s*/g, () => { bump('vert-arrow', 1); return ' '; });
// Middle dot and bullet glyphs garble in some extractors \u2014 replace with pipe.
t = t.replace(/\s*\u00B7\s*/g, () => { bump('middot', 1); return ' | '; });
t = t.replace(/\s*\u2022\s*/g, () => { bump('bullet', 1); return ' | '; });
// Currency symbols sometimes stripped by font-subsetted PDFs \u2014 spell out
// the unambiguous ones. \u00A5 is intentionally NOT converted: it maps to both
// Japanese Yen (JPY) and Chinese Yuan (CNY), so any spelled-out code would be
// wrong for half of users \u2014 better to leave the glyph than emit bad data.
t = t.replace(/\u20AC/g, () => { bump('euro', 1); return 'EUR '; });
t = t.replace(/\u00A3/g, () => { bump('pound', 1); return 'GBP '; });
return t;
}
}
const SECTION_ALIASES = new Map([
['summary', 'summary'],
['professional summary', 'summary'],
['competencies', 'competencies'],
['core competencies', 'competencies'],
['experience', 'experience'],
['work experience', 'experience'],
['professional experience', 'experience'],
['projects', 'projects'],
['selected projects', 'projects'],
['personal projects', 'projects'],
['education', 'education'],
['education & certifications', 'education'],
['certifications', 'certifications'],
['skills', 'skills'],
['technical skills', 'skills'],
]);
function normalizeSectionTitle(text) {
return text
.replace(/<[^>]+>/g, ' ')
.replace(/\{\{[^}]+\}\}/g, ' ')
.replace(/&/g, '&')
.replace(/[*_`~]/g, '')
.replace(/\s+/g, ' ')
.trim()
.toLowerCase();
}
function sectionKey(text) {
const normalized = normalizeSectionTitle(text);
return SECTION_ALIASES.get(normalized) ?? normalized;
}
function extractRenderedSectionOrder(html) {
const titleMatches = [...html.matchAll(/class=["'][^"']*\bsection-title\b[^"']*["'][^>]*>([\s\S]*?)<\/[^>]+>/gi)];
const sections = [];
for (const match of titleMatches) {
const text = normalizeSectionTitle(match[1]);
if (!text) continue;
sections.push({ key: sectionKey(text), title: text });
}
return sections;
}
function extractSourceSectionOrder(markdown) {
const sections = [];
for (const line of markdown.split(/\r?\n/)) {
const heading = line.match(/^\s{0,3}(#{1,6})\s+(.+?)\s*#*\s*$/);
if (!heading) continue;
const text = normalizeSectionTitle(heading[2]);
if (!text) continue;
sections.push({ key: sectionKey(text), title: text });
}
return sections;
}
function validateCvSectionOrder(html, cvMarkdown) {
const rendered = extractRenderedSectionOrder(html);
const source = extractSourceSectionOrder(cvMarkdown);
if (rendered.length < 2 || source.length < 2) return;
const sourcePositions = new Map(source.map((section, index) => [section.key, index]));
const renderedComparable = rendered.filter(section => sourcePositions.has(section.key));
if (renderedComparable.length < 2) return;
for (let i = 1; i < renderedComparable.length; i++) {
const previous = renderedComparable[i - 1];
const current = renderedComparable[i];
if (sourcePositions.get(current.key) < sourcePositions.get(previous.key)) {
const renderedOrder = renderedComparable.map(section => section.title).join(' -> ');
const sourceOrder = source
.filter(section => renderedComparable.some(renderedSection => renderedSection.key === section.key))
.map(section => section.title)
.join(' -> ');
throw new Error(`CV section order diverges from cv.md: rendered ${renderedOrder}; cv.md ${sourceOrder}`);
}
}
}
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);
// Validate format
const validFormats = ['a4', 'letter'];
if (!validFormats.includes(format)) {
console.error(`Invalid format "${format}". Use: ${validFormats.join(', ')}`);
process.exit(1);
}
console.log(`📄 Input: ${inputPath}`);
console.log(`📁 Output: ${outputPath}`);
console.log(`📏 Format: ${format.toUpperCase()}`);
let html = await readFile(inputPath, 'utf-8');
let cvMarkdown = '';
try {
cvMarkdown = await readFile(resolve(__dirname, 'cv.md'), 'utf-8');
} catch (err) {
if (err?.code !== 'ENOENT') throw err;
}
validateCvSectionOrder(html, cvMarkdown);
// Normalize text for ATS compatibility (issue #1)
const normalized = normalizeTextForATS(html);
html = normalized.html;
const totalReplacements = Object.values(normalized.replacements).reduce((a, b) => a + b, 0);
if (totalReplacements > 0) {
const breakdown = Object.entries(normalized.replacements).map(([k, v]) => `${k}=${v}`).join(', ');
console.log(`🧹 ATS normalization: ${totalReplacements} replacements (${breakdown})`);
}
return renderHtmlToPdf(html, outputPath, { format, baseDir: dirname(inputPath) });
}
/**
* Inline url('./fonts/...') references as base64 data: URLs.
*
* Chromium refuses to load file:// subresources from a setContent() page
* (the document stays at about:blank), so fonts referenced by path are
* silently dropped and PDFs fall back to system fonts. data: URLs carry
* no origin restriction, so they load from any page. See #951.
*
* Missing font files keep their original reference and log a warning.
*
* @param {string} html - HTML that may reference url('./fonts/<file>').
* @returns {Promise<string>} HTML with local font references inlined.
*/
export async function inlineLocalFonts(html) {
const FONT_REF = /url\(\s*(['"]?)\.\/fonts\/([^'")\s]+)\1\s*\)/g;
const MIME = { woff2: 'font/woff2', woff: 'font/woff', otf: 'font/otf', ttf: 'font/ttf' };
const fontsDir = resolve(__dirname, 'fonts');
const names = [...new Set([...html.matchAll(FONT_REF)].map((m) => m[2]))];
const dataUrls = new Map();
for (const name of names) {
// Containment check: ".." segments and absolute names (./fonts//etc/passwd)
// would otherwise resolve outside fonts/.
const fontPath = resolve(fontsDir, name);
const rel = relative(fontsDir, fontPath);
if (rel.startsWith('..') || isAbsolute(rel)) {
console.warn(`⚠️ Font reference escapes fonts/, keeping original reference: ${name}`);
continue;
}
try {
const buf = await readFile(fontPath);
const ext = name.slice(name.lastIndexOf('.') + 1).toLowerCase();
dataUrls.set(name, `url('data:${MIME[ext] || 'application/octet-stream'};base64,${buf.toString('base64')}')`);
} catch (err) {
if (err?.code !== 'ENOENT') throw err;
console.warn(`⚠️ Font file not found, keeping original reference: fonts/${name}`);
}
}
return html.replace(FONT_REF, (match, _quote, name) => dataUrls.get(name) || match);
}
/**
* Render an HTML string to a PDF file via headless Chromium.
*
* Local url('./fonts/...') references are inlined as data: URLs first so
* fonts render regardless of page origin (see inlineLocalFonts).
*
* @param {string} html - Full HTML document to render.
* @param {string} outputPath - Absolute path to write the PDF to.
* @param {{format?: 'a4'|'letter', baseDir?: string}} [opts]
* @returns {Promise<{outputPath: string, pageCount: number, size: number}>}
*/
export async function renderHtmlToPdf(html, outputPath, opts = {}) {
const format = opts.format || 'a4';
const baseDir = opts.baseDir || process.cwd();
mkdirSync(dirname(outputPath), { recursive: true });
html = await inlineLocalFonts(html);
const browser = await chromium.launch({ headless: true });
try {
const page = await browser.newPage();
// Set content with file base URL for any relative resources
await page.setContent(html, {
waitUntil: 'load',
baseURL: `${pathToFileURL(baseDir).href}/`,
});
// 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;
console.log(`✅ PDF generated: ${outputPath}`);
console.log(`📊 Pages: ${pageCount}`);
console.log(`📦 Size: ${(pdfBuffer.length / 1024).toFixed(1)} KB`);
return { outputPath, pageCount, size: pdfBuffer.length };
} finally {
await browser.close();
}
}
const isMain = process.argv[1] && import.meta.url === pathToFileURL(resolve(process.argv[1])).href;
if (isMain) {
generatePDF().catch((err) => {
console.error('❌ PDF generation failed:', err.message);
process.exit(1);
});
}
export { normalizeTextForATS };