Files
membank/.workbuddy/skills/image-tools/scripts/convert-html-to-png.js

60 lines
1.9 KiB
JavaScript

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: 750, height: 1000 });
const detailDir = 'G:\\stonelan-workspace\\membank\\产品\\飞牛 NAS\\fyd-亚克力机箱 - 单\\详情';
const htmlFiles = fs.readdirSync(detailDir)
.filter(file => file.endsWith('.html') && file.includes('亚克力机箱-单'))
.filter(file => !file.includes('配置清单.html') || file.includes('配置清单-'));
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);
});