Ask HN: Why is the PDF format so inaccessible?
I am working on some typographical software that is supposed to generate PDFs at the end. It seems like there is no accessible information on how to do this. The PDF ISO specification is behind a paywall and has a dead link to a 2008 spec. There are open source converters like pandoc, but nothing that actually writes to PDF that I can find. Is there any resource that goes over the process of PDF generation?
108 comments
[ 2.6 ms ] story [ 169 ms ] threadImagemagick also writes to pdf I believe, but it may only convert raster images. With postscript you can generate a vector pdf
However, PDF is a commonly used format.
When I wanted to generate PDF (or other formats such as PNG), I just wrote a PostScript program to do (and then run it through Ghostscript). (Drivers could also be added to make other output formats too if wanted.)
The open PDF standard now costs 250 USD. Adobe is supposed to have an archive of the 1.7 spec online but they do not care enough to keep that up it appears.
I am trying to think of a reason why they would do such a blindly dumb thing but the C++ people used to do the same thing.
From the document:
This document you are now reading is a copy of the ISO 32000-1 standard. By agreement with ISO, Adobe Systems is allowed to offer this version of the ISO standard as a free PDF file on it's web site. It is not an official ISO document but the technical content is identical including the section numbering and page numbering.
I include that, so you can Google up a copy when the URL changes again. (Copy is disabled, I had to retype it. The misuse of "it's" is present in the original.)
As afar as I know, they have always posted the PDF format for free. ISO’s business model is different, they pay the bills by selling the documents.
For generating PDF, assuming you aren’t some sort of sociopath that wants to embed JavaScript or some custom plugin, then you can just drop back to 1.3 or so and deal with a simpler spec. Use the parts you need, ignore the rest.
Some time around 2000 I wrote a PDF generator to do my type setting, so that was 1.2 or 1.3. Very straightforward format.
· If you're embedding ICC color profiles, you need to be using at least PDF version 1.3, which came out in 01999.
· If you're embedding lossily-compressed raster graphics like JPEGs, while you can always improve the print quality further by switching to losslessly-compressed graphics like PNGs, you may be able to get better quality at a given filesize by using better lossy compression algorithms like JPEG-2000 or (for bilevel images) JBIG2. JPEG-2000 support was added in PDF 1.5 and is excluded from, I think, PDF/A. JBIG2 support was added in PDF 1.4, and also includes a lossless format.
I don't know of any features in 2.0 that would improve print quality over 1.5 in any way.
https://en.m.wikipedia.org/wiki/PDF/A
The spec is 756 pages. For the 2008 spec. How is that very straightforward? My god.
Even the 2003 version clocks in at 696 pages and apparently includes "Interactive Forms", "Movies" and "Sounds".
Here's the link https://web.archive.org/web/20101214132912/http://partners.a...
How is that _straightforward_?
I do agree that writing a reader would be brutal.
I have the impression you have no direct contact, experience, or first-hand knowledge with PDF.
If age mattered (which it doesn't) then PDF's latest update was published on 2020, which is far fresher than XML's spec.
Nevertheless, it's absurd to compare a document format with a markup language. at most, you should compare ooxml with PDF, if that comparison mattered. If it did then you'd certainly be surprised when you'd discover that PDF is far simpler and more readable and easier to reason about than ooxml+XML.
Nevertheless what makes PDF complex is that it has about a dozen versions which support everything and the kitchen sink, including incremental document updates which can also be comprised of ad-hoc version updates.
Standards are dragged down by their oldest version, not the newest.
EDIT: Sorry, that was a nice punchline but didn't actually explain very well. My point is that unless they actually start over, which almost never happens, newer versions tend to just be more layers of stuff to deal with. It's not just a "who has the most recent revision date", but "who has the least 'interesting' historical baggage baked into the spec".
Not really, specially if you keep in mind that newer standards have erratas and newer versions, like PDF 2.0, deprecate features.
Not saying they don't have adoption, but to this day you are debilitatingly limiting yourself if you ignore the older ones, and their cruft.
The ReportLab APIs mirror the PDF file structure relatively closely.
Don't listen to the people who are nattering on about how PDF is proprietary on purpose. I think that may have been the case in its early years but it hasn't been the case this millennium.
PDF 1.7 (the spec from 02008) and even earlier verions are most often used, as you'll see if you run head -1 *.pdf in a directory with a lot of random PDFs. PDF 2.0 is not important and you may want to intentionally write an earlier version for broader compatibility. The big incompatibility is actually PDF 1.5 to 1.6: 1.6 added compressed object streams, and a lot of readers still don't support those.
______
¹ https://github.com/kragen/dercuano/blob/master/genpdf.py
² http://canonical.org/~kragen/sw/laserboot/Makefile
Couple years ago I needed to generate PDF reports, relatively complicated ones: headers/footers/backgrounds, page numbers, complex tables, jpeg bitmaps, custom vector graphics in diagrams, etc. This one did the job: https://www.nuget.org/packages/iTextSharp-LGPL
I don't think Pandoc knows anything about the PDF format. It can't read it https://github.com/jgm/pandoc/tree/master/src/Text/Pandoc/Re... or write it https://github.com/jgm/pandoc/tree/master/src/Text/Pandoc/Wr.... It uses other tools to do that.
Also it is really not that cryptic but very much laborious, hence many people rely on classical tools to generate PDF instead of handcrafting pdf files from scratch. Here is a nice introduction from a decade ago for you https://blog.idrsolutions.com/2010/09/grow-your-own-pdf-file...
Not directly answering your question, but I suppose the solution is to just pick the closest thing and convert. HTML&CSS being the most full-featured/generic. Markdown simplest for basic 'word processing'. Latex good for more advanced such cases. Images good for others. Maybe ePub would suit your 'typographical' needs (I think it's a lot more open than PDF, and itself HTML based)?
PDFs specify fixed format. You need some out-of-band info to generate PDFs from HTML.
> Maybe ePub would suit your 'typographical' needs (I think it's a lot more open than PDF, and itself HTML based)?
EPub is basically a zip with HTML+CSS+some metadata. Newer versions were based on HTML5 while older ones were based on XHTML.
https://web.archive.org/web/*/https://www.adobe.com/content/...
Edit: Nevermind, see this comment: https://news.ycombinator.com/item?id=31267227
There’s also this book which provides a good introduction and overview and is useful for understanding how the format works (although the PDF reference itself is pretty decent too, as far as specs go): https://www.oreilly.com/library/view/developing-with-pdf/978... (You can find a PDF copy if you look around.) EDIT: There’s also https://www.oreilly.com/library/view/pdf-explained/978144932... which might be even better.
However, be warned that the PDF format can be quite complex and is not exactly for the faint of heart. It’s best to use an established library to generate PDF output, like PDFBox, iText, PDFSharp, PDFKit, etc. Those tend to have their own tutorials.
For emphasis: Do not generate PDFs “by hand”! You risk inadvertently generating PDFs that do not fully conform to the spec, and not noticing it because PDF readers are quite lenient in what they accept. A lot of PDFs in the wild are not standard-conforming in some way or other, because their generators were not carefully written against the spec, but against “whatever Acrobat Reader accepts”. This is the bane of every software on the receiving end that needs to process PDFs.
That said, if you're only writing and not reading existing ones, it's as straightforward or as complex as you want to make it. I wrote a little program many years ago to convert plaintext to PDF in <1kLoC of C --- and its output was actually often many times smaller than what the commercial PDF-generators do, because I just used the defaults and bare-minimum necessary. I wrote it after being confronted with a requirement to use PDF, and the output of contemporary generators seemed rather bloated in comparison. The spec has an example of a bare-minimum; it's quite easy to programmatically generate.
I'm afraid that's the old 2008 spec, which specifies versions up to PDF 1.7.
The current standard is ISO 32000-2, released in 2020, which specifies PDF-2.0. I'd love to get my hands on it, but alas it's paywalled.
This page lists the major changes: https://www.loc.gov/preservation/digital/formats/fdd/fdd0004...
The FDIS of PDF 2.0 is available here for those who are curious: https://cdn.standards.iteh.ai/samples/75839/ad216d84afd34f96...
I don’t know if there are any significant changes between the FDIS and the published version, but it’s better to assume there may be some.
Instead, write a conformant one. Better, write one that not only conforms, but also isn't affected by any of the bugs in popular PDF renderers, by testing against all of them. Shawn Davis at LevelUp Research, working on the same DARPA project I'm currently on, has written the amazing SPARCLUR https://youtu.be/6I6E1N3CJzQ (no sound) https://github.com/levelupresearch/sparclur https://pypi.org/project/sparclur/ which will feed your test PDF to Ghostscript, MuPDF, PDFium, PDFMiner, Poppler, QPDF, Xpdf, and some other PDF engines, and compare the results. That way you can see not only if any of them produce errors and warnings, but also if they render it differently or extract different text from it. SPARCLUR is Apache-licensed, written in Python, and very well integrated with Jupyter.
(We've developed some other tools for this as well, but they're not as accessible.)
SPARCLUR doesn't test your PDF against Adobe's implementation, or for that matter Foxit, and I don't remember why.
But basically if you're going to generate PDF files you might as well feed them to SPARCLUR's Spotlight and automatically verify that they work identically, or near enough, in half a dozen independently implemented PDF renderers. Hopefully SPARCLUR will dramatically improve the software quality and compatibility of future PDF generators.
Hard to automate, Windows, macOS and Linux versions have differences
> Foxit
No Linux version, even harder automation than Adobe's
My top tip for understanding PDFs is to take one that you have a decompress it then open with a text editor.
What you'll find is very approachable for a developer. It's a tree of nodes of different types. Some are dictionaries, some are streams of other data. All of them are documented in the spec. There's all sorts of wonderful corners, like spot printing colors.PDFs are actually ok, wait till you dig into the fonts. Now there's the real dark art of the ancients.
When did this happen? I swear some years ago I looked and it was a $300 standard?
We have that with websites too. Sometimes it is even hard to spot an error like a missing tag because browsers just assume (often correctly) that it was missing in the first place. But yes, probably also a reason nobody wants to write new browser engines.
Can you comply with a subsection of the specification?
I guess my point is that nobody will care to create an app with attention to the hundreds/thousands of programming tasks required for full compliance with a 600 page specification...
https://github.com/foliojs/pdfkit
As to why it’s so inaccessible…because Adobe created this monstrosity to do just about everything. Text, fonts, vector graphics, raster graphics, forms, color spaces, JavaScript, encryption, signatures, 3D artwork, video, audio, Flash, and probably more. It’s bonkers as to what it can possibly include, and it was developed during a way different time.
Sanity has prevailed and a lot of that just doesn’t work anymore, but IIRC Adobe wanted PDF to be “the” file interchange format.
(A lot of those “interactive” features are exposed in Adobe InDesign, hence my passing, regrettable familiarity.)
This part of the reason it's such a security nightmare.
https://github.com/jchv/resume/blob/master/journal.md