CI 构建期自动生成 PDF:微软雅黑渲染 + 站点托管下载
Deploy Resume / deploy (push) Failing after 3m5s

- 新增 scripts/pdf.mjs:构建期 Chromium 渲染 4 份 PDF(printBackground 保留蓝色底色)
- 微软雅黑字库(msyh.ttc/msyhbd.ttc)随仓库分发,构建阶段 fc-match 自检;最终镜像保持干净 nginx(无 Chromium/字体/Node)
- Dockerfile 加 pdf 中间阶段;deploy.yml 加部署后 PDF 链接验证
- 导航卡片与简历页页脚加「下载 PDF」入口
- 排版对齐 v19/soe-v3 参考稿:@page 页边距 57/55/70px,私企 1 页饱满收口、国企 2 页宽松排版
  (迭代求解缩放比例,测量版面宽度与 page.pdf 缩放语义一致)
- 页数目标写入变体清单 pdf.pages
This commit is contained in:
2026-11-03 13:16:55 +08:00
parent 17574b35cf
commit bc3d703b04
18 changed files with 200 additions and 17 deletions
+1
View File
@@ -58,6 +58,7 @@ for (const view of views) {
selfUrl: view.selfUrl,
baseUrl: view.merged.site.baseUrl,
txtUrl: view.txtPath,
pdfUrl: view.pdfUrl,
headerHtml,
bodyHtml,
});
+2
View File
@@ -210,6 +210,8 @@ export function buildView(v, data) {
role,
path: `${variantPath}/`,
txtPath: `/txt${variantPath}`,
pdfUrl: `/pdf/${v.company}-${v.role}.pdf`,
pdfPages: (v.pdf && v.pdf.pages) || 1,
selfUrl,
theme: company.theme || v.company,
meta: { title, description },
+103
View File
@@ -0,0 +1,103 @@
// scripts/pdf.mjs —— 构建期生成 4 份简历 PDF(微软雅黑渲染)
// 仅在构建阶段运行(Docker pdf 中间阶段 / 本地 pnpm run pdf),运行容器不参与渲染
import fs from 'node:fs';
import path from 'node:path';
import { execSync } from 'node:child_process';
import { chromium } from 'playwright';
import { DIST, loadAll, buildView } from './lib.mjs';
import { createStaticServer } from './serve.mjs';
const OUT = path.join(DIST, 'pdf');
const MIN_SIZE = 20 * 1024; // PDF 有效体积下限
// ---------- 字体自检:Linux 构建容器内必须命中微软雅黑 ----------
if (process.platform === 'linux') {
let matched = '';
try {
matched = execSync('fc-match "Microsoft YaHei"', { encoding: 'utf8' }).trim();
} catch {
console.error('[pdf] fc-match 不可用(fontconfig 未安装?),中止构建');
process.exit(1);
}
console.log(`[pdf] 字体匹配:${matched}`);
if (!/msyh/i.test(matched)) {
console.error('[pdf] 未命中微软雅黑字库(assets/fonts/),中文字形将回退,中止构建');
process.exit(1);
}
}
// ---------- 视图清单 ----------
const data = loadAll();
const views = data.variants.map((v) => buildView(v, data));
// ---------- 本地静态服务 + Chromium ----------
const server = createStaticServer(DIST);
await new Promise((resolve) => server.listen(0, '127.0.0.1', resolve));
const port = server.address().port;
const browser = await chromium.launch({
executablePath: process.env.CHROMIUM_PATH || undefined,
args: ['--no-sandbox', '--disable-dev-shm-usage'],
});
fs.mkdirSync(OUT, { recursive: true });
const CONTENT_W = 680; // 纸面内容区宽(A4 793.7 - 左右边距 57×2)
const PAGE_H = 997.5; // 纸面内容区高(A4 1122.5 - 上 55 - 下 70)
try {
for (const view of views) {
const page = await browser.newPage({ viewport: { width: CONTENT_W, height: 997 } });
await page.emulateMedia({ media: 'print' });
await page.goto(`http://127.0.0.1:${port}${view.path}`, { waitUntil: 'networkidle' });
// page.pdf 的 scale < 1 时版面按 CONTENT_W/scale 宽度重排后整体缩放;
// 迭代求满足"渲染后高度 ≤ 目标页可用高度"的最大缩放(内容尽可能铺满)
const targetPages = view.pdfPages || 1;
const budget = PAGE_H * targetPages * 0.985;
let scale = 1;
const candidates = [];
for (let i = 0; i < 5; i++) {
await page.setViewportSize({ width: Math.round(CONTENT_W / scale), height: 997 });
const height = await page.evaluate(() => document.querySelector('.page').scrollHeight);
candidates.push({ scale, extent: height * scale });
const next = Math.min(1, budget / height);
if (Math.abs(next - scale) < 0.004) {
scale = next;
break;
}
scale = next;
}
const fitting = candidates.filter((c) => c.extent <= budget);
if (fitting.length) scale = Math.max(...fitting.map((c) => c.scale));
else scale = Math.min(...candidates.map((c) => c.scale));
const file = path.join(OUT, `${view.id}.pdf`);
await page.pdf({
path: file,
format: 'A4',
preferCSSPageSize: true, // 采用 @page 边距与尺寸
printBackground: true, // 保留小节蓝色底色
displayHeaderFooter: false,
scale,
});
await page.close();
const size = fs.statSync(file).size;
console.log(`[pdf] ${view.id}.pdf ${(size / 1024).toFixed(0)}KB 缩放 ${scale.toFixed(3)} 目标 ${targetPages} 页 ${view.meta.title}`);
}
} finally {
await browser.close();
server.close();
}
// ---------- 产物自检 ----------
let failed = false;
for (const view of views) {
const file = path.join(OUT, `${view.id}.pdf`);
if (!fs.existsSync(file) || fs.statSync(file).size < MIN_SIZE) {
console.error(`[pdf] 产物缺失或过小:${file}`);
failed = true;
}
}
if (failed) process.exit(1);
console.log(`[pdf] 完成:${views.length} 份 PDF → dist/pdf/`);
+16 -9
View File
@@ -1,11 +1,11 @@
// scripts/serve.mjs —— 本地预览静态服务器(模拟 nginx 路由,含旧地址 301)
// 也导出 createStaticServer() 供 scripts/pdf.mjs 在构建期复用
import http from 'node:http';
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { DIST } from './lib.mjs';
const PORT = Number(process.env.PORT || 4173);
const MIME = {
'.html': 'text/html; charset=utf-8',
'.css': 'text/css; charset=utf-8',
@@ -17,6 +17,7 @@ const MIME = {
'.gif': 'image/gif',
'.ico': 'image/x-icon',
'.txt': 'text/plain; charset=utf-8',
'.pdf': 'application/pdf',
};
const REDIRECTS = {
@@ -41,8 +42,8 @@ function send(res, file, forceType) {
res.end(fs.readFileSync(file));
}
http
.createServer((req, res) => {
export function createStaticServer(root = DIST) {
return http.createServer((req, res) => {
const p = decodeURIComponent(new URL(req.url, 'http://localhost').pathname);
if (REDIRECTS[p]) {
res.writeHead(301, { Location: REDIRECTS[p] });
@@ -53,15 +54,21 @@ http
return res.end();
}
const txtMatch = p.match(/^\/txt\/(private|soe)\/(dev|support)$/);
if (txtMatch) return send(res, path.join(DIST, 'txt', txtMatch[1], txtMatch[2], 'index.txt'));
if (p === '/txt') return send(res, path.join(DIST, 'txt', 'index.html'));
if (txtMatch) return send(res, path.join(root, 'txt', txtMatch[1], txtMatch[2], 'index.txt'));
if (p === '/txt') return send(res, path.join(root, 'txt', 'index.html'));
let file = path.join(DIST, p);
let file = path.join(root, p);
if (p.endsWith('/')) file = path.join(file, 'index.html');
else if (fs.existsSync(file) && fs.statSync(file).isDirectory()) {
res.writeHead(301, { Location: `${p}/` });
return res.end();
}
send(res, file);
})
.listen(PORT, () => console.log(`本地预览:http://localhost:${PORT}/ (dist/)`));
});
}
// 直接运行时启动预览服务
if (process.argv[1] === fileURLToPath(import.meta.url)) {
const PORT = Number(process.env.PORT || 4173);
createStaticServer().listen(PORT, () => console.log(`本地预览:http://localhost:${PORT}/ (dist/)`));
}