Top
Best
New

Posted by lulzx 5 days ago

Show HN: TinyPDF – 3kb pdf library (70x smaller than jsPDF)(github.com)
I needed to generate invoices in a Node.js app. jsPDF is 229KB. I only needed text, rectangles, lines, and JPEG images.

  So I wrote tinypdf: <400 lines of TypeScript, zero dependencies, 3.3KB minified+gzipped.

  What it does:
  - Text (Helvetica, colors, alignment)
  - Rectangles and lines
  - JPEG images
  - Multiple pages, custom sizes

  What it doesn't do:
  - Custom fonts, PNG/SVG, forms, encryption, HTML-to-PDF

  That's it. The 95% use case for invoices, receipts, reports, tickets, and labels.

  GitHub: https://github.com/Lulzx/tinypdf
  npm: npm install tinypdf
251 points | 32 comments
RodgerTheGreat 4 days ago|
It's definitely far easier to emit a controlled, useful subset of PDF than it is to parse PDF documents. I wrote a small PDF library for the Decker ecosystem that just focuses on bitmaps and page layout; roughly 4kb and 135 LoC.

docs/demos: https://beyondloom.com/decker/pdf.html

browsable source: https://github.com/JohnEarnest/Decker/blob/main/examples/dec...

kuschkufan 4 days ago||
This decker stuff is pretty nifty too
user3939382 3 days ago||
I’m working on one rn. It takes arbitrary PDFs and builds composable dynamic pandoc pipelines to match the source byte for byte output. It’s very very complex. But if I can get it finished it will fuck over Adobe so worth it.
culi 4 days ago||
While not quite as small as 3kb, I recently found this incredible library called html-to-image that's only 300kb. It clones whatever subtree of your document you want to a <foreignObject> inside an svg which then allows it to output canvas, png, svg, pdf, blob, jpeg, etc. Even more impressively is that it handles custom fonts, pseudo-elements, computed styles and more.

https://github.com/bubkoo/html-to-image

It's probably the most impressive and seamless experience I've had with converting HTML to pdfs/images so I just wanted to sing its praises here

normie3000 4 days ago|
It does sound very cool, but I don't think it exports to PDF: https://github.com/bubkoo/html-to-image/issues/327
culi 3 days ago||
Oh my, you're right. I've actually only tried it for exporting to images. Sorry for the misleading comment!
layer8 4 days ago||
Only supports ASCII characters, which is part of the trick here. As soon as you need more Unicode (even just typographic quote characters and such), you’ll need significantly more logic. Also no bold, italics, etc.
andai 4 days ago||
Back in the day I needed PDF export for some client thing. I can't remember if I was using pdfjs or jspdf. I do however remember that it was many thousands of lines of code, and yet, I had to lay out the lines of text on the page manually.

My page layout code was like 50 lines of code. And I remember thinking... OK they already wrote 8,000 lines of code... They couldn't have added 50 more?!

400 lines though. Respect. I will take a proper look at this when I recover from burnout :)

anilgulecha 5 days ago||
Great exercize, but for most use cases - people will continue reaching for jsPDF.

I think if you have a markdown->PDF function included, where I can send in markdown and get PDF, that would solve quite many needs, and would be useful.

lulzx 5 days ago|
I have added it!
nebezb 4 days ago|||
Wow you’re not kidding. That was fast.

https://github.com/Lulzx/tinypdf/commit/961e6b602f19e125f210...

pylotlight 4 days ago|||
HTML -> PDF was also a use case I've used previously FYI
raybb 4 days ago||
Was Typst falling short in any particular area that made you not want to use it? (If it was on your radar at all). I think it would work for your use case and could also run client side if needed.

Here's the TS library: https://github.com/Myriad-Dreamin/typst.ts

wg0 4 days ago||
So essentially - it only works with Latin script? Because without fonts, every other script is NOT going to render.
jbaiter 4 days ago|
Agree, the lack of support for TTF fonts is a bummer for most non-english use cases:-/
copypaper 4 days ago||
Nice work! I'm curious though, what was your use case for needing a smaller library? Since you're running this on a server, what difference does an extra 226KB make?
lulzx 4 days ago|
on cloudflare workers, the bundle limit makes 226KB massive.
kentonv 4 days ago||
FWIW, the limit these days is 10MB (after compression).

https://developers.cloudflare.com/workers/platform/limits/

That said, thank you for fighting library bloat! Small libraries tend to perform better (not just on Workers but in general).

IntelliAvatar 5 days ago|
3KB is wild. What features did you intentionally leave out to get this small?
wonger_ 4 days ago||
Not the author, but generating PDFs is much, much simpler than parsing PDFs
IntelliAvatar 3 days ago||
That makes sense. I was mostly curious about what explicit trade-offs the author chose beyond “generation only” — e.g. fonts, Unicode, images, compression, etc.

Would be interesting to see a concrete “not supported” list from the author.

lysace 4 days ago|||
Support for more than 7-bit ASCII characters. :)
teaearlgraycold 4 days ago|||
It only supports Helvetica!
hu3 4 days ago||
utf-8
More comments...