const puppeteer = require('puppeteer'); const path = require('path'); //const puppeteer = require('C:\\Office\\Feishu\\workplace\\spring-boot-01-helloworld\\spring-boot-01-helloworld'); (async () => { const htmlFilePath = process.argv[2]; // 临时HTML路径 const outputPath = process.argv[3]; // 图片输出路径 const browser = await puppeteer.launch({ headless: true, args: ['--no-sandbox', '--disable-setuid-sandbox'], protocolTimeout: 1200000, }); //const browser = await puppeteer.launch(); const page = await browser.newPage(); //await page.goto('file://${htmlFilePath}', { waitUntil: 'networkidle0' }); await page.goto(`file://${htmlFilePath}`, { waitUntil: 'networkidle0' }); // 等待图表渲染完成(检查Canvas元素) await page.waitForSelector('#chart', { timeout: 1200000 }); // 截图 // await page.screenshot({ // path: outputPath, // clip: { x: 0, y: 0, width: 800, height: 650 } // }); try { const base64Image = await page.screenshot({ encoding: 'base64' }); console.log(base64Image); } catch (error) { console.error('Failed to take screenshot:', error); // 可以选择重试或执行其他错误处理逻辑 //await captureScreenshot(url); // 重试截图 } finally { await browser.close(); } })();