npm install @ferrow/pdf-generatorA minimal text-and-lines PDF writer, built from scratch — no bindings, no embedded binaries, zero dependencies. It emits valid PDF 1.4 byte-for-byte: catalog, pages, page, font, and content-stream objects, an exact xref table (byte offsets computed while writing, not estimated), and a trailer.
- Multi-page documents (
addPage()). - Text placement at
(x, y)with a size, using the core Helvetica font (no font embedding — Helvetica is one of the 14 standard PDF fonts every reader already has). - Straight lines (
line(x1, y1, x2, y2)). mm()/pt()unit helpers (PDF's native unit is points; 1mm = 72/25.4pt).- Correct xref offsets, verified by
filereporting a validPDF document, version 1.4, N pagesand bystartxrefpointing at the realxrefbyte.
- No images, no embedded/custom fonts, no Unicode (Helvetica + WinAnsi-range
text only — escapes
\,(,); does not transcode wide characters). - No compression, encryption, forms, or annotations.
- Not a layout engine — no word wrap, no flow, you place text and lines by coordinate. For anything beyond plain text-and-lines, use pdfkit or pdf-lib.
npm install
npm run build
node dist/examples/demo.jsimport { PdfDocument, mm } from 'pdf-generator';
const doc = new PdfDocument(); // defaults to A4; pass { pageSize } for Letter, etc.
const page = doc.addPage();
page.text(mm(20), page.size.height - mm(20), 'Hello, PDF', { size: 18 });
page.line(mm(20), page.size.height - mm(25), mm(190), page.size.height - mm(25));
const buffer = doc.toBuffer(); // Node Buffer, valid PDF 1.4
require('fs').writeFileSync('out.pdf', buffer);Coordinates are in points with the PDF-native origin at the page's bottom-left corner (not top-left).
examples/demo.ts generates a 2-page PDF and verifies the output:
$ node dist/examples/demo.js
header: %PDF-1.4
size: 985 bytes
contains text: true
$ file dist/examples/demo-output.pdf
dist/examples/demo-output.pdf: PDF document, version 1.4, 2 pages
MIT
Sponsored by Ferrow
Part of the ferrow-toolkit collection · Sponsored by Ferrow