File size: 1,882 Bytes
24a2ddf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * 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); });