File size: 1,259 Bytes
8e2959f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
import puppeteer from 'puppeteer';
class ScreenshotService {
async generateScreenshot(htmlContent, options = {}) {
const browser = await puppeteer.launch({
headless: true,
args: [
'--no-sandbox',
'--disable-setuid-sandbox',
'--disable-dev-shm-usage',
'--disable-accelerated-2d-canvas',
'--no-first-run',
'--no-zygote',
'--single-process', // <- this one doesn't work in Windows
'--disable-gpu'
]
});
try {
const page = await browser.newPage();
// 设置页面视窗大小,匹配PPT的4:3比例
await page.setViewport({
width: 1000,
height: 750,
deviceScaleFactor: 2 // 高分辨率
});
// 设置HTML内容
await page.setContent(htmlContent, { waitUntil: 'networkidle0' });
// 等待页面完全加载
await page.waitForTimeout(1000);
// 截图
const screenshot = await page.screenshot({
type: 'jpeg',
quality: 85,
fullPage: false
});
return screenshot;
} finally {
await browser.close();
}
}
}
export default new ScreenshotService(); |