-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgenerate-tailored-pdf.mjs
More file actions
386 lines (322 loc) · 13.1 KB
/
Copy pathgenerate-tailored-pdf.mjs
File metadata and controls
386 lines (322 loc) · 13.1 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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/**
* 根据 JD 生成定制化 PDF 简历
*
* 用法:
* node generate-tailored-pdf.mjs --jd "JD文本或文件路径" --output "output/xxx.pdf"
* node generate-tailored-pdf.mjs --keywords "Go,微服务,K8s,分布式" --output "output/xxx.pdf"
*/
import { readFileSync, writeFileSync } from 'fs';
import { execSync } from 'child_process';
import { resolve } from 'path';
import { parseArgs } from 'util';
const { values: args } = parseArgs({
options: {
jd: { type: 'string' },
keywords: { type: 'string' },
output: { type: 'string' },
company: { type: 'string' },
role: { type: 'string' },
format: { type: 'string', default: 'a4' },
}
});
// ============================================================
// Step 1: 读取源数据
// ============================================================
function loadSourceData() {
const cv = readFileSync('cv.md', 'utf-8');
const profileRaw = readFileSync('config/profile.yml', 'utf-8');
// 简单解析 YAML(只取需要的字段)
const name = (profileRaw.match(/full_name:\s*"([^"]+)"/) || [null, ''])[1];
const email = (profileRaw.match(/email:\s*"([^"]+)"/) || [null, ''])[1];
const phone = (profileRaw.match(/phone:\s*"([^"]+)"/) || [null, ''])[1];
const wechat = (profileRaw.match(/wechat:\s*"([^"]+)"/) || [null, ''])[1];
const location = (profileRaw.match(/city:\s*"([^"]+)"/) || [null, ''])[1];
const github = (profileRaw.match(/github:\s*"([^"]+)"/) || [null, ''])[1];
return { cv, name, email, phone, wechat, location, github };
}
// ============================================================
// Step 2: 从 JD 提取关键词
// ============================================================
function extractKeywords(jdText) {
if (!jdText) return [];
const text = jdText.toLowerCase();
const keywords = [];
const techMap = [
'Go', 'Golang', 'Gin', 'GORM', 'gRPC', 'GraphQL', 'Kafka', 'RabbitMQ',
'Redis', 'MySQL', 'PostgreSQL', 'MongoDB', 'ElasticSearch',
'Docker', 'Kubernetes', 'K8s', 'Docker Compose', 'Helm',
'React', 'Vue', 'TypeScript', 'JavaScript',
'Python', 'Java', 'C++', 'Rust', 'Node.js',
'微服务', '分布式', '高并发', 'DDD', '幂等', '分布式锁',
'RAG', 'Agent', 'LangChain', 'LLM', '大模型', 'AI',
'Nacos', 'Sentinel', 'Dubbo',
'Jenkins', 'GitLab CI', 'GitHub Actions',
'Prometheus', 'Grafana', 'Jaeger',
'Camunda', '工作流', '流程编排',
'多租户', '权限管理', 'SaaS',
];
for (const kw of techMap) {
if (text.includes(kw.toLowerCase())) {
keywords.push(kw);
}
}
return [...new Set(keywords)].slice(0, 15);
}
// ============================================================
// Step 3: 填充模板占位符
// ============================================================
function fillTemplate(data, jdKeywords, company, role) {
let template = readFileSync('templates/cv-template.html', 'utf-8');
const { name, email, phone, wechat, location, github, cv } = data;
// 基础占位符
template = template.replace(/{{LANG}}/g, 'zh-CN');
template = template.replace(/{{PAGE_WIDTH}}/g, '210mm');
template = template.replace(/{{NAME}}/g, name);
template = template.replace(/{{PHONE}}/g, phone);
template = template.replace(/{{EMAIL}}/g, email);
template = template.replace(/{{LOCATION}}/g, location);
// 微信行
const wechatRow = wechat
? `\n <span class="separator">|</span>\n <span>微信: ${wechat}</span>`
: '';
template = template.replace(/{{WECHAT_ROW}}/g, wechatRow);
// GitHub / Blog
template = template.replace(/{{GITHUB_URL}}/g, github ? `https://${github}` : '#');
template = template.replace(/{{GITHUB_DISPLAY}}/g, github || '');
template = template.replace(/{{BLOG_URL}}/g, '#');
template = template.replace(/{{BLOG_DISPLAY}}/g, '');
// 段落标题
template = template.replace(/{{SECTION_SUMMARY}}/g, '职业概述');
template = template.replace(/{{SECTION_COMPETENCIES}}/g, '核心能力');
template = template.replace(/{{SECTION_EXPERIENCE}}/g, '工作经历');
template = template.replace(/{{SECTION_PROJECTS}}/g, '项目经历');
template = template.replace(/{{SECTION_EDUCATION}}/g, '教育背景');
template = template.replace(/{{SECTION_CERTIFICATIONS}}/g, '证书');
template = template.replace(/{{SECTION_SKILLS}}/g, '技能');
// 职业概述(从 cv.md 提取并注入 JD 关键词)
const summary = generateSummary(cv, jdKeywords, company);
template = template.replace(/{{SUMMARY_TEXT}}/g, summary);
// 核心能力
const competencies = generateCompetencies(cv, jdKeywords);
template = template.replace(/{{COMPETENCIES}}/g, competencies);
// 工作经历(从 cv.md 提取)
const experience = generateExperience(cv);
template = template.replace(/{{EXPERIENCE}}/g, experience);
// 项目经历
const projects = generateProjects(cv);
template = template.replace(/{{PROJECTS}}/g, projects);
// 教育背景
const education = generateEducation(cv);
template = template.replace(/{{EDUCATION}}/g, education);
// 证书
template = template.replace(/{{CERTIFICATIONS}}/g, '');
// 技能
const skills = generateSkills(cv, jdKeywords);
template = template.replace(/{{SKILLS}}/g, skills);
return template;
}
function generateSummary(cv, keywords, company) {
// 从 cv.md 提取职业概述段
const match = cv.match(/## 职业概述\s*\n+([\s\S]*?)(?=\n##|\n\*\*)/);
let summary = match ? match[1].trim() : '';
// 如果有 JD 关键词,补充一句
if (keywords.length > 0 && company) {
const top3 = keywords.slice(0, 3).join('、');
summary += ` 正积极向 AI 应用开发与 ${top3} 方向拓展。`;
}
return summary;
}
function generateCompetencies(cv, keywords) {
const tags = [];
// 从技能和 JD 关键词生成
const defaultTags = [
'Go / Golang', '微服务架构', 'Kubernetes / Docker', 'Kafka 消息系统',
'分布式系统设计', 'MySQL / PostgreSQL', 'Redis', 'gRPC', '流程编排',
'Gin / GORM', 'Jaeger 链路追踪', '私有化部署交付'
];
for (const tag of defaultTags) {
// 如果 JD 有关键词匹配,优先放前面
const matched = keywords.some(kw => tag.toLowerCase().includes(kw.toLowerCase()));
if (matched) {
tags.unshift(`<span class="competency-tag">${tag}</span>`);
} else {
tags.push(`<span class="competency-tag">${tag}</span>`);
}
}
return tags.join('\n ');
}
function generateExperience(cv) {
// 工作经历:简短版 — 公司、岗位、时间、一句话概括
const jobs = extractBriefWorkExperience(cv);
let html = '';
for (const job of jobs) {
html += ` <div class="job">
<div class="job-header">
<span class="job-company">${job.company}</span>
<span class="job-period">${job.period}</span>
</div>
<div class="job-role">${job.role}</div>
<div class="job-desc" style="font-size:10px;color:#555;margin-top:3px;">${job.summary}</div>
</div>\n`;
}
return html;
}
function extractBriefWorkExperience(cv) {
const jobs = [];
const companyBlocks = cv.split(/(?=### [^\n]+--)/);
for (const block of companyBlocks) {
const nameMatch = block.match(/###\s+(.+?)\s+--/);
if (!nameMatch) continue;
const company = nameMatch[1].trim();
const roleMatch = block.match(/\*\*(.+?)\*\*\s*\|\s*(.+)/);
const role = roleMatch ? roleMatch[1].trim() : '';
const period = roleMatch ? roleMatch[2].trim() : '';
// 提取第一句概括(紧接着角色那行的下一行非空文本)
const lines = block.split('\n');
let summary = '';
for (let i = 0; i < lines.length; i++) {
if (lines[i].includes(role) && lines[i].includes(period)) {
for (let j = i + 1; j < Math.min(i + 5, lines.length); j++) {
const line = lines[j].trim();
if (line && !line.startsWith('#') && !line.startsWith('*') && !line.startsWith('-')) {
summary = line;
break;
}
}
break;
}
}
if (company && role && period) {
jobs.push({ company, role, period, summary });
}
}
return jobs;
}
function generateProjects(cv) {
const projectsSection = cv.match(/## 项目经历([\s\S]*?)(?=## 教育|$)/);
if (!projectsSection) return '<p>暂无</p>';
const text = projectsSection[1];
let html = '';
// 按 "### " 开头分割项目
const projectBlocks = text.split(/\n(?=### )/);
for (const block of projectBlocks) {
if (!block.trim()) continue;
const titleMatch = block.match(/###\s+(.+)/);
if (!titleMatch) continue;
const title = titleMatch[1].trim();
// 提取技术栈行: **日期 | 技术栈**
const techLineMatch = block.match(/\*\*(.+?)\*\*/);
let techLine = techLineMatch ? techLineMatch[1].trim() : '';
// 提取角色
const roleMatch = block.match(/\*\*角色:?\*\*\s*(.+)/);
const role = roleMatch ? roleMatch[1].trim() : '';
// 提取背景
const bgMatch = block.match(/\*\*背景:?\*\*\s*(.+)/);
const background = bgMatch ? bgMatch[1].trim() : '';
// 提取我的工作
let workContent = '';
const workSection = block.match(/\*\*我的工作:?\*\*\s*([\s\S]*?)(?=\*\*产出|\*\*服务|\*\*其他|---|$)/);
if (workSection) {
const points = workSection[1].matchAll(/^[-*]\s+(.+)$/gm);
const pointList = [];
for (const p of points) pointList.push(p[1].trim());
workContent = pointList.join('<br>');
}
// 提取产出
const outputMatch = block.match(/\*\*产出:?\*\*\s*(.+)/);
const output = outputMatch ? outputMatch[1].trim() : '';
// 提取服务客户
const clientMatch = block.match(/\*\*服务客户:?\*\*\s*(.+)/);
const clients = clientMatch ? clientMatch[1].trim() : '';
// 提取其他模块
const otherMatch = block.match(/\*\*其他参与模块:?\*\*\s*(.+)/);
const others = otherMatch ? otherMatch[1].trim() : '';
html += ` <div class="project">
<span class="project-title">${title}</span>
<div class="project-tech">${techLine}</div>`;
if (role) {
html += `\n <div class="project-meta"><strong>角色:</strong>${role}</div>`;
}
if (background) {
html += `\n <div class="project-meta"><strong>背景:</strong>${background}</div>`;
}
if (workContent) {
html += `\n <div class="project-desc"><strong>我的工作:</strong><br>${workContent}</div>`;
}
if (output) {
html += `\n <div class="project-meta"><strong>产出:</strong>${output}</div>`;
}
if (clients) {
html += `\n <div class="project-meta"><strong>服务客户:</strong>${clients}</div>`;
}
if (others) {
html += `\n <div class="project-meta"><strong>其他:</strong>${others}</div>`;
}
html += `\n </div>\n`;
}
return html;
}
function generateEducation(cv) {
const eduMatch = cv.match(/[-*]\s+(.+大学.+)\s*[((](.+)[))]/);
if (!eduMatch) return '<p>见简历</p>';
return ` <div class="edu-item">
<div class="edu-header">
<span class="edu-title">本科 · ${eduMatch[2]}</span>
<span class="edu-org">${eduMatch[1]}</span>
<span class="edu-year">2009 - 2013</span>
</div>
</div>`;
}
function generateSkills(cv, keywords) {
const skillsSection = cv.match(/## 技能([\s\S]*?)$/);
if (!skillsSection) return '';
const text = skillsSection[1];
let html = '<div class="skills-grid">\n';
const skillLines = text.matchAll(/[-*]\s+\*\*(.+?):\*\*\s*(.+)/g);
for (const m of skillLines) {
html += ` <span class="skill-item"><span class="skill-category">${m[1]}:</span> ${m[2]}</span>\n`;
}
html += ' </div>';
return html;
}
// ============================================================
// Step 4: 主流程
// ============================================================
async function main() {
console.error('📋 生成定制化 PDF 简历...\n');
const data = loadSourceData();
console.error(` ✅ 读取 cv.md + profile.yml`);
// JD 关键词
let jdKeywords = [];
if (args.keywords) {
jdKeywords = args.keywords.split(',').map(k => k.trim());
} else if (args.jd) {
const jdText = readFileSync(args.jd, 'utf-8');
jdKeywords = extractKeywords(jdText);
}
if (jdKeywords.length > 0) {
console.error(` ✅ 提取 JD 关键词: ${jdKeywords.join(', ')}`);
} else {
console.error(` ⚠️ 未提供 JD 关键词,生成通用简历`);
}
// 生成 HTML
const company = args.company || 'target';
const role = args.role || 'backend';
const html = fillTemplate(data, jdKeywords, company, role);
const tmpHtml = `/tmp/cv-${data.name.replace(/\s/g, '-')}-${company}.html`;
writeFileSync(tmpHtml, html, 'utf-8');
console.error(` ✅ 生成 HTML: ${tmpHtml}`);
// 生成 PDF
const output = args.output || `output/cv-${data.name.replace(/\s/g, '-')}-${company}.pdf`;
const format = args.format || 'a4';
console.error(` ✅ 生成 PDF...`);
execSync(`node generate-pdf.mjs "${tmpHtml}" "${output}" --format=${format}`, {
stdio: 'inherit',
cwd: process.cwd()
});
console.error(`\n📁 PDF 已保存: ${output}`);
}
main().catch(e => {
console.error(`❌ 错误: ${e.message}`);
process.exit(1);
});