File size: 10,956 Bytes
8e2959f 0afb612 8e2959f 21da54e 0afb612 7b673de 0afb612 21da54e 0afb612 613c920 0afb612 613c920 0afb612 613c920 0afb612 613c920 0afb612 613c920 0afb612 613c920 0afb612 613c920 0afb612 613c920 0afb612 613c920 0afb612 1d4c41b 21da54e 0afb612 613c920 0afb612 613c920 0afb612 613c920 aaa3dc1 0afb612 d1278b8 0afb612 d1278b8 0afb612 cf5bd9a 0afb612 21da54e 0afb612 21da54e 0afb612 21da54e 0afb612 aaa3dc1 0afb612 aaa3dc1 21da54e b4297f5 d1278b8 0afb612 7b673de cf5bd9a 0afb612 cf5bd9a 0afb612 cf5bd9a 0afb612 613c920 0afb612 613c920 0afb612 613c920 0afb612 613c920 0afb612 613c920 0afb612 aaa3dc1 0afb612 d1278b8 aaa3dc1 0afb612 aaa3dc1 0afb612 aaa3dc1 8e2959f 0afb612 |
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 |
import puppeteer from 'puppeteer';
import playwright from 'playwright';
class ScreenshotService {
constructor() {
this.puppeteerBrowser = null;
this.playwrightBrowser = null;
this.isInitializing = false;
this.puppeteerReady = false;
this.playwrightReady = false;
console.log('Screenshot service initialized');
}
// Initialize Puppeteer browser
async initPuppeteer() {
if (this.puppeteerBrowser || this.isInitializing) return;
this.isInitializing = true;
try {
console.log('π Starting Puppeteer browser...');
const launchOptions = {
headless: 'new',
args: [
'--no-sandbox',
'--disable-setuid-sandbox',
'--disable-dev-shm-usage',
'--disable-gpu',
'--disable-extensions',
'--disable-background-timer-throttling',
'--disable-backgrounding-occluded-windows',
'--disable-renderer-backgrounding',
'--no-first-run',
'--no-default-browser-check',
'--disable-default-apps',
'--disable-features=TranslateUI',
'--disable-ipc-flooding-protection',
'--memory-pressure-off',
'--max_old_space_size=4096'
],
timeout: 30000
};
// Hugging Face Spaces optimizations
if (process.env.SPACE_ID) {
launchOptions.args.push(
'--single-process',
'--disable-background-networking',
'--disable-background-mode'
);
}
this.puppeteerBrowser = await puppeteer.launch(launchOptions);
this.puppeteerReady = true;
console.log('β
Puppeteer browser started successfully');
// Cleanup when process exits
process.on('exit', () => this.cleanup());
process.on('SIGINT', () => this.cleanup());
process.on('SIGTERM', () => this.cleanup());
} catch (error) {
console.error('β Puppeteer initialization failed:', error.message);
this.puppeteerReady = false;
} finally {
this.isInitializing = false;
}
}
// Initialize Playwright browser (fallback)
async initPlaywright() {
if (this.playwrightBrowser) return;
try {
console.log('π Starting Playwright browser...');
this.playwrightBrowser = await playwright.chromium.launch({
headless: true,
args: [
'--no-sandbox',
'--disable-setuid-sandbox',
'--disable-dev-shm-usage'
]
});
this.playwrightReady = true;
console.log('β
Playwright browser started successfully');
} catch (error) {
console.error('β Playwright initialization failed:', error.message);
this.playwrightReady = false;
}
}
// Generate screenshot
async generateScreenshot(htmlContent, options = {}) {
const {
format = 'jpeg',
quality = 90,
width = 1000,
height = 562,
timeout = 15000
} = options;
console.log(`πΈ Generating screenshot: ${width}x${height}, ${format}@${quality}%`);
// Try Puppeteer first
if (!this.puppeteerReady) {
await this.initPuppeteer();
}
if (this.puppeteerReady) {
try {
return await this.generateWithPuppeteer(htmlContent, { format, quality, width, height, timeout });
} catch (error) {
console.warn('Puppeteer screenshot failed, trying Playwright:', error.message);
}
}
// Fallback to Playwright
if (!this.playwrightReady) {
await this.initPlaywright();
}
if (this.playwrightReady) {
try {
return await this.generateWithPlaywright(htmlContent, { format, quality, width, height, timeout });
} catch (error) {
console.warn('Playwright screenshot failed:', error.message);
}
}
// Final fallback: generate SVG
console.warn('All screenshot methods failed, generating fallback SVG');
return this.generateFallbackImage(width, height, 'Screenshot Service', 'Browser unavailable');
}
// Generate screenshot using Puppeteer
async generateWithPuppeteer(htmlContent, options) {
const { format, quality, width, height, timeout } = options;
let page = null;
try {
page = await this.puppeteerBrowser.newPage();
// Set viewport
await page.setViewport({ width, height });
// Set content
await page.setContent(htmlContent, {
waitUntil: 'networkidle0',
timeout: timeout
});
// Wait for fonts
await page.evaluate(() => {
return document.fonts ? document.fonts.ready : Promise.resolve();
});
// Additional wait for rendering
await page.waitForTimeout(300);
// Take screenshot
const screenshotOptions = {
type: format,
quality: format === 'jpeg' ? quality : undefined,
fullPage: false,
clip: { x: 0, y: 0, width, height }
};
const screenshot = await page.screenshot(screenshotOptions);
console.log(`β
Puppeteer screenshot generated: ${screenshot.length} bytes`);
return screenshot;
} finally {
if (page) {
await page.close().catch(console.error);
}
}
}
// Generate screenshot using Playwright
async generateWithPlaywright(htmlContent, options) {
const { format, quality, width, height, timeout } = options;
let page = null;
try {
page = await this.playwrightBrowser.newPage();
// Set viewport
await page.setViewportSize({ width, height });
// Set content
await page.setContent(htmlContent, {
waitUntil: 'networkidle',
timeout: timeout
});
// Wait for fonts
await page.evaluate(() => {
return document.fonts ? document.fonts.ready : Promise.resolve();
});
// Take screenshot
const screenshotOptions = {
type: format,
quality: format === 'jpeg' ? quality : undefined,
fullPage: false,
clip: { x: 0, y: 0, width, height }
};
const screenshot = await page.screenshot(screenshotOptions);
console.log(`β
Playwright screenshot generated: ${screenshot.length} bytes`);
return screenshot;
} finally {
if (page) {
await page.close().catch(console.error);
}
}
}
// Generate fallback SVG image
generateFallbackImage(width = 1000, height = 562, title = 'PPT Screenshot', subtitle = '', message = '') {
const svg = `<?xml version="1.0" encoding="UTF-8"?>
<svg width="${width}" height="${height}" xmlns="http://www.w3.org/2000/svg">
<defs>
<linearGradient id="bg" x1="0%" y1="0%" x2="100%" y2="100%">
<stop offset="0%" style="stop-color:#f8f9fa;stop-opacity:1" />
<stop offset="100%" style="stop-color:#e9ecef;stop-opacity:1" />
</linearGradient>
<pattern id="dots" patternUnits="userSpaceOnUse" width="20" height="20">
<circle cx="2" cy="2" r="1" fill="#dee2e6" opacity="0.5"/>
</pattern>
</defs>
<!-- Background -->
<rect width="100%" height="100%" fill="url(#bg)"/>
<rect width="100%" height="100%" fill="url(#dots)"/>
<!-- Border -->
<rect x="20" y="20" width="${width-40}" height="${height-40}"
fill="none" stroke="#dee2e6" stroke-width="3"
stroke-dasharray="15,10" rx="10"/>
<!-- Icon -->
<g transform="translate(${width/2}, ${height*0.25})">
<circle cx="0" cy="0" r="30" fill="#6c757d" opacity="0.3"/>
<rect x="-15" y="-10" width="30" height="20" rx="3" fill="#6c757d"/>
<rect x="-12" y="-7" width="24" height="3" fill="white"/>
<rect x="-12" y="-2" width="24" height="3" fill="white"/>
<rect x="-12" y="3" width="16" height="3" fill="white"/>
</g>
<!-- Title -->
<text x="${width/2}" y="${height*0.45}"
text-anchor="middle"
font-family="Microsoft YaHei, PingFang SC, Hiragino Sans GB, Source Han Sans SC, Noto Sans SC, WenQuanYi Micro Hei, SimHei, SimSun, Arial, sans-serif"
font-size="28"
font-weight="bold"
fill="#495057">${title}</text>
${subtitle ? `
<!-- Subtitle -->
<text x="${width/2}" y="${height*0.55}"
text-anchor="middle"
font-family="Microsoft YaHei, PingFang SC, Hiragino Sans GB, Source Han Sans SC, Noto Sans SC, WenQuanYi Micro Hei, SimHei, SimSun, Arial, sans-serif"
font-size="18"
fill="#6c757d">${subtitle}</text>
` : ''}
${message ? `
<!-- Message -->
<text x="${width/2}" y="${height*0.65}"
text-anchor="middle"
font-family="Microsoft YaHei, PingFang SC, Hiragino Sans GB, Source Han Sans SC, Noto Sans SC, WenQuanYi Micro Hei, SimHei, SimSun, Arial, sans-serif"
font-size="14"
fill="#adb5bd">${message}</text>
` : ''}
<!-- Footer -->
<text x="${width/2}" y="${height*0.85}"
text-anchor="middle"
font-family="Microsoft YaHei, PingFang SC, Hiragino Sans GB, Source Han Sans SC, Noto Sans SC, WenQuanYi Micro Hei, SimHei, SimSun, Arial, sans-serif"
font-size="12"
fill="#ced4da">PPT Screenshot Service β’ ${new Date().toLocaleString('zh-CN')}</text>
<!-- Dimensions info -->
<text x="${width/2}" y="${height*0.92}"
text-anchor="middle"
font-family="Microsoft YaHei, PingFang SC, Hiragino Sans GB, Source Han Sans SC, Noto Sans SC, WenQuanYi Micro Hei, SimHei, SimSun, Arial, sans-serif"
font-size="10"
fill="#ced4da">Size: ${width} Γ ${height}</text>
</svg>`;
return Buffer.from(svg, 'utf-8');
}
// Get service status
getStatus() {
return {
puppeteerReady: this.puppeteerReady,
playwrightReady: this.playwrightReady,
environment: process.env.NODE_ENV || 'development',
isHuggingFace: !!process.env.SPACE_ID
};
}
// Cleanup resources
async cleanup() {
console.log('π§Ή Cleaning up screenshot service...');
if (this.puppeteerBrowser) {
try {
await this.puppeteerBrowser.close();
this.puppeteerBrowser = null;
this.puppeteerReady = false;
console.log('β
Puppeteer browser closed');
} catch (error) {
console.error('Error closing Puppeteer browser:', error);
}
}
if (this.playwrightBrowser) {
try {
await this.playwrightBrowser.close();
this.playwrightBrowser = null;
this.playwrightReady = false;
console.log('β
Playwright browser closed');
} catch (error) {
console.error('Error closing Playwright browser:', error);
}
}
}
}
export default new ScreenshotService(); |