littlepdf
v0.1.1
Published
Minimal PDF creation library. <400 LOC, zero dependencies, makes real PDFs.
Maintainers
Readme
littlepdf
Minimal PDF creation library. <400 LOC, zero dependencies, makes real PDFs.
npm install littlepdfWhy littlepdf?
Ultra-lightweight. We removed TTF fonts, PNG/SVG, HTML-to-PDF, forms, encryption, and compression. What's left is the 95% use case: put text and images on a page.
Build with it
Invoices, receipts, reports, shipping labels, tickets, certificates, contracts, data exports
Features
| Feature | Description | |---------|-------------| | Text | Helvetica, any size, hex colors, align left/center/right | | Shapes | Rectangles and lines | | Images | JPEG (photos, logos, signatures) | | Pages | Multiple pages, custom sizes | | Markdown | Convert markdown to PDF with headers, lists, rules |
Not included
Custom fonts, PNG/GIF/SVG, vector graphics, forms, encryption, compression, HTML-to-PDF
Need those? Use jsPDF or pdf-lib.
Quick start
import { pdf } from 'littlepdf'
import { writeFileSync } from 'fs'
const doc = pdf()
doc.page((ctx) => {
ctx.rect(50, 700, 200, 40, '#2563eb') // blue rectangle
ctx.text('Hello PDF!', 60, 712, 24, { color: '#ffffff' })
ctx.line(50, 680, 250, 680, '#000000', 1) // black line
})
writeFileSync('output.pdf', doc.build())Add images
import { readFileSync } from 'fs'
doc.page((ctx) => {
const logo = new Uint8Array(readFileSync('logo.jpg'))
ctx.image(logo, 50, 700, 100, 50)
})Measure text width
import { measureText } from 'littlepdf'
measureText('Hello', 12) // => 27.34 (points)Markdown to PDF
import { markdown } from 'littlepdf'
import { writeFileSync } from 'fs'
const pdf = markdown(`
# Hello World
A minimal PDF from markdown.
## Features
- Headers (h1, h2, h3)
- Bullet lists
- Numbered lists
- Horizontal rules
---
Automatic word wrapping and pagination included.
`)
writeFileSync('output.pdf', pdf)API
pdf() // create document
doc.page(callback) // add page (612×792 default)
doc.page(width, height, callback) // add page with custom size
doc.build() // returns Uint8Array
ctx.text(str, x, y, size, options?) // options: { color, align, width }
ctx.rect(x, y, w, h, fill) // filled rectangle
ctx.line(x1, y1, x2, y2, stroke, width?) // line
ctx.image(jpegBytes, x, y, w, h) // JPEG image
measureText(str, size) // text width in points
markdown(str, options?) // options: { width, height, margin }Development
Setup
npm installBuild
npm run buildRun Example
npm run devOr directly with tsx:
npx tsx examples/example.tsThis will generate examples/example.pdf.
Publishing
To publish this package to npm:
- Update
package.jsonwith your author info and repository URL - Build:
npm run build - Publish:
npm publish --access public
See PUBLISHING.md for detailed instructions.
License
MIT
