CatPtain commited on
Commit
900310e
·
verified ·
1 Parent(s): b4297f5

Upload screenshotService.js

Browse files
backend/src/services/screenshotService.js CHANGED
@@ -9,7 +9,7 @@ class ScreenshotService {
9
 
10
  // 更适合容器环境的Puppeteer配置
11
  browser = await puppeteer.launch({
12
- headless: 'new', // 使用新的headless模式
13
  args: [
14
  '--no-sandbox',
15
  '--disable-setuid-sandbox',
@@ -36,7 +36,7 @@ class ScreenshotService {
36
  '--memory-pressure-off',
37
  '--max_old_space_size=4096'
38
  ],
39
- timeout: 30000, // 30秒超时
40
  protocolTimeout: 30000
41
  });
42
 
@@ -45,33 +45,56 @@ class ScreenshotService {
45
  const page = await browser.newPage();
46
  console.log('New page created');
47
 
48
- // 设置页面视窗大小,匹配PPT4: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
 
@@ -95,23 +118,24 @@ class ScreenshotService {
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
  }
 
9
 
10
  // 更适合容器环境的Puppeteer配置
11
  browser = await puppeteer.launch({
12
+ headless: 'new',
13
  args: [
14
  '--no-sandbox',
15
  '--disable-setuid-sandbox',
 
36
  '--memory-pressure-off',
37
  '--max_old_space_size=4096'
38
  ],
39
+ timeout: 30000,
40
  protocolTimeout: 30000
41
  });
42
 
 
45
  const page = await browser.newPage();
46
  console.log('New page created');
47
 
48
+ // PPT标准尺寸 (4:3 比例)
49
+ const pptWidth = 1000;
50
+ const pptHeight = 750;
51
+
52
+ // 设置页面视窗大小,添加足够的边距以避免裁切问题
53
  await page.setViewport({
54
+ width: pptWidth + 100, // 添加边距
55
+ height: pptHeight + 100, // 添加边距
56
+ deviceScaleFactor: 2 // 高分辨率
57
  });
58
 
59
  console.log('Viewport set');
60
 
61
+ // 修改HTML内容,确保PPT容器居中且尺寸精确
62
+ const modifiedHtmlContent = htmlContent.replace(
63
+ '<div class="slide-container"',
64
+ `<div class="slide-container" style="width: ${pptWidth}px; height: ${pptHeight}px; position: absolute; left: 50px; top: 50px; margin: 0; transform: none; box-shadow: 0 2px 10px rgba(0,0,0,0.1);"`
65
+ ).replace(
66
+ '.viewport-container {',
67
+ `.viewport-container {
68
+ position: relative;
69
+ width: ${pptWidth + 100}px;
70
+ height: ${pptHeight + 100}px;
71
+ background-color: transparent;
72
+ display: block;`
73
+ );
74
+
75
  // 设置HTML内容
76
+ await page.setContent(modifiedHtmlContent, {
77
+ waitUntil: 'domcontentloaded',
78
  timeout: 10000
79
  });
80
 
81
  console.log('HTML content set');
82
 
83
+ // 等待渲染完成
84
+ await page.waitForTimeout(1000);
85
 
86
  console.log('Taking screenshot...');
87
 
88
+ // 精确截取PPT区域
89
  const screenshot = await page.screenshot({
90
  type: 'jpeg',
91
+ quality: 90, // 提高质量
92
+ clip: {
93
+ x: 50,
94
+ y: 50,
95
+ width: pptWidth,
96
+ height: pptHeight
97
+ },
98
  timeout: 10000
99
  });
100
 
 
118
  }
119
  }
120
 
121
+ // 生成错误提示图片 - 也使用精确的PPT尺寸
122
  generateErrorImage(errorMessage = '截图生成失败') {
123
+ const pptWidth = 1000;
124
+ const pptHeight = 750;
125
+
126
  const canvas = `
127
+ <svg width="${pptWidth}" height="${pptHeight}" xmlns="http://www.w3.org/2000/svg">
128
+ <rect width="${pptWidth}" height="${pptHeight}" fill="#f8f9fa"/>
129
+ <rect x="50" y="50" width="${pptWidth - 100}" height="${pptHeight - 100}" fill="white" stroke="#dee2e6" stroke-width="2"/>
130
+ <text x="${pptWidth / 2}" y="${pptHeight / 2 - 20}" text-anchor="middle" font-family="Arial" font-size="24" fill="#6c757d">
131
  截图生成失败
132
  </text>
133
+ <text x="${pptWidth / 2}" y="${pptHeight / 2 + 20}" text-anchor="middle" font-family="Arial" font-size="16" fill="#868e96">
134
  ${errorMessage}
135
  </text>
136
  </svg>
137
  `;
138
 
 
139
  return Buffer.from(canvas);
140
  }
141
  }