temp
This commit is contained in:
		
							
								
								
									
										16
									
								
								scripts/copy.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								scripts/copy.ts
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,16 @@
 | 
			
		||||
import * as fs from 'fs';
 | 
			
		||||
import * as path from 'path';
 | 
			
		||||
 | 
			
		||||
const targetDir = path.join('.vitepress', 'dist', 'pdf');
 | 
			
		||||
const sourceFile = path.join('pdf', 'book.pdf');
 | 
			
		||||
const targetFile = path.join(targetDir, 'book.pdf');
 | 
			
		||||
 | 
			
		||||
// 创建目标文件夹(如果不存在)
 | 
			
		||||
if (!fs.existsSync(targetDir)) {
 | 
			
		||||
  fs.mkdirSync(targetDir, { recursive: true });
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 复制文件
 | 
			
		||||
fs.copyFileSync(sourceFile, targetFile);
 | 
			
		||||
 | 
			
		||||
console.log('PDF 已复制到 .vitepress/dist/pdf/book.pdf');
 | 
			
		||||
							
								
								
									
										89
									
								
								scripts/export-pdf.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										89
									
								
								scripts/export-pdf.ts
									
									
									
									
									
										Normal 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();
 | 
			
		||||
							
								
								
									
										41
									
								
								scripts/picture.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										41
									
								
								scripts/picture.ts
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,41 @@
 | 
			
		||||
import fs from 'node:fs';
 | 
			
		||||
import path from 'node:path';
 | 
			
		||||
import sharp from 'sharp';
 | 
			
		||||
 | 
			
		||||
const root = path.join(process.cwd(), 'public','assets', 'daily');
 | 
			
		||||
 | 
			
		||||
// 获取 root 目录下的所有图片,然后把图片的大小调整小一点,在 500k-1M 之间,放回原名。
 | 
			
		||||
const zipPicture = async (root: string) => {
 | 
			
		||||
  const files = fs.readdirSync(root);
 | 
			
		||||
  for (const file of files) {
 | 
			
		||||
    const filePath = path.join(root, file);
 | 
			
		||||
    const stat = fs.statSync(filePath);
 | 
			
		||||
    if (stat.isFile() && /\.(jpg|jpeg|png)$/i.test(file)) {
 | 
			
		||||
      let buffer = fs.readFileSync(filePath);
 | 
			
		||||
      let size = buffer.length;
 | 
			
		||||
      if (size > 1024 * 1024) { // 超过1M才压缩
 | 
			
		||||
        let quality = 80;
 | 
			
		||||
        let compressedBuffer;
 | 
			
		||||
        do {
 | 
			
		||||
          compressedBuffer = await sharp(buffer)
 | 
			
		||||
            .jpeg({ quality })
 | 
			
		||||
            .toBuffer();
 | 
			
		||||
          size = compressedBuffer.length;
 | 
			
		||||
          quality -= 10;
 | 
			
		||||
        } while (size > 1024 * 1024 && quality > 30);
 | 
			
		||||
 | 
			
		||||
        if (size < 500 * 1024) {
 | 
			
		||||
          // 如果压缩后小于500k,适当提高质量
 | 
			
		||||
          quality += 5;
 | 
			
		||||
          compressedBuffer = await sharp(buffer)
 | 
			
		||||
            .jpeg({ quality })
 | 
			
		||||
            .toBuffer();
 | 
			
		||||
        }
 | 
			
		||||
        fs.writeFileSync(filePath, compressedBuffer);
 | 
			
		||||
        console.log(`${file} 压缩后大小: ${(compressedBuffer.length / 1024).toFixed(2)} KB`);
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
zipPicture(root);
 | 
			
		||||
		Reference in New Issue
	
	Block a user