File size: 16,068 Bytes
8e2959f 21da54e 900310e b4297f5 21da54e b4297f5 21da54e b4297f5 21da54e 8e2959f 21da54e 8e2959f 21da54e 8e2959f 21da54e 1d4c41b 21da54e 1d4c41b 21da54e 1d4c41b 21da54e 1d4c41b 21da54e 1d4c41b 21da54e 1d4c41b 21da54e 1d4c41b 21da54e 1d4c41b 21da54e 1d4c41b 21da54e 1d4c41b 21da54e b4297f5 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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 |
import puppeteer from 'puppeteer';
class ScreenshotService {
constructor() {
this.browser = null;
this.isInitialized = false;
}
async initBrowser() {
if (!this.browser) {
console.log('初始化Puppeteer浏览器...');
this.browser = await puppeteer.launch({
headless: 'new',
args: [
'--no-sandbox',
'--disable-setuid-sandbox',
'--disable-dev-shm-usage',
'--disable-accelerated-2d-canvas',
'--no-first-run',
'--no-zygote',
'--single-process',
'--disable-gpu',
'--disable-background-timer-throttling',
'--disable-backgrounding-occluded-windows',
'--disable-renderer-backgrounding',
'--disable-features=TranslateUI',
'--disable-extensions',
'--hide-scrollbars',
'--mute-audio',
'--no-default-browser-check',
'--disable-default-apps',
'--disable-background-networking',
'--disable-sync',
'--metrics-recording-only',
'--disable-domain-reliability',
'--disable-component-extensions-with-background-pages',
'--force-device-scale-factor=1',
'--enable-precise-memory-info'
]
});
console.log('Puppeteer浏览器初始化完成');
}
return this.browser;
}
async closeBrowser() {
if (this.browser) {
await this.browser.close();
this.browser = null;
console.log('Puppeteer浏览器已关闭');
}
}
async modifyHtmlForScreenshot(htmlContent, targetWidth, targetHeight) {
console.log(`开始修改HTML for截图, 目标尺寸: ${targetWidth}x${targetHeight}`);
// 创建完全针对截图优化的HTML版本
const optimizedHtml = htmlContent.replace(
/<head>/i,
`<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=${targetWidth}, height=${targetHeight}, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no, viewport-fit=cover">
<!-- 截图模式标识 -->
<meta name="screenshot-mode" content="true">
<!-- 强制精确尺寸控制 -->
<style id="screenshot-optimization">
/* 全局重置 - 完全消除边距 */
*, *::before, *::after {
margin: 0 !important;
padding: 0 !important;
box-sizing: border-box !important;
border: none !important;
outline: none !important;
}
/* 根元素强制设置 */
html {
width: ${targetWidth}px !important;
height: ${targetHeight}px !important;
min-width: ${targetWidth}px !important;
min-height: ${targetHeight}px !important;
max-width: ${targetWidth}px !important;
max-height: ${targetHeight}px !important;
overflow: hidden !important;
position: fixed !important;
top: 0 !important;
left: 0 !important;
margin: 0 !important;
padding: 0 !important;
border: none !important;
outline: none !important;
transform: none !important;
transform-origin: top left !important;
}
/* Body元素强制设置 */
body {
width: ${targetWidth}px !important;
height: ${targetHeight}px !important;
min-width: ${targetWidth}px !important;
min-height: ${targetHeight}px !important;
max-width: ${targetWidth}px !important;
max-height: ${targetHeight}px !important;
overflow: hidden !important;
position: fixed !important;
top: 0 !important;
left: 0 !important;
margin: 0 !important;
padding: 0 !important;
border: none !important;
outline: none !important;
transform: none !important;
transform-origin: top left !important;
}
/* 幻灯片容器强制设置 */
.slide-container {
width: ${targetWidth}px !important;
height: ${targetHeight}px !important;
min-width: ${targetWidth}px !important;
min-height: ${targetHeight}px !important;
max-width: ${targetWidth}px !important;
max-height: ${targetHeight}px !important;
position: fixed !important;
top: 0 !important;
left: 0 !important;
overflow: hidden !important;
transform: none !important;
transform-origin: top left !important;
margin: 0 !important;
padding: 0 !important;
border: none !important;
outline: none !important;
box-shadow: none !important;
z-index: 1 !important;
}
/* 隐藏所有滚动条 */
html::-webkit-scrollbar,
body::-webkit-scrollbar,
*::-webkit-scrollbar {
display: none !important;
width: 0 !important;
height: 0 !important;
}
/* Firefox */
html {
scrollbar-width: none !important;
}
/* 禁用用户交互 */
* {
-webkit-user-select: none !important;
-moz-user-select: none !important;
-ms-user-select: none !important;
user-select: none !important;
-webkit-user-drag: none !important;
-khtml-user-drag: none !important;
-moz-user-drag: none !important;
-o-user-drag: none !important;
user-drag: none !important;
pointer-events: none !important;
}
/* 移除响应式样式 */
.browse-mode * {
display: none !important;
}
</style>`
);
// 注入截图专用JavaScript
const finalHtml = optimizedHtml.replace(
/<\/body>/i,
`
<script id="screenshot-finalizer">
// 截图模式最终设置
window.SCREENSHOT_EXACT_MODE = true;
window.PPT_TARGET_WIDTH = ${targetWidth};
window.PPT_TARGET_HEIGHT = ${targetHeight};
// 立即执行尺寸强制设置
(function() {
const exactWidth = ${targetWidth};
const exactHeight = ${targetHeight};
console.log('截图模式强制设置:', exactWidth + 'x' + exactHeight);
// 强制设置根元素
const html = document.documentElement;
const body = document.body;
html.style.cssText = \`
width: \${exactWidth}px !important;
height: \${exactHeight}px !important;
min-width: \${exactWidth}px !important;
min-height: \${exactHeight}px !important;
max-width: \${exactWidth}px !important;
max-height: \${exactHeight}px !important;
overflow: hidden !important;
margin: 0 !important;
padding: 0 !important;
position: fixed !important;
top: 0 !important;
left: 0 !important;
border: none !important;
outline: none !important;
transform: none !important;
transform-origin: top left !important;
\`;
body.style.cssText = \`
width: \${exactWidth}px !important;
height: \${exactHeight}px !important;
min-width: \${exactWidth}px !important;
min-height: \${exactHeight}px !important;
max-width: \${exactWidth}px !important;
max-height: \${exactHeight}px !important;
overflow: hidden !important;
margin: 0 !important;
padding: 0 !important;
position: fixed !important;
top: 0 !important;
left: 0 !important;
border: none !important;
outline: none !important;
transform: none !important;
transform-origin: top left !important;
\`;
// 设置容器
const container = document.querySelector('.slide-container');
if (container) {
container.style.cssText = \`
width: \${exactWidth}px !important;
height: \${exactHeight}px !important;
min-width: \${exactWidth}px !important;
min-height: \${exactHeight}px !important;
max-width: \${exactWidth}px !important;
max-height: \${exactHeight}px !important;
position: fixed !important;
top: 0 !important;
left: 0 !important;
overflow: hidden !important;
margin: 0 !important;
padding: 0 !important;
border: none !important;
outline: none !important;
transform: none !important;
box-shadow: none !important;
z-index: 1 !important;
\`;
}
// 移除响应式类
html.classList.remove('browse-mode');
body.classList.remove('browse-mode');
console.log('截图模式设置完成');
})();
// DOM加载完成后再次确认
document.addEventListener('DOMContentLoaded', function() {
console.log('DOM加载完成,最终确认尺寸设置');
// 再次强制设置,确保完全生效
const html = document.documentElement;
const body = document.body;
const container = document.querySelector('.slide-container');
[html, body, container].forEach(element => {
if (element) {
element.style.width = '${targetWidth}px';
element.style.height = '${targetHeight}px';
element.style.minWidth = '${targetWidth}px';
element.style.minHeight = '${targetHeight}px';
element.style.maxWidth = '${targetWidth}px';
element.style.maxHeight = '${targetHeight}px';
element.style.margin = '0';
element.style.padding = '0';
element.style.border = 'none';
element.style.outline = 'none';
element.style.overflow = 'hidden';
if (element !== container) {
element.style.position = 'fixed';
element.style.top = '0';
element.style.left = '0';
}
}
});
// 最终验证
setTimeout(() => {
console.log('最终尺寸验证:', {
html: html.offsetWidth + 'x' + html.offsetHeight,
body: body.offsetWidth + 'x' + body.offsetHeight,
container: container ? container.offsetWidth + 'x' + container.offsetHeight : 'none',
target: '${targetWidth}x${targetHeight}'
});
}, 100);
});
</script>
</body>`
);
console.log('HTML修改完成,已注入截图优化代码');
return finalHtml;
}
async captureScreenshot(htmlContent, width, height, options = {}) {
try {
await this.initBrowser();
console.log(`开始截图, 目标尺寸: ${width}x${height}`);
// 修改HTML内容以适应截图
const optimizedHtml = await this.modifyHtmlForScreenshot(htmlContent, width, height);
// 创建新页面
const page = await this.browser.newPage();
try {
// 设置精确的viewport尺寸
await page.setViewport({
width: width,
height: height,
deviceScaleFactor: options.deviceScaleFactor || 2, // 高清截图
});
// 设置页面内容
await page.setContent(optimizedHtml, {
waitUntil: ['load', 'domcontentloaded', 'networkidle0'],
timeout: 30000
});
// 等待页面完全渲染
await page.waitForTimeout(2000);
// 执行最终的尺寸验证和调整
await page.evaluate((targetWidth, targetHeight) => {
const html = document.documentElement;
const body = document.body;
const container = document.querySelector('.slide-container');
// 最终强制设置
[html, body].forEach(element => {
if (element) {
element.style.width = targetWidth + 'px';
element.style.height = targetHeight + 'px';
element.style.minWidth = targetWidth + 'px';
element.style.minHeight = targetHeight + 'px';
element.style.maxWidth = targetWidth + 'px';
element.style.maxHeight = targetHeight + 'px';
element.style.margin = '0';
element.style.padding = '0';
element.style.border = 'none';
element.style.outline = 'none';
element.style.overflow = 'hidden';
element.style.position = 'fixed';
element.style.top = '0';
element.style.left = '0';
}
});
if (container) {
container.style.width = targetWidth + 'px';
container.style.height = targetHeight + 'px';
container.style.minWidth = targetWidth + 'px';
container.style.minHeight = targetHeight + 'px';
container.style.maxWidth = targetWidth + 'px';
container.style.maxHeight = targetHeight + 'px';
container.style.position = 'fixed';
container.style.top = '0';
container.style.left = '0';
container.style.margin = '0';
container.style.padding = '0';
container.style.border = 'none';
container.style.outline = 'none';
container.style.overflow = 'hidden';
container.style.transform = 'none';
container.style.boxShadow = 'none';
}
console.log('最终页面尺寸确认:', {
html: html.offsetWidth + 'x' + html.offsetHeight,
body: body.offsetWidth + 'x' + body.offsetHeight,
container: container ? container.offsetWidth + 'x' + container.offsetHeight : 'none'
});
}, width, height);
// 再次等待以确保所有更改生效
await page.waitForTimeout(1000);
// 执行截图,使用精确的剪裁区域
const screenshot = await page.screenshot({
type: options.format || 'png',
quality: options.quality || 100,
clip: {
x: 0,
y: 0,
width: width,
height: height
},
omitBackground: false, // 包含背景
captureBeyondViewport: false, // 不截取视口外内容
});
console.log(`截图完成, 生成了 ${screenshot.length} 字节的图片数据`);
return screenshot;
} finally {
await page.close();
}
} catch (error) {
console.error('截图失败:', error);
throw new Error(`截图失败: ${error.message}`);
}
}
}
export default new ScreenshotService(); |