18 comments

  • RodgerTheGreat49 days ago
    It&#x27;s definitely <i>far</i> 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.<p>docs&#x2F;demos: <a href="https:&#x2F;&#x2F;beyondloom.com&#x2F;decker&#x2F;pdf.html" rel="nofollow">https:&#x2F;&#x2F;beyondloom.com&#x2F;decker&#x2F;pdf.html</a><p>browsable source: <a href="https:&#x2F;&#x2F;github.com&#x2F;JohnEarnest&#x2F;Decker&#x2F;blob&#x2F;main&#x2F;examples&#x2F;decks&#x2F;pdf.deck#L142-L276" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;JohnEarnest&#x2F;Decker&#x2F;blob&#x2F;main&#x2F;examples&#x2F;dec...</a>
    • kuschkufan49 days ago
      This decker stuff is pretty nifty too
    • user393938248 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.
  • culi49 days ago
    While not quite as small as 3kb, I recently found this incredible library called html-to-image that&#x27;s only 300kb. It clones whatever subtree of your document you want to a &lt;foreignObject&gt; 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.<p><a href="https:&#x2F;&#x2F;github.com&#x2F;bubkoo&#x2F;html-to-image" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;bubkoo&#x2F;html-to-image</a><p>It&#x27;s probably the most impressive and seamless experience I&#x27;ve had with converting HTML to pdfs&#x2F;images so I just wanted to sing its praises here
    • normie300049 days ago
      It does sound very cool, but I don&#x27;t think it exports to PDF: <a href="https:&#x2F;&#x2F;github.com&#x2F;bubkoo&#x2F;html-to-image&#x2F;issues&#x2F;327" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;bubkoo&#x2F;html-to-image&#x2F;issues&#x2F;327</a>
      • culi48 days ago
        Oh my, you&#x27;re right. I&#x27;ve actually only tried it for exporting to images. Sorry for the misleading comment!
  • layer849 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.
  • andai49 days ago
    Back in the day I needed PDF export for some client thing. I can&#x27;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.<p>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&#x27;t have added 50 more?!<p>400 lines though. Respect. I will take a proper look at this when I recover from burnout :)
  • anilgulecha49 days ago
    Great exercize, but for most use cases - people will continue reaching for jsPDF.<p>I think if you have a markdown-&gt;PDF function included, where I can send in markdown and get PDF, that would solve quite many needs, and would be useful.
    • lulzx49 days ago
      I have added it!
      • nebezb49 days ago
        Wow you’re not kidding. That was fast.<p><a href="https:&#x2F;&#x2F;github.com&#x2F;Lulzx&#x2F;tinypdf&#x2F;commit&#x2F;961e6b602f19e125f210500812aa81e49de1ac41" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;Lulzx&#x2F;tinypdf&#x2F;commit&#x2F;961e6b602f19e125f210...</a>
      • pylotlight49 days ago
        HTML -&gt; PDF was also a use case I&#x27;ve used previously FYI
  • raybb49 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.<p>Here&#x27;s the TS library: <a href="https:&#x2F;&#x2F;github.com&#x2F;Myriad-Dreamin&#x2F;typst.ts" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;Myriad-Dreamin&#x2F;typst.ts</a>
  • wg049 days ago
    So essentially - it only works with Latin script? Because without fonts, every other script is NOT going to render.
    • jbaiter48 days ago
      Agree, the lack of support for TTF fonts is a bummer for most non-english use cases:-&#x2F;
  • copypaper49 days ago
    Nice work! I&#x27;m curious though, what was your use case for needing a smaller library? Since you&#x27;re running this on a server, what difference does an extra 226KB make?
    • lulzx48 days ago
      on cloudflare workers, the bundle limit makes 226KB massive.
      • kentonv48 days ago
        FWIW, the limit these days is 10MB (after compression).<p><a href="https:&#x2F;&#x2F;developers.cloudflare.com&#x2F;workers&#x2F;platform&#x2F;limits&#x2F;" rel="nofollow">https:&#x2F;&#x2F;developers.cloudflare.com&#x2F;workers&#x2F;platform&#x2F;limits&#x2F;</a><p>That said, thank you for fighting library bloat! Small libraries tend to perform better (not just on Workers but in general).
  • IntelliAvatar50 days ago
    3KB is wild. What features did you intentionally leave out to get this small?
    • wonger_49 days ago
      Not the author, but generating PDFs is much, much simpler than parsing PDFs
      • IntelliAvatar48 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.<p>Would be interesting to see a concrete “not supported” list from the author.
    • lysace49 days ago
      Support for more than 7-bit ASCII characters. :)
    • teaearlgraycold49 days ago
      It only supports Helvetica!
    • hu348 days ago
      utf-8
  • niutech41 days ago
    Why not generate good old RTF files instead of PDF? They are much simpler and support more than ASCII charset.
  • userbinator49 days ago
    I still have a tiny DOS binary (x86 Asm) that I wrote decades ago for turning plaintext ASCII files into PDFs, for those annoying use-cases where the former isn&#x27;t accepted but the latter is. It&#x27;s only a few hundred bytes, with the majority being data to be copied verbatim into the output file.
  • dzrmb49 days ago
    I actually was battling jsPDF the other day so definitely need to give this a try, thanks!
  • nanis48 days ago
    HTML + CSS works great for this kind of thing. Once you get the print scope correct, you really never need to think about it again.
  • winterec49 days ago
    Great work thanks for sharing. I&#x27;ve been looking for something like this for generating invoice PDFs without bloat.
  • ErroneousBosh48 days ago
    Heh, no stars when I first looked and though &quot;hey I&#x27;ll star this&quot; and now 300 ;-)
  • alexpadula48 days ago
    Well ain’t that a useful 400 lines of code eh! Good work
  • croisillon49 days ago
    is it related to one of the other 10 products called TinyPDF?
    • esafak49 days ago
      Yes, obviously: it&#x27;s a tiny PDF library.