Spaces:
Running
Running
| /** | |
| * Generate a PDF via the backend and save to disk for quality verification. | |
| * Usage: node tests/stress/verify-pdf-output.js <output.pdf> | |
| */ | |
| const fs = require('fs'); | |
| const http = require('http'); | |
| const path = require('path'); | |
| const { prepareProfile } = require('./payload-generator'); | |
| const BASE = process.env.BASE_URL || 'http://localhost:17861'; | |
| const outFile = process.argv[2] || path.join(__dirname, 'results', 'verify-output.pdf'); | |
| async function main() { | |
| const profile = prepareProfile({ name: 'medium', opts: { textMB: 0.3, images: 3, codeRatio: 0.35 } }); | |
| const requestBody = { ...profile.requestBody, html: profile.html }; | |
| const payload = JSON.stringify(requestBody); | |
| const url = new URL('/api/generate_pdf', BASE); | |
| const started = Date.now(); | |
| const res = await new Promise((resolve, reject) => { | |
| const req = http.request({ hostname: url.hostname, port: url.port || 80, path: url.pathname, method: 'POST', | |
| headers: { 'Content-Type': 'application/json', 'Content-Length': Buffer.byteLength(payload) }, timeout: 120000 }, resolve); | |
| req.on('error', reject); | |
| req.write(payload); | |
| req.end(); | |
| }); | |
| const chunks = []; | |
| for await (const c of res) chunks.push(c); | |
| const buf = Buffer.concat(chunks); | |
| const elapsed = ((Date.now() - started) / 1000).toFixed(2); | |
| console.log(`HTTP ${res.statusCode} in ${elapsed}s, bytes=${buf.length}`); | |
| if (res.statusCode !== 200) { | |
| console.log(buf.toString().substring(0, 500)); | |
| process.exit(1); | |
| } | |
| fs.writeFileSync(outFile, buf); | |
| const head = buf.slice(0, 8).toString('latin1'); | |
| console.log(`Saved: ${outFile}`); | |
| console.log(`Header magic: ${head} (expects %PDF-1.x)`); | |
| const pageCount = (buf.toString('latin1').match(/\/Type\s*\/Page[^s]/g) || []).length; | |
| console.log(`Approx page count: ${pageCount}`); | |
| } | |
| main().catch((e) => { console.error(e); process.exit(1); }); | |