diff --git a/.gitea/workflows/deploy.yml b/.gitea/workflows/deploy.yml index b3c5919..c4805e1 100644 --- a/.gitea/workflows/deploy.yml +++ b/.gitea/workflows/deploy.yml @@ -12,7 +12,7 @@ jobs: - name: Setup Environment run: | sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories - apk add --no-cache docker-cli + apk add --no-cache docker-cli curl - name: Checkout run: | @@ -37,5 +37,17 @@ jobs: -p 8080:80 \ resume-site:latest + - name: Verify Deployment (页面 + PDF) + run: | + sleep 3 + curl -fsS http://127.0.0.1:8080/ > /dev/null + for f in private-dev private-support soe-dev soe-support; do + ct=$(curl -fsS -o /dev/null -w '%{content_type}' http://127.0.0.1:8080/pdf/$f.pdf) + case "$ct" in + application/pdf*) echo "PDF OK: $f ($ct)" ;; + *) echo "PDF BAD: $f (content-type: $ct)"; exit 1 ;; + esac + done + - name: Cleanup Dangling Images run: docker image prune -f diff --git a/.gitignore b/.gitignore index a010f36..2f510f6 100644 --- a/.gitignore +++ b/.gitignore @@ -12,8 +12,8 @@ blob-report/ resume_output.jpg dist/ -# Fonts (downloaded/generated) -fonts/ +# Fonts (downloaded/generated at root; assets/fonts/ 字库入库) +/fonts/ # OS files .DS_Store diff --git a/Dockerfile b/Dockerfile index 907581b..2eeb9e3 100644 --- a/Dockerfile +++ b/Dockerfile @@ -14,14 +14,30 @@ RUN pnpm install --frozen-lockfile COPY . . RUN node scripts/build.mjs && node scripts/check.mjs -# ---- 运行阶段:Nginx 托管 dist/ ---- +# ---- PDF 阶段(仅构建期中间依赖:Chromium + 微软雅黑,产出 dist/pdf/)---- +FROM node:22-alpine AS pdf +RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories \ + && apk add --no-cache chromium fontconfig +ENV PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD=1 \ + CHROMIUM_PATH=/usr/bin/chromium +# 微软雅黑字库(仅本阶段使用,不进入最终镜像) +COPY assets/fonts/ /usr/share/fonts/yahei/ +RUN fc-cache -f +WORKDIR /app +COPY package.json ./ +COPY --from=build /app/node_modules ./node_modules +COPY --from=build /app/dist ./dist +COPY scripts/ scripts/ +RUN node scripts/pdf.mjs + +# ---- 运行阶段:Nginx 托管 dist/(干净镜像:无 Chromium/字体/Node) ---- FROM nginx:alpine # 复制 Nginx 配置 COPY nginx.conf /etc/nginx/conf.d/default.conf -# 复制构建产物(HTML + txt + css + img) -COPY --from=build /app/dist/ /usr/share/nginx/html/ +# 复制构建产物(HTML + txt + css + img + pdf) +COPY --from=pdf /app/dist/ /usr/share/nginx/html/ # 暴露端口 EXPOSE 80 diff --git a/README.md b/README.md index e66d484..c3d1ce0 100644 --- a/README.md +++ b/README.md @@ -49,6 +49,7 @@ resume/ | 国企 · 技术支持/交付实施 | http://47.121.181.112:8080/soe/support/ | | 纯文本选择页 | http://47.121.181.112:8080/txt | | 纯文本简历(4 份) | http://47.121.181.112:8080/txt/private/dev 等 | +| PDF 简历(4 份,构建期自动生成) | http://47.121.181.112:8080/pdf/private-dev.pdf 等 | 旧地址 301 兼容(面试官已收到的链接不失效):`/resume` → `/private/dev/`,`/soe` → `/soe/dev/`,`/txt/resume`、`/txt/soe`、`/other/` 等同理。 @@ -65,8 +66,9 @@ resume/ # 2. 本地构建 + 自检 + 预览 pnpm install # 首次 pnpm run serve # 构建并在 http://localhost:4173/ 预览 +pnpm run pdf # 生成 dist/pdf/ 下 4 份 PDF(首次需 npx playwright install chromium) -# 3. 提交推送 main,Gitea Actions 自动构建镜像并部署 +# 3. 提交推送 main,Gitea Actions 自动构建镜像并部署(PDF 在构建期生成并随站点发布) ``` 新增岗位/企业维度:在 `data/roles/` 或 `data/companies/` 加文件,在 `data/variants/` 加对应清单(声明板块顺序),重新构建即可;导航页、路由、纯文本版全部自动生成。 @@ -77,6 +79,14 @@ pnpm run serve # 构建并在 http://localhost:4173/ 预览 - 字段归属:事实与默认措辞 → `base.yaml`/`experiences/`;展示方式 → `companies/`;求职意向/技能 → `roles/`;措辞的岗位/组合级差异 → `roles/` 或 `variants/*-*.yaml` 的 `overrides`(以 `experiences..bullets` 等键覆盖); - `pnpm run check` 会对跨层同键(改一层被另一层覆盖的风险)和 override 数量给出警告。 +## PDF 生成 + +- PDF 在**构建期**由 Docker 中间阶段(Chromium + 微软雅黑字库 `assets/fonts/`)渲染 `dist/pdf/<企业>-<岗位>.pdf`,随站点发布到 `/pdf/*.pdf`;**运行容器是干净的 nginx 静态镜像**(无 Chromium/字体/Node,不在服务时渲染)。 +- 字体使用微软雅黑(msyh.ttc / msyhbd.ttc),构建阶段自检 `fc-match` 必须命中字库,否则构建失败。 +- 导航页卡片与简历页页脚提供「下载 PDF」入口;PDF 与浏览器打印一致(A4、微软雅黑、蓝色底色、条目不跨页、无页脚/悬浮按钮)。 +- 页数目标写在变体清单 `pdf.pages`(私企 1 页 / 国企 2 页),构建时迭代求解缩放比例收进目标页数并尽量铺满;页边距为 @page 几何(左右 57px、上 55px、下 70px,与 v19/soe-v3 参考稿一致)。 +- PDF 生成失败 = 流水线失败 = 不部署;部署后 CI 会 curl 校验 4 条 PDF 链接的 200 与 `application/pdf`。 + ## 部署方式 推送到 `main` 分支后,Gitea Actions 自动执行: diff --git a/assets/fonts/msyh.ttc b/assets/fonts/msyh.ttc new file mode 100644 index 0000000..ea174b2 Binary files /dev/null and b/assets/fonts/msyh.ttc differ diff --git a/assets/fonts/msyhbd.ttc b/assets/fonts/msyhbd.ttc new file mode 100644 index 0000000..ac4e4a6 Binary files /dev/null and b/assets/fonts/msyhbd.ttc differ diff --git a/css/base.css b/css/base.css index 8be1da3..7bd3208 100644 --- a/css/base.css +++ b/css/base.css @@ -442,14 +442,27 @@ body { margin-left: auto; } -/* Print:保留底色(浏览器需开启"背景图形"或由 print-color-adjust 保证)、条目不跨页截断、隐藏页脚与悬浮入口 */ +/* Print:保留底色、条目不跨页截断、隐藏页脚与悬浮入口 + 页边距参考 v19/soe-v3 简历:左右 57px、上 55px、下 70px(@page 几何,不随缩放变化) */ @media print { + @page { + size: A4; + margin: 55px 57px 70px 57px; + } + * { -webkit-print-color-adjust: exact !important; print-color-adjust: exact !important; color-adjust: exact !important; } + /* 内容流宽度 = 纸面内容区(680px),由 PDF 缩放收进目标页数 */ + .page { + width: 100%; + min-height: 0; + padding: 0; + } + .section-title, .edu-header, .exp-header, diff --git a/data/variants/private-dev.yaml b/data/variants/private-dev.yaml index eb69f84..88e0d45 100644 --- a/data/variants/private-dev.yaml +++ b/data/variants/private-dev.yaml @@ -13,5 +13,9 @@ nav: # 板块清单与顺序(基本信息 header 恒在首位,无需声明) sections: [education, honors, internships, projects, skills] +# PDF 目标页数(构建期自适应缩放收进该页数) +pdf: + pages: 1 + # 组合级覆盖(仅放确需单独措辞的内容;默认应共享维度层) overrides: {} diff --git a/data/variants/private-support.yaml b/data/variants/private-support.yaml index a77b1db..4c34919 100644 --- a/data/variants/private-support.yaml +++ b/data/variants/private-support.yaml @@ -11,4 +11,8 @@ nav: sections: [education, honors, internships, projects, skills] +# PDF 目标页数(构建期自适应缩放收进该页数) +pdf: + pages: 1 + overrides: {} diff --git a/data/variants/soe-dev.yaml b/data/variants/soe-dev.yaml index 8675301..bd76a87 100644 --- a/data/variants/soe-dev.yaml +++ b/data/variants/soe-dev.yaml @@ -11,6 +11,10 @@ nav: sections: [education, honors, internships, campus, projects, certificates, studentWork, socialPractice, skills, selfEval] +# PDF 目标页数(国企版两页双面排版) +pdf: + pages: 2 + # 国企口径措辞:与私企版共享事实(experiences/),此处覆盖技术向表述为业务/价值向表述 overrides: experiences: diff --git a/data/variants/soe-support.yaml b/data/variants/soe-support.yaml index 4ef36ec..8d7a9f7 100644 --- a/data/variants/soe-support.yaml +++ b/data/variants/soe-support.yaml @@ -13,4 +13,8 @@ nav: sections: [education, honors, internships, campus, projects, certificates, studentWork, socialPractice, skills, selfEval] +# PDF 目标页数(国企版两页双面排版) +pdf: + pages: 2 + overrides: {} diff --git a/package.json b/package.json index 26ae8a8..601794d 100644 --- a/package.json +++ b/package.json @@ -5,6 +5,7 @@ "scripts": { "build": "node scripts/build.mjs", "check": "node scripts/check.mjs", + "pdf": "node scripts/build.mjs && node scripts/pdf.mjs", "serve": "node scripts/build.mjs && node scripts/serve.mjs" }, "dependencies": { diff --git a/scripts/build.mjs b/scripts/build.mjs index 17766d0..3afbfa8 100644 --- a/scripts/build.mjs +++ b/scripts/build.mjs @@ -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, }); diff --git a/scripts/lib.mjs b/scripts/lib.mjs index 3678cac..b4643ad 100644 --- a/scripts/lib.mjs +++ b/scripts/lib.mjs @@ -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 }, diff --git a/scripts/pdf.mjs b/scripts/pdf.mjs new file mode 100644 index 0000000..5e6ca01 --- /dev/null +++ b/scripts/pdf.mjs @@ -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/`); diff --git a/scripts/serve.mjs b/scripts/serve.mjs index 212b4f4..93aaf85 100644 --- a/scripts/serve.mjs +++ b/scripts/serve.mjs @@ -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/)`)); +} diff --git a/templates/nav.html.hbs b/templates/nav.html.hbs index bf64d36..01203d9 100644 --- a/templates/nav.html.hbs +++ b/templates/nav.html.hbs @@ -23,6 +23,7 @@
{{nav.desc}}
diff --git a/templates/page.html.hbs b/templates/page.html.hbs index 1ba1456..6a652c8 100644 --- a/templates/page.html.hbs +++ b/templates/page.html.hbs @@ -20,6 +20,7 @@ {{{bodyHtml}}}