XWX-AI commited on
Commit
89c8cea
·
1 Parent(s): ab40ae2

压力测试

Browse files
Files changed (1) hide show
  1. server.js +61 -3
server.js CHANGED
@@ -25,6 +25,14 @@ app.post('/api/generate_pdf', async (req, res) => {
25
  }
26
 
27
  const brandText = showWatermark !== false ? 'Powered by XWX AI Chat Exporter' : '';
 
 
 
 
 
 
 
 
28
 
29
  // Launch Chrome
30
  // We use the installed 'chromium' from apt-get
@@ -34,15 +42,62 @@ app.post('/api/generate_pdf', async (req, res) => {
34
  '--no-sandbox',
35
  '--disable-setuid-sandbox',
36
  '--disable-dev-shm-usage', // Critical for Docker
37
- '--font-render-hinting=none' // Better font rendering
 
 
 
38
  ],
39
  headless: 'new'
40
  });
41
 
42
  const page = await browser.newPage();
43
 
44
- // Set content
45
- await page.setContent(html, { waitUntil: 'networkidle0' });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
 
47
  // Inject styles to ensure 100% width and print media simulation
48
  await page.addStyleTag({
@@ -74,6 +129,9 @@ app.post('/api/generate_pdf', async (req, res) => {
74
 
75
  await browser.close();
76
  browser = null;
 
 
 
77
 
78
  // Send response
79
  res.setHeader('Content-Type', 'application/pdf');
 
25
  }
26
 
27
  const brandText = showWatermark !== false ? 'Powered by XWX AI Chat Exporter' : '';
28
+
29
+ // Log HTML size for debugging
30
+ const htmlSizeMB = (html.length / 1024 / 1024).toFixed(2);
31
+ console.log(`[PDF-GEN] Received HTML: ${htmlSizeMB} MB`);
32
+
33
+ // Count base64 images in HTML
34
+ const base64Matches = html.match(/data:image\/[^;]+;base64,/g);
35
+ console.log(`[PDF-GEN] Found ${base64Matches ? base64Matches.length : 0} base64 images in HTML`);
36
 
37
  // Launch Chrome
38
  // We use the installed 'chromium' from apt-get
 
42
  '--no-sandbox',
43
  '--disable-setuid-sandbox',
44
  '--disable-dev-shm-usage', // Critical for Docker
45
+ '--font-render-hinting=none', // Better font rendering
46
+ '--disable-gpu', // Disable GPU for stability in Docker
47
+ '--disable-software-rasterizer',
48
+ '--memory-pressure-off'
49
  ],
50
  headless: 'new'
51
  });
52
 
53
  const page = await browser.newPage();
54
 
55
+ // Set viewport for consistent rendering
56
+ await page.setViewport({ width: 1200, height: 800 });
57
+
58
+ // Set content with longer timeout for large HTML
59
+ // Use 'load' instead of 'networkidle0' because base64 images don't trigger network requests
60
+ console.log('[PDF-GEN] Setting page content...');
61
+ await page.setContent(html, {
62
+ waitUntil: 'load',
63
+ timeout: 120000 // 2 minutes for large content
64
+ });
65
+ console.log('[PDF-GEN] Page content loaded');
66
+
67
+ // Wait for images to fully render (additional 3 seconds for base64 decoding)
68
+ console.log('[PDF-GEN] Waiting for images to render...');
69
+ await page.waitForTimeout(3000);
70
+ console.log('[PDF-GEN] Image render wait complete');
71
+
72
+ // Count images in the rendered page
73
+ const pageImageCount = await page.evaluate(() => {
74
+ return document.querySelectorAll('img').length;
75
+ });
76
+ console.log(`[PDF-GEN] Rendered page contains ${pageImageCount} img elements`);
77
+
78
+ // Scroll to bottom to ensure all lazy-loaded images are visible
79
+ console.log('[PDF-GEN] Scrolling to ensure all images are loaded...');
80
+ await page.evaluate(async () => {
81
+ await new Promise((resolve) => {
82
+ let totalHeight = 0;
83
+ const distance = 100;
84
+ const timer = setInterval(() => {
85
+ const scrollHeight = document.body.scrollHeight;
86
+ window.scrollBy(0, distance);
87
+ totalHeight += distance;
88
+
89
+ if (totalHeight >= scrollHeight) {
90
+ clearInterval(timer);
91
+ window.scrollTo(0, 0); // Scroll back to top
92
+ resolve();
93
+ }
94
+ }, 50);
95
+ });
96
+ });
97
+ console.log('[PDF-GEN] Scroll complete');
98
+
99
+ // Wait additional time for any lazy-loaded images
100
+ await page.waitForTimeout(2000);
101
 
102
  // Inject styles to ensure 100% width and print media simulation
103
  await page.addStyleTag({
 
129
 
130
  await browser.close();
131
  browser = null;
132
+
133
+ const pdfSizeMB = (pdfBuffer.length / 1024 / 1024).toFixed(2);
134
+ console.log(`[PDF-GEN] PDF generated successfully: ${pdfSizeMB} MB`);
135
 
136
  // Send response
137
  res.setHeader('Content-Type', 'application/pdf');