Spaces:
Running
Running
feat: update server logic
Browse filesCo-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
server.js
CHANGED
|
@@ -121,22 +121,35 @@ app.get('/', (req, res) => {
|
|
| 121 |
|
| 122 |
app.post('/api/generate_pdf', async (req, res) => {
|
| 123 |
const startTime = Date.now();
|
| 124 |
-
const envText = isTest ? '测试环境' : '生产环境';
|
| 125 |
-
const
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
const
|
| 129 |
-
const
|
| 130 |
-
const
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
const getElapsed = () => ((Date.now() - startTime) / 1000).toFixed(2) + 's';
|
| 141 |
let browser = null;
|
| 142 |
|
|
@@ -172,7 +185,11 @@ app.post('/api/generate_pdf', async (req, res) => {
|
|
| 172 |
|
| 173 |
console.log(`[PDF-GEN] [${getElapsed()}] 解析请求完成: HTML ${htmlSizeMB} MB, 图片 ${imgCount} 张 (${imgSizeMB} MB)`);
|
| 174 |
|
| 175 |
-
//
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
// 问题:loading="lazy" 导致视口外的图片在 PDF 中不渲染
|
| 177 |
// 解决方案:https://stackoverflow.com/questions/79156691/puppeteersharp-fails-to-display-base64-encoded-images-in-pdf-output
|
| 178 |
htmlToProcess = htmlToProcess.replace(/loading=["']lazy["']/gi, '');
|
|
@@ -382,7 +399,7 @@ app.post('/api/generate_pdf', async (req, res) => {
|
|
| 382 |
await browser.close();
|
| 383 |
browser = null;
|
| 384 |
|
| 385 |
-
console.log(`
|
| 386 |
|
| 387 |
res.setHeader('Content-Type', 'application/pdf');
|
| 388 |
res.setHeader('Content-Disposition', 'attachment; filename=export.pdf');
|
|
|
|
| 121 |
|
| 122 |
app.post('/api/generate_pdf', async (req, res) => {
|
| 123 |
const startTime = Date.now();
|
| 124 |
+
const envText = isTest ? '【测试环境】' : '【生产环境】';
|
| 125 |
+
const { platform, exportCount, language, exportPdf, exportMd, exportTxt, exportDocx, exportJson, exportClipboard, exportNotion, extensionVersion, imageCount, totalImageSizeMB, isPro } = req.body;
|
| 126 |
+
|
| 127 |
+
// PRO status with text emphasis (no ANSI colors for Docker compatibility)
|
| 128 |
+
const userTag = isPro ? '★★★PRO★★★' : '◆FREE◆';
|
| 129 |
+
const platformStr = platform || 'unknown';
|
| 130 |
+
const versionStr = extensionVersion || '-';
|
| 131 |
+
const langStr = language || '-';
|
| 132 |
+
|
| 133 |
+
// Format counts
|
| 134 |
+
const fmtParts = [];
|
| 135 |
+
if (exportPdf != null) fmtParts.push(`PDF:${exportPdf}`);
|
| 136 |
+
if (exportMd != null) fmtParts.push(`MD:${exportMd}`);
|
| 137 |
+
if (exportTxt != null) fmtParts.push(`TXT:${exportTxt}`);
|
| 138 |
+
if (exportDocx != null) fmtParts.push(`DOCX:${exportDocx}`);
|
| 139 |
+
if (exportJson != null) fmtParts.push(`JSON:${exportJson}`);
|
| 140 |
+
if (exportClipboard != null) fmtParts.push(`CLIP:${exportClipboard}`);
|
| 141 |
+
if (exportNotion != null) fmtParts.push(`NOTION:${exportNotion}`);
|
| 142 |
+
const fmtStr = fmtParts.join(', ') || '-';
|
| 143 |
+
|
| 144 |
+
// Two-line log for readability
|
| 145 |
+
console.log(
|
| 146 |
+
`---------------[PDF-GEN] ${envText} ${userTag} 收到请求 | 平台: ${platformStr} | 版本: ${versionStr} | 语言: ${langStr}`
|
| 147 |
+
);
|
| 148 |
+
console.log(
|
| 149 |
+
` 导出: ${exportCount ?? '-'}次 | 格式: ${fmtStr} | 图片: ${imageCount ?? '-'}张 (${totalImageSizeMB ?? '-'}MB)`
|
| 150 |
+
);
|
| 151 |
+
console.log(`---------------`);
|
| 152 |
+
|
| 153 |
const getElapsed = () => ((Date.now() - startTime) / 1000).toFixed(2) + 's';
|
| 154 |
let browser = null;
|
| 155 |
|
|
|
|
| 185 |
|
| 186 |
console.log(`[PDF-GEN] [${getElapsed()}] 解析请求完成: HTML ${htmlSizeMB} MB, 图片 ${imgCount} 张 (${imgSizeMB} MB)`);
|
| 187 |
|
| 188 |
+
// 移除 <script> 标签防止 Puppeteer 执行阻塞(如 alert() 弹窗会导致 setContent 内部 CDP 调用超时)
|
| 189 |
+
htmlToProcess = htmlToProcess.replace(/<script[\s\S]*?<\/script>/gi, '');
|
| 190 |
+
console.log(`[PDF-GEN] [${getElapsed()}] 已移除 <script> 标签`);
|
| 191 |
+
|
| 192 |
+
// 移除所有图片的 loading="lazy" 属性
|
| 193 |
// 问题:loading="lazy" 导致视口外的图片在 PDF 中不渲染
|
| 194 |
// 解决方案:https://stackoverflow.com/questions/79156691/puppeteersharp-fails-to-display-base64-encoded-images-in-pdf-output
|
| 195 |
htmlToProcess = htmlToProcess.replace(/loading=["']lazy["']/gi, '');
|
|
|
|
| 399 |
await browser.close();
|
| 400 |
browser = null;
|
| 401 |
|
| 402 |
+
console.log(`[PDF-GEN] [${getElapsed()}] >>> 任务全部完成 <<<`);
|
| 403 |
|
| 404 |
res.setHeader('Content-Type', 'application/pdf');
|
| 405 |
res.setHeader('Content-Disposition', 'attachment; filename=export.pdf');
|