const puppeteer = require('puppeteer'); const path = require('path'); const fs = require('fs'); async function convertMainImageToPng() { console.log('🎨 启动主图渲染工具...\n'); const browser = await puppeteer.launch({ headless: 'new', args: ['--no-sandbox', '--disable-setuid-sandbox'] }); const page = await browser.newPage(); 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')); 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: true, type: 'png' }); console.log(`✅ 已生成: ${pngFile}\n`); } catch (error) { console.error(`❌ 渲染失败 ${htmlFile}: ${error.message}\n`); } } await browser.close(); console.log('✨ 所有主图渲染完成!'); } convertMainImageToPng().catch(error => { console.error('💥 渲染过程出错:', error); process.exit(1); });