erp_be/src/utils/simplePdf.js
2026-06-18 18:29:46 +05:30

39 lines
1.4 KiB
JavaScript

const buildSimplePdf = (lines = []) => {
const escapePdfText = (text) =>
String(text).replace(/\\/g, '\\\\').replace(/\(/g, '\\(').replace(/\)/g, '\\)');
const content = lines
.map((line, index) => `1 0 0 1 50 ${780 - index * 16} Tm (${escapePdfText(line)}) Tj`)
.join('\n');
const stream = `BT\n/F1 11 Tf\n${content}\nET`;
const streamLength = Buffer.byteLength(stream, 'utf8');
const objects = [
'1 0 obj<< /Type /Catalog /Pages 2 0 R >>endobj',
'2 0 obj<< /Type /Pages /Kids [3 0 R] /Count 1 >>endobj',
'3 0 obj<< /Type /Page /Parent 2 0 R /MediaBox [0 0 595 842] /Resources << /Font << /F1 4 0 R >> >> /Contents 5 0 R >>endobj',
'4 0 obj<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>endobj',
`5 0 obj<< /Length ${streamLength} >>stream\n${stream}\nendstream endobj`,
];
let pdf = '%PDF-1.4\n';
const offsets = [0];
objects.forEach((object) => {
offsets.push(Buffer.byteLength(pdf, 'utf8'));
pdf += `${object}\n`;
});
const xrefOffset = Buffer.byteLength(pdf, 'utf8');
pdf += `xref\n0 ${objects.length + 1}\n`;
pdf += '0000000000 65535 f \n';
offsets.slice(1).forEach((offset) => {
pdf += `${String(offset).padStart(10, '0')} 00000 n \n`;
});
pdf += `trailer<< /Size ${objects.length + 1} /Root 1 0 R >>\nstartxref\n${xrefOffset}\n%%EOF`;
return Buffer.from(pdf, 'utf8');
};
module.exports = { buildSimplePdf };