Live data from Hacker News

So you want to parse a PDF?

eliot-jones.com

181–190 of 236 posts

Re: So you want to parse a PDF?

#181
post #57

The answer seems obvious to me: 1. PDFs support arbitrary attached/included metadata in whatever format you like. 2. So everything that produces PDFs should attach the same information in a machine-friendly format. 3. Then everyone who wants to "parse" the PDF can refer to the metadata instead. From a practical standpoint: my first name is Geoff. Half the resume parsers out there interpret my name as "Geo" and "ff" s…

That “obvious solution” is very reminiscent of https://xkcd.com/927/.

And, as a sibling notes, it opens up the failure case of the attached data not matching the rendered PDF contents.

Re: So you want to parse a PDF?

#182

Earlier quoted context omitted.

That is a really interesting idea. Did some napkin math: Consumer printers can reliably handle 300 Dots Per Inch (DPI). Standard letter paper is 8.5” x 11” and we need a 0.5” margins on all sides to be safe. This gives you a 7.5” x 10” printable area, which is 2250 x 3000 Dots. Assume 1 Dot = 1 QR Code module (cell) and we can pack 432 Version 26 QR codes onto the page (121 modules per side; 4 modules quiet space buf…

Also... many PDFs today are not intended to ever meet a dead tree. If that's the case you can put pretty high DPI QR codes there without issue.

Hmm you could do a bunch of crazy stuff if you assume it will stay digital.

You could have an arbitrarily large page size. You could use color to encode more data… maybe stack QR codes using each channel of a color space (3 for RGB, 4 for CMYK)

There are interesting accessibility and interoperability trade offs. If it’s print-ready with embedded metadata, you can recover the data from a printed page with any smart phone. If it’s a 1 inch by 20 ft digital page of CMKY stacked QR codes, you’ll need some custom code.

Playing “Where’s Waldo” with a huge field of QR codes is probably still way more tractable than handling PDF directly though!

Re: So you want to parse a PDF?

#183
post #15

Disclaimer - Founder of Tensorlake, we built a Document Parsing API for developers. This is exactly the reason why Computer Vision approaches for parsing PDFs works so well in the real world. Relying on metadata in files just doesn't scale across different source of PDFs. We convert PDFs to images, run a layout understanding model on them first, and then apply specialized models like text recognition and table recogn…

It might sound absurd, but on paper this should be the best way to approach the problem. My understanding is that PDFs are intended to produce an output that is consumed by humans and not by computers, the format seems to be focused on how to display some data so that a human can (hopefully) easily read them. Here it seems that we are using a technique that mimics the human approach, which would seem to make sense. I…

> It might sound absurd, but on paper this should be the best way to approach the problem.

On paper yes, but for electronic documents? ;)

More seriously: PDF supports all the necessary features, like structure tags. You can create a PDF with the basically the same structural information as an HTML document. The problem is that most PDF-generating workflows don’t bother with it, because it requires care and is more work.

And yes, PDF was originally created as an input format for printing. The “portable” in “PDF” refers to the fact that, unlike PostScript files of the time (1980s), they are not tied to a specific printer make or model.

Re: So you want to parse a PDF?

#184
post #57

The answer seems obvious to me: 1. PDFs support arbitrary attached/included metadata in whatever format you like. 2. So everything that produces PDFs should attach the same information in a machine-friendly format. 3. Then everyone who wants to "parse" the PDF can refer to the metadata instead. From a practical standpoint: my first name is Geoff. Half the resume parsers out there interpret my name as "Geo" and "ff" s…

Your Geoff problem could be solved easily by not putting the ligature into the PDF in the first place. You don't need the cooperation of the entire rest of the world (at the cost of hundreds of millions of dollars) to solve that one little problem that is at most a tiny inconvenience.

Re: So you want to parse a PDF?

#185
post #15

Disclaimer - Founder of Tensorlake, we built a Document Parsing API for developers. This is exactly the reason why Computer Vision approaches for parsing PDFs works so well in the real world. Relying on metadata in files just doesn't scale across different source of PDFs. We convert PDFs to images, run a layout understanding model on them first, and then apply specialized models like text recognition and table recogn…

I would like to add the ability to import data tables from PDF documents to my data wrangling software (Easy Data Transform). But I have no intention of coding it myself. Does anyone know of a good library for this? Needs to be:

-callable from C++

-available for Windows and Mac

-free or reasonable 1-time fee

Re: So you want to parse a PDF?

#186
post #57

The answer seems obvious to me: 1. PDFs support arbitrary attached/included metadata in whatever format you like. 2. So everything that produces PDFs should attach the same information in a machine-friendly format. 3. Then everyone who wants to "parse" the PDF can refer to the metadata instead. From a practical standpoint: my first name is Geoff. Half the resume parsers out there interpret my name as "Geo" and "ff" s…

Your Geoff problem could be solved easily by not putting the ligature into the PDF in the first place. You don't need the cooperation of the entire rest of the world (at the cost of hundreds of millions of dollars) to solve that one little problem that is at most a tiny inconvenience.

That's right, and all the Günters, Renées and Þórunns out there can just change their names to Gunter, Renee and Thorunn.

Re: So you want to parse a PDF?

#187
post #15

Disclaimer - Founder of Tensorlake, we built a Document Parsing API for developers. This is exactly the reason why Computer Vision approaches for parsing PDFs works so well in the real world. Relying on metadata in files just doesn't scale across different source of PDFs. We convert PDFs to images, run a layout understanding model on them first, and then apply specialized models like text recognition and table recogn…

While we have a PDF internals expert here, I'm itching to ask: Why is mupdf-gl so much faster than everything else? (on vanilla desktop linux) Its search speed on big pdfs is dramatically faster than everything else I've tried and I've often wondered why the others can't be as fast as mupdf-gl. Thanks for any insights!

It's funny you ask this - i have spent a time building pdf indexing/search apps on the side over the past few weeks.

I'll give you the rundown. The answer to your specific question is basically "some of them process letter by letter to put text back in order, and some don't. Some build fast trie/etc based indexes to do searching, some don't"

All of my machine manuals/etc are in PDF, and too many search apps/OS search indexers don't make it simple to find things in them. I have a really good app on the mac, but basically nothing on windows. All i want is a dumb single window app that can manage pdf collections, search them for words, and display the results for me. Nothing more or less.

So i built one for my non-mac platforms over the past few weeks. One version in C++ (using QT), one version in .net (using MAUI), for fun.

All told, i'm indexing (for this particular example), 2500 pdfs that have about 150k pages in them.

On the indexing side, lucene and sqlite FTS do a fine job, and no issues - both are fast, and indexing/search is not limited by their speed or capability.

On the pdf parsing/text extraction side, i have tried literally every library that i can find for my ecosystem (about 25). Both commercial and not. I did not try libraries that i know share underlying text extraction/etc engines (IE there are a million pdfium wrappers).

I parse in parallel (IE files are processed in parallel) , extract pages in parallel (IE every page is processed in parallel), and index the extracted text either in parallel or in batches (lucene is happy with multiple threads indexing, sqlite would rather have me do it sequentially in batches).

The slowest libraries are 100x slower than the fastest to extract text. They cluster, too, so i assume some of them share underlying strategies or code despite my attempt to identify these ahead of time. The current Foxit SDK can extract about 1000-2000 pages per second, sometimes faster, and things like pdfpig, etc can only do about 10 pages per second.

Pdfium would be as fast as the current foxit sdk but it is not thread safe (I assume this is because it's based on a source drop of foxit from before they added thread safety), so all calls are serialized. Even so it can do about 100-200 pages/second.

Memory usage also varies wildly and is uncorrelated with speed (IE there are fast ones that take tons of memory and slow ones that take tons of memory). For native ones, memory usage seems more related to fragmentation than it does it seems related to dumb things. There are, of course, some dumb things (one library creates a new C++ class instance for every letter)

From what i can tell digging into the code that's available, it's all about the amount of work they do up front when loading the file, and then how much time they take to put the text back in content order to give me.

The slowest are doing letter by letter. The fastest are not.

Rendering is similar - some of them are dominated by stupid shit that you notice instantly with a profiler. For example, one of the .net libraries renders to png encoded bitmaps by default, and between it and windows, it spends 300ms to encode/decode it to display. Which is 10x slower than it rasterized it. If i switch it to render to bmp instead, it takes 5ms to encode/decode it (for dumb reasons, the MAUI apis require streams to create drawable images). The difference is very noticeable if i browse through search results using the up/down key.

Anyway, hopefully this helps answer your question and some related ones.

Re: So you want to parse a PDF?

#188
post #57

The answer seems obvious to me: 1. PDFs support arbitrary attached/included metadata in whatever format you like. 2. So everything that produces PDFs should attach the same information in a machine-friendly format. 3. Then everyone who wants to "parse" the PDF can refer to the metadata instead. From a practical standpoint: my first name is Geoff. Half the resume parsers out there interpret my name as "Geo" and "ff" s…

So what you're saying is: the solution to PDF parsing is make a new file format altogether lol. Very helpful.

Re: So you want to parse a PDF?

#189

Earlier quoted context omitted.

Seems like a fairly reasonable decision given all the high quality implementations out there.

How is it reasonable to render the PDF, rasterize it, OCR it, use AI, instead of just using the "quality implementation" to actually get structured data out? Sounds like "I don't know programming, so I will just use AI".

I just spent a few weeks testing about 25 different pdf engines to parse files and extract text.

Only three of them can process all 2500 files i tried (which are just machine manuals from major manufacaturers, so not highly weird shit) without hitting errors, let alone producing correct results.

About 10 of them have a 5% or less failure rate on parsing the files (let alone extracting text). This is horrible.

It then goes very downhill.

I'm retired, so i have time to fuck around like this. But going into it, there is no way i would have expected these results, or had time to figure out which 3 libraries could actually be used.

Re: So you want to parse a PDF?

#190

Earlier quoted context omitted.

Your Geoff problem could be solved easily by not putting the ligature into the PDF in the first place. You don't need the cooperation of the entire rest of the world (at the cost of hundreds of millions of dollars) to solve that one little problem that is at most a tiny inconvenience.

That's right, and all the Günters, Renées and Þórunns out there can just change their names to Gunter, Renee and Thorunn.

I don't think any of those uses a ligature. Ü, é and Þ are distinct characters in legacy latin-1 and in Unicode. It wouldn't surprise me if non-scandinavian websites do not like Þ, however.

It's probably not PDF's fault that parsers are choking on the ff ligature. Changing all those parsers isn't practical, and Adobe can't make that happen.

Finally, if you run based on metadata that isn't visible, you open up to a different kind of problem, where a visual inspection of the PDF is different from the parsed data. If I'm writing something to automatically classify PDFs from the wild, I want to use the visible data. A lot of tools (such as Paperless) will ocr a rasterized pdf to avoid these inconsistencies.

Post reply on HN