Upload screenshotService.js
Browse files
backend/src/services/screenshotService.js
CHANGED
@@ -2,48 +2,118 @@ import puppeteer from 'puppeteer';
|
|
2 |
|
3 |
class ScreenshotService {
|
4 |
async generateScreenshot(htmlContent, options = {}) {
|
5 |
-
|
6 |
-
headless: true,
|
7 |
-
args: [
|
8 |
-
'--no-sandbox',
|
9 |
-
'--disable-setuid-sandbox',
|
10 |
-
'--disable-dev-shm-usage',
|
11 |
-
'--disable-accelerated-2d-canvas',
|
12 |
-
'--no-first-run',
|
13 |
-
'--no-zygote',
|
14 |
-
'--single-process', // <- this one doesn't work in Windows
|
15 |
-
'--disable-gpu'
|
16 |
-
]
|
17 |
-
});
|
18 |
|
19 |
try {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
const page = await browser.newPage();
|
|
|
21 |
|
22 |
// 设置页面视窗大小,匹配PPT的4:3比例
|
23 |
await page.setViewport({
|
24 |
width: 1000,
|
25 |
height: 750,
|
26 |
-
deviceScaleFactor:
|
27 |
});
|
28 |
|
|
|
|
|
29 |
// 设置HTML内容
|
30 |
-
await page.setContent(htmlContent, {
|
|
|
|
|
|
|
31 |
|
32 |
-
|
33 |
-
|
|
|
|
|
|
|
|
|
34 |
|
35 |
// 截图
|
36 |
const screenshot = await page.screenshot({
|
37 |
type: 'jpeg',
|
38 |
-
quality:
|
39 |
-
fullPage: false
|
|
|
40 |
});
|
41 |
|
|
|
|
|
42 |
return screenshot;
|
|
|
|
|
|
|
|
|
|
|
43 |
} finally {
|
44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
}
|
46 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
}
|
48 |
|
49 |
export default new ScreenshotService();
|
|
|
2 |
|
3 |
class ScreenshotService {
|
4 |
async generateScreenshot(htmlContent, options = {}) {
|
5 |
+
let browser = null;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
try {
|
8 |
+
console.log('Starting Puppeteer browser...');
|
9 |
+
|
10 |
+
// 更适合容器环境的Puppeteer配置
|
11 |
+
browser = await puppeteer.launch({
|
12 |
+
headless: 'new', // 使用新的headless模式
|
13 |
+
args: [
|
14 |
+
'--no-sandbox',
|
15 |
+
'--disable-setuid-sandbox',
|
16 |
+
'--disable-dev-shm-usage',
|
17 |
+
'--disable-accelerated-2d-canvas',
|
18 |
+
'--no-first-run',
|
19 |
+
'--no-zygote',
|
20 |
+
'--disable-gpu',
|
21 |
+
'--disable-background-timer-throttling',
|
22 |
+
'--disable-backgrounding-occluded-windows',
|
23 |
+
'--disable-renderer-backgrounding',
|
24 |
+
'--disable-web-security',
|
25 |
+
'--disable-features=TranslateUI',
|
26 |
+
'--disable-extensions',
|
27 |
+
'--disable-component-extensions-with-background-pages',
|
28 |
+
'--disable-default-apps',
|
29 |
+
'--mute-audio',
|
30 |
+
'--no-default-browser-check',
|
31 |
+
'--autoplay-policy=user-gesture-required',
|
32 |
+
'--disable-background-mode',
|
33 |
+
'--disable-plugins',
|
34 |
+
'--disable-translate',
|
35 |
+
'--disable-ipc-flooding-protection',
|
36 |
+
'--memory-pressure-off',
|
37 |
+
'--max_old_space_size=4096'
|
38 |
+
],
|
39 |
+
timeout: 30000, // 30秒超时
|
40 |
+
protocolTimeout: 30000
|
41 |
+
});
|
42 |
+
|
43 |
+
console.log('Browser launched successfully');
|
44 |
+
|
45 |
const page = await browser.newPage();
|
46 |
+
console.log('New page created');
|
47 |
|
48 |
// 设置页面视窗大小,匹配PPT的4:3比例
|
49 |
await page.setViewport({
|
50 |
width: 1000,
|
51 |
height: 750,
|
52 |
+
deviceScaleFactor: 1 // 降低分辨率减少内存使用
|
53 |
});
|
54 |
|
55 |
+
console.log('Viewport set');
|
56 |
+
|
57 |
// 设置HTML内容
|
58 |
+
await page.setContent(htmlContent, {
|
59 |
+
waitUntil: 'domcontentloaded', // 改为更快的加载策略
|
60 |
+
timeout: 10000
|
61 |
+
});
|
62 |
|
63 |
+
console.log('HTML content set');
|
64 |
+
|
65 |
+
// 等待渲染完成(减少等待时间)
|
66 |
+
await page.waitForTimeout(500);
|
67 |
+
|
68 |
+
console.log('Taking screenshot...');
|
69 |
|
70 |
// 截图
|
71 |
const screenshot = await page.screenshot({
|
72 |
type: 'jpeg',
|
73 |
+
quality: 80, // 降低质量减少文件大小
|
74 |
+
fullPage: false,
|
75 |
+
timeout: 10000
|
76 |
});
|
77 |
|
78 |
+
console.log('Screenshot taken successfully, size:', screenshot.length);
|
79 |
+
|
80 |
return screenshot;
|
81 |
+
} catch (error) {
|
82 |
+
console.error('Screenshot generation error:', error);
|
83 |
+
|
84 |
+
// 返回一个错误图片而不是抛出异常
|
85 |
+
return this.generateErrorImage(error.message);
|
86 |
} finally {
|
87 |
+
if (browser) {
|
88 |
+
try {
|
89 |
+
await browser.close();
|
90 |
+
console.log('Browser closed');
|
91 |
+
} catch (closeError) {
|
92 |
+
console.error('Error closing browser:', closeError);
|
93 |
+
}
|
94 |
+
}
|
95 |
}
|
96 |
}
|
97 |
+
|
98 |
+
// 生成错误提示图片
|
99 |
+
generateErrorImage(errorMessage = '截图生成失败') {
|
100 |
+
// 创建一个简单的错误图片的base64数据
|
101 |
+
const canvas = `
|
102 |
+
<svg width="1000" height="750" xmlns="http://www.w3.org/2000/svg">
|
103 |
+
<rect width="1000" height="750" fill="#f8f9fa"/>
|
104 |
+
<rect x="50" y="50" width="900" height="650" fill="white" stroke="#dee2e6" stroke-width="2"/>
|
105 |
+
<text x="500" y="350" text-anchor="middle" font-family="Arial" font-size="24" fill="#6c757d">
|
106 |
+
截图生成失败
|
107 |
+
</text>
|
108 |
+
<text x="500" y="400" text-anchor="middle" font-family="Arial" font-size="16" fill="#868e96">
|
109 |
+
${errorMessage}
|
110 |
+
</text>
|
111 |
+
</svg>
|
112 |
+
`;
|
113 |
+
|
114 |
+
// 将SVG转换为Buffer(简化版本)
|
115 |
+
return Buffer.from(canvas);
|
116 |
+
}
|
117 |
}
|
118 |
|
119 |
export default new ScreenshotService();
|