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(); // 主图尺寸 800x800 await page.setViewport({ width: 800, height: 800 }); const mainImageDir = 'G:\\stonelan-workspace\\membank\\产品\\飞牛 NAS\\fyd-亚克力机箱 - 单\\主图'; const htmlFiles = fs.readdirSync(mainImageDir) .filter(file => file.endsWith('.html') && file.includes('主图')); console.log(`📁 找到 ${htmlFiles.length} 个主图 HTML 文件需要转换\n`); for (const htmlFile of htmlFiles) { try { const htmlPath = path.join(mainImageDir, htmlFile); const pngFile = htmlFile.replace('.html', '.png'); const pngPath = path.join(mainImageDir, pngFile); console.log(`📄 正在转换: ${htmlFile}`); const fileUrl = `file:///${htmlPath.replace(/\\/g, '/')}`; await page.goto(fileUrl, { waitUntil: 'networkidle0', timeout: 10000 }); await page.screenshot({ path: pngPath, fullPage: false, type: 'png', clip: { x: 0, y: 0, width: 800, height: 800 } }); 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); });