This commit is contained in:
2025-10-08 00:49:47 +08:00
commit b95c4476b3
17 changed files with 3253 additions and 0 deletions

89
scripts/export-pdf.ts Normal file
View File

@@ -0,0 +1,89 @@
import puppeteer from 'puppeteer';
import fs from 'node:fs';
import path from 'node:path';
import { PDFDocument } from 'pdf-lib';
async function exportPDF(): Promise<void> {
const browser = await puppeteer.launch();
const page = await browser.newPage();
// 设置页面样式适应 PDF
await page.setViewport({ width: 1200, height: 800 });
// const baseUrl = 'http://localhost:5173'; // VitePress 开发服务器地址
const baseUrl = 'https://look-good.xiongxiao.me'; // VitePress 开发服务器生产地址
const outputPath = path.resolve(process.cwd(), 'pdf');
if (!fs.existsSync(outputPath)) {
fs.mkdirSync(outputPath);
}
// 根据 sidebar 配置,导出的路径列表
const routes: string[] = [
'/',
'/preface',
'/self-introduction',
'/book/0-1',
'/book/01',
'/book/02',
'/book/03',
'/book/0-2',
'/book/04',
'/book/05',
'/book/06',
'/book/07',
'/book/08',
'/book/09',
'/book/0-3',
'/book/10',
'/book/11',
'/book/12',
'/appendix/a',
'/appendix/b',
'/appendix/c',
'/appendix/d',
'/appendix/e'
];
const pdfFiles: string[] = [];
for (const route of routes) {
const url = baseUrl + route;
const fileName = route === '/' ? 'index' : route.slice(1).replace(/\//g, '_');
const pdfPath = path.join(outputPath, `${fileName}.pdf`);
console.log(`⏳ 正在生成: ${fileName}.pdf from ${url}`);
await page.goto(url, { waitUntil: 'networkidle0' });
await page.pdf({
path: pdfPath,
format: 'A4',
printBackground: true,
margin: {
top: '20mm',
right: '20mm',
bottom: '20mm',
left: '20mm',
},
});
pdfFiles.push(pdfPath);
console.log(`✅ 已生成: ${fileName}.pdf`);
}
await browser.close();
// 合并所有 PDF
const mergedPdf = await PDFDocument.create();
for (const pdfFile of pdfFiles) {
const pdfBytes = fs.readFileSync(pdfFile);
const pdf = await PDFDocument.load(pdfBytes);
const copiedPages = await mergedPdf.copyPages(pdf, pdf.getPageIndices());
copiedPages.forEach((page) => mergedPdf.addPage(page));
}
const mergedPdfBytes = await mergedPdf.save();
const bookPdfPath = path.join(outputPath, 'book.pdf');
fs.writeFileSync(bookPdfPath, mergedPdfBytes);
console.log(`🎉 已合并为: book.pdf`);
}
exportPDF();