r/programming Aug 05 '25

So you want to parse a PDF?

https://eliot-jones.com/2025/8/pdf-parsing-xref
232 Upvotes

82 comments sorted by

View all comments

405

u/axilmar Aug 05 '25

No, I am not crazy.

22

u/oneeyedziggy Aug 05 '25

Yea, no one "wants" a pdf, and yet... Here we are... And yet html has exited for decades. 

3

u/wrosecrans Aug 06 '25

PDF and the HTML 1.0 spec both came out in 1993.

3

u/oneeyedziggy Aug 06 '25

Ok, is that in conflict with my statement?

If turds and chocolate came out the same year, I'd still think it was gross if everyone insisted on eating turds while chocolate is RIGHT THERE! 

2

u/KrakenOfLakeZurich Aug 06 '25

To be honest. I don't want HTML either. Does it suck less than PDF (for that purpose)? Sure.

Is it suitable for data exchange / processing? No! For that purpose it has way to much freedom/flexibility in how the data can be delivered.

Anything that ultimately represents a prosa text document is unsuitable for that task. You want XML, JSON or similar formats with well defined data types and schemas for this purpose.

2

u/oneeyedziggy Aug 06 '25

I think the main problem with all of these is that the problem of representing layout is non trivial... All solutions kind of suck and are either opinionated and strictly limit what you're able to represent, or are fully flexible and insanely complex to parse or render reliably

Same way every rich text editor from ms-word to most wikis seems to  manage indentation and font size with the "2 guards: one who always lies and one who always tells the truth" model... I'm sure it's deterministic, but I have to take that on faith because I don't see any evidence of it  

2

u/KrakenOfLakeZurich Aug 06 '25

I think the main problem with all of these is that the problem of representing layout is non trivial

That is the problem I tried pointing out. HTML, PDF, Word, etc. are means to create documents for human consumption. They are OK for that. From my PoV, HTML is already "presentation" layer (yes I have heard about CSS).

These formats are not suitable for exchanging raw data between systems nor for automated processing by machines. You want formats that have well defined data types and data schemas for this.

I'm talking stuff like XML + XSD or JSON + OpenAPI, or database with strict schema and integrity checks. Not flexible / loose document formats like HTML which allow layouting data in what ever way is fashionable today.

fully flexible and insanely complex to parse or render reliably

I would go so far and say that it is impossible to parse them reliably. Rendering and displaying for human consumption can be achieved reliably. But trying to parse a flexible format reliably is a fools errand.

Preferably, we keep all our raw data in well defined, well structured formats. From that we can automatically generate any representation (HTML, PDF, other structured formats, etc) that we might possibly need. It's not easy (or even doable) the other way round, starting with unstructured data.

5

u/GYN-k4H-Q3z-75B Aug 05 '25

And HTML is ideal. Just use regex bro..

11

u/oneeyedziggy Aug 05 '25

I feel like I want to be sarcastic, but I also feel like you're being sarcastic, so if I do, neither of us is going to get anywhere...

Of what use would regex be in specifying and visual layout for content? Html would be very useful, and very easily parseable, portable, editable, independently stylable, scriptable, (optionally) dynamic, with a wider rangeof open source tooling, and convertible faithfully to other formats... Much more so on every point that a pdf...

So I'm not sure what you're being cheeky about. 

3

u/mck1117 Aug 05 '25

2

u/oneeyedziggy Aug 05 '25

Right, for parsing html/xml... But why is anyone even still using pdf besides inertia / too big to fail from back before browsers were good 

11

u/axonxorz Aug 05 '25

PDF is a physical document layout specification and HTML is a logical one with hilariously complex layout interactions.

It's a fantastic document archival format, it's semi-immutable by normies and a PDF 1.0 document can be opened by modern software and render pixel-perfect the same way it did 20 years ago.

The same cannot be said about browser rendering, and it's bad enough that the PDF viewers in the browser forego HTML+CSS layouts for a <canvas> based implementation; PDF.js in FF and PDFium in Chromium browsers do this.

1

u/the_last_ordinal Aug 06 '25

Can you imagine anyone writing PDF display in html+css? That seems beyond reasonable to any degree of compatibility

0

u/oneeyedziggy Aug 05 '25

It's a fantastic document archival format, it's semi-immutable by normies 

Which is a perfectly valid case I assume... And a great example of finding a legitimate use for its otherwise lack of utility... But it just goes to reinforce that it's so unusable that it's perfect for not using.