const puppeteer = require('puppeteer'); const path = require('path'); const fs = require('fs'); async function convertHtmlToPng() { console.log('🚀 启动 HTML 转 PNG 转换工具...\n'); const browser = await puppeteer.launch({ headless: 'new', args: ['--no-sandbox', '--disable-setuid-sandbox'] }); const page = await browser.newPage(); await page.setViewport({ width: 1000, height: 1000 }); const detailDir = 'G:\\stonelan-workspace\\membank\\产品\\Linux主机\\详情'; const htmlFiles = fs.readdirSync(detailDir) .filter(file => file.endsWith('.html')); console.log(`📁 找到 ${htmlFiles.length} 个 HTML 文件需要转换\n`); for (const htmlFile of htmlFiles) { try { const htmlPath = path.join(detailDir, htmlFile); const pngFile = htmlFile.replace('.html', '.png'); const pngPath = path.join(detailDir, pngFile); console.log(`📄 正在转换: ${htmlFile}`); const fileUrl = `file:///${htmlPath.replace(/\\/g, '/')}`; await page.goto(fileUrl, { waitUntil: 'networkidle0', timeout: 10000 }); await page.screenshot({ path: pngPath, fullPage: true, type: 'png' }); console.log(`✅ 已生成: ${pngFile}\n`); } catch (error) { console.error(`❌ 转换失败 ${htmlFile}: ${error.message}\n`); } } await browser.close(); console.log('✨ 所有转换任务已完成!'); } convertHtmlToPng().catch(error => { console.error('💥 转换过程出错:', error); process.exit(1); });