News for hackers. Stuff that matters.
Re:PDF to Text, a Challenging Problem by 90s_dev on Tuesday May 13, 2025 @02:18PM (#43975996)
Have any of you ever thought to yourself, this is new and interesting, and then vaguely remembered that you spent months or years becoming an expert at it earlier in life but entirely forgot it? And in fact large chunks of the very interesting things you've done just completely flew out of your mind long ago, to the point where you feel absolutely new at life, like you've accomplished relatively nothing, until something like this jars you out of that forgetfulness?

I definitely vaguely remember doing some incredibly cool things with PDFs and OCR about 6 or 7 years ago. Some project comes to mind... google tells me it was "tesseract" and that sounds familiar.

Re:PDF to Text, a Challenging Problem by bazzargh on Tuesday May 13, 2025 @04:10PM (#43977155)
Back in... 2006ish? I got annoyed with being unable to copy text from multicolumn scientific papers on my iRex (an early ereader that was somewhat hackable) so dug a bit into why that was. Under the hood, the pdf reader used poppler, so I modified poppler to infer reading order in multicolumn documents using algorithms that tessaract's author (Thomas Breuel) had published for OCR.

It was a bit of a heuristic hack; it was 20 years ago but as I recall poppler's ancient API didn't really represent text runs in a way you'd want for an accessibility API. A version of the multicolumn select made it in but it was a pain to try to persuade poppler's maintainer that subsequent suggestions to improve performance were ok - because they used slightly different heuristics so had different text selections in some circumstances. There was no 'right' answer, so wanting the results to match didn't make sense.

And that's how kpdf got multicolumn select, of a sort.

Using tessaract directly for this has probably made more sense for some years now.

Re:PDF to Text, a Challenging Problem by steeeeeve on Tuesday May 13, 2025 @05:53PM (#43978191)
I too went down that rabbithole. Haha. Anything around that time to get an edge in a fantasy football league. I found a bunch of historical NFL stats pdfs and it took forever to make usable data out of them.
Re:PDF to Text, a Challenging Problem by korkybuchek on Tuesday May 13, 2025 @03:38PM (#43976796)
Not that I'm privy to your mind, but it probably was tesseract (and this is my exact experience too...although for me it was about 12 years ago).
Re:PDF to Text, a Challenging Problem by downboots on Tuesday May 13, 2025 @02:27PM (#43976086)
No different than a fire ant whose leaf got knocked over by the wind and it moved on to the next.
Re:PDF to Text, a Challenging Problem by 90s_dev on Tuesday May 13, 2025 @03:42PM (#43976839)
Well I sure do feel different than a fire ant.
Re:PDF to Text, a Challenging Problem by downboots on Tuesday May 13, 2025 @03:54PM (#43976974)
anttention is all we have
Re:PDF to Text, a Challenging Problem by 90s_dev on Tuesday May 13, 2025 @03:57PM (#43977013)
Not true, I also have a nice cigar waiting for the rain to go away.
Re:PDF to Text, a Challenging Problem by 90s_dev on Tuesday May 13, 2025 @06:37PM (#43978537)
Hmm, it's gone now. Well I used to have one anyway.
Re:PDF to Text, a Challenging Problem by pimlottc on Tuesday May 13, 2025 @04:51PM (#43977580)
This is life. So many times I’ve finished a project and thought to myself: “Now I am an expert at doing this. Yet I probably won’t ever do this again.” Because the next thing will completely in a different subject area and I’ll start again from the basics.
Re:PDF to Text, a Challenging Problem by didericis on Tuesday May 13, 2025 @04:53PM (#43977605)
I built an auto HQ solver with tesseract when HQ was blowing up over thanksgiving (HQ was the gameshow by the vine people with live hosts). I would take a screenshot of the app during a question, share it/send it to a little local api, do a google query for the question, see how many times each answer on the first page appeared in the results, then rank the answers by probability.

Didn't work well/was a very naive way to search for answers (which is prob good/idk what kind of trouble I'd have gotten in if it let me or anyone else who used it win all the time), but it was fun to build.

Re:PDF to Text, a Challenging Problem by anon373839 on Tuesday May 13, 2025 @05:51PM (#43978171)
Tesseract was the best open-source OCR for a long time. But I’d argue that docTR is better now, as it’s more accurate out of the box and GPU accelerated. It implements a variety of different text detection and recognition model architectures that you can combine in a modular pipeline. And you can train or fine-tune in PyTorch or TensorFlow to get even better performance on your domain.
Re:PDF to Text, a Challenging Problem by TZubiri on Tuesday May 13, 2025 @08:11PM (#43979324)
It's still fresh with me, 7 or 8 years ago in my 20s, perhaps you are a bit older? Otherwise wouldn't hurt to do a checkup with a physician
Re:PDF to Text, a Challenging Problem by 90s_dev on Tuesday May 13, 2025 @09:16PM (#43979701)
No I'm just good at forgetting things, I've been practicing since I was a kid.
Re:PDF to Text, a Challenging Problem by LordGrignard on Tuesday May 13, 2025 @10:09PM (#43979997)
OH. MY. GOD. THATS SO COOL, IM STEALING THAT
Re:PDF to Text, a Challenging Problem by hallman76 on Tuesday May 13, 2025 @10:32PM (#43980118)
We will never get back the collective man-decades of time that has been burned by this format. When will the madness stop?
Re:PDF to Text, a Challenging Problem by xvilka on Wednesday May 14, 2025 @12:06AM (#43980692)
The solution always has been in plain sight - just make XML-based format. Nobody liked it, except OpenDocument and eventually Microsoft. Though these formats serve different purpose, new, similar one, could be created with picture-perfect features.
Re:PDF to Text, a Challenging Problem by theamk on Tuesday May 13, 2025 @10:54PM (#43980258)
When we get an alternative that can:

(1) be stored in a single file

(2) Allow tables, images and anything else that can be shown on a piece paper

(3) Won't have animation, fold-out text, or anything that cannot be be shown on a piece of paper

(4) won't require Javascript or access to external sites

that means never.. We've got lucky we at least got PDF before "web designers" made (3) impossible, and marketers made (4) impossible

Re:PDF to Text, a Challenging Problem by protocolture on Tuesday May 13, 2025 @11:47PM (#43980591)
Behold a Bitmap.

But for real, thats a pretty easy set of hurdles. Really the barrier is the psychological fallacy that PDF's are immutable.

Re:PDF to Text, a Challenging Problem by theamk on Wednesday May 14, 2025 @12:09AM (#43980708)
[delayed]
Re:PDF to Text, a Challenging Problem by protocolture on Wednesday May 14, 2025 @12:33AM (#43980842)
Word can edit pretty much any pdf these days, the issue is that it will often garble the attempt.
Re:PDF to Text, a Challenging Problem by imtringued on Wednesday May 14, 2025 @05:34AM (#43982603)
PDFs are not really immutable. I use Okular all the time to write my "notes" (it's just text that you can place anywhere) on top of a PDF form and then print out a new completely filled out PDF. The only thing I do by hand is sign the physical paper.
Re:PDF to Text, a Challenging Problem by iLemming on Wednesday May 14, 2025 @09:54PM (#43991100)
Your understanding of immutability feels skewed here. Every time you annotate the PDF, it creates a new version. Even when you overwrite the same file, the structure of the original document changes, therefore creating a new document, ultimately making it "the ship of Theseus.pdf"

Sure, someone may try using the same argument, applying it to .doc and .txt documents, yet there is a general consensus saying that pdfs were designed to "resist the change". You can probably self-illustrate the point by making changes to a .txt document and then removing your changes - the md5 of the file would remain the same.

Re:PDF to Text, a Challenging Problem by me-vs-cat on Thursday May 15, 2025 @08:57PM (#44000833)
Have you ever used Acrobat? Not "Acrobat Reader", but regular Acrobat, the most popular PDF editor. It's from Adobe, and it definitely does not "resist" edits.
Re:PDF to Text, a Challenging Problem by iLemming on Thursday May 15, 2025 @09:39PM (#44001063)
I got what you're saying the first time, and you still seem to be entirely missing the point. Immutability means that an object cannot be modified after it's created, and any changes result in a new object rather than altering the original.

You're saying "well, look, I can modify this pdf and I can even undo my changes...", what I'm saying is that whenever you modify a PDF, you're essentially creating a new file rather than truly "undoing" changes in the original. PDFs have complex internal structures with metadata, object references, and possibly compression that make bit-perfect restoration challenging.

Unlike plain text files where changes can be precisely tracked and reversed at the character level, PDFs don't easily support this kind of granular reversibility. Even "undoing" in PDF editors often means generating yet another variant rather than returning to the exact binary state of the original.

Take a look at how Git stores PDFs - when the delta approach doesn't work efficiently since even small logical changes can result in significantly different binary files with completely different checksums, it stores EVERY version of the same document in a separate blob object.

When you annotate a pdf and then later change your mind, undo all the annotations and save it — only to your eyes it may look the same as the original — in digital reality, it will be a different file.

Re:PDF to Text, a Challenging Problem by protocolture on Thursday May 15, 2025 @09:58PM (#44001184)
The people who act as if PDFs are legally immutable are not performing MD5 comparisons.

Also, that isnt even an intention of the file format as far as I can see, its mostly a byproduct of cruft and backwards compatibility.

No one would call .doc immutable because its very difficult to move an image and then restore that image to the original location.

In this context, people will save something out as pdf and store it because they dont think it cannot be modified.

But as has been rightly pointed out, thats not the case.

Re:PDF to Text, a Challenging Problem by iLemming on Thursday May 15, 2025 @10:27PM (#44001307)
I feel like I'm talking to a toddler, sigh. Let me try again.

Immutability doesn't mean that an "object cannot be modified", it means that in order to modify an object, you must create a new (clone) object. That's all what I meant to say. Sure, you can get pedantic or otherwise and say "yes, pdfs are immutable; or no, pdfs aren't immutable in some contexts", etc., and depending on the point of view, both of these claims could be correct — I'm not arguing about the specifics.

I'm just saying that your explanation of why you think pdfs are not immutable hinges on an incorrect idea of what immutability actually is.

There's a rigorous definition for "immutability" in computer science, e.g., strings in many programming languages are immutable, but that doesn't mean you can't manipulate them, it just means that operations that appear to modify strings actually create new string objects.

The greatest illustration for immutability is imbued in programming languages with immutability-by-default, e.g., Clojure. Once someone groks the basics, it becomes really clear what that thing is about.

Re:PDF to Text, a Challenging Problem by me-vs-cat on Friday May 16, 2025 @05:58PM (#44010138)
> I feel like I'm talking to a toddler, sigh.

Me too, but I'm done. Have fun!

Re:PDF to Text, a Challenging Problem by me-vs-cat on Friday May 16, 2025 @06:07PM (#44010198)
> I got what you're saying the first time,

That wasn't me. Multiple people were taking the time to help you understand.

Re:PDF to Text, a Challenging Problem by harshreality on Wednesday May 14, 2025 @06:07AM (#43982758)
What's immutable, without tools to decompress and possibly perform further de-obfuscation of text streams, is the typical way publishing software encodes text into streams inside PDFs.

It remains possible to have a pdf with text that is easily mutable with any text editor.

Even if text inside a pdf is annoyingly encoded, you can always just replace the appropriate object/text streams... if you can identify the right one(s). You can extract and edit and re-insert, or simply replace, embedded images as well.

I don't think "this format promotes, as the norm, so much obfuscation of basic text objects that it becomes impractical to edit them in situ without wholesale replacement" is the win you think it is.

"Looks good on paper" has to do with the rendering engine (largely high-DPI and good font handling/spacing/kerning), not PDF as a content layout/presentation format. A high-quality software rasterizer (for postscript or PDF, often embedded in the printer)—not the PDF file format—has been the magic sauce.

Today, some large portion of end-user interaction with PDFs is via rendering into a web browser DOM via javascript. Text in PDFs is rendered as text in the browser. Perhaps nothing else demonstrates more clearly that the "PDF is superior" argument is invalid.

Re:PDF to Text, a Challenging Problem by me-vs-cat on Wednesday May 14, 2025 @12:00PM (#43986093)
> you can always just replace the appropriate object/text streams

Or right-click and select Edit. Works in several PDF editors, on both text and image content.

Re:PDF to Text, a Challenging Problem by me-vs-cat on Wednesday May 14, 2025 @11:54AM (#43986015)
PDFs are not immutable.
Re:PDF to Text, a Challenging Problem by coderatlarge on Tuesday May 13, 2025 @11:27PM (#43980448)
Re:PDF to Text, a Challenging Problem by theamk on Wednesday May 14, 2025 @12:10AM (#43980715)
[delayed]
Re:PDF to Text, a Challenging Problem by drfuchs on Wednesday May 14, 2025 @03:53AM (#43982030)
DVI isn’t suitable as you’d still have to intuit where the paragraph- and even word-breaks are; what’s body text vs. headers/footers, sidebars, captions, etc; never mind what math expression a particular jumble of characters and rules came from.
Re:PDF to Text, a Challenging Problem by dqv on Wednesday May 14, 2025 @01:39AM (#43981173)
> (3) Won't have animation, fold-out text, or anything that cannot be be shown on a piece of paper

> (4) won't require Javascript or access to external sites

So about that... https://opensource.adobe.com/dc-acrobat-sdk-docs/library/jsa...

Re:PDF to Text, a Challenging Problem by lmz on Wednesday May 14, 2025 @06:47AM (#43982954)
Re:PDF to Text, a Challenging Problem by izacus on Wednesday May 14, 2025 @09:05AM (#43984064)
Did you miss the meaning of the word "require"?
Re:PDF to Text, a Challenging Problem by theamk on Thursday May 15, 2025 @03:08PM (#43998265)
that's the power of legacy. Adobe may think they can add junk to PDF like Javascript support, or lmz's "3D PDF" link below, but since PDFs viewers have a diverse ecosystem, those features won't have a great adoption.

And this is actually pretty great, maybe even the best part of PDFs! Companies _know_ that publishing PDF that require 3d-graphics or Javascript means many people won't be able to see them, so they publish good, static PDFs, maintaining virtuous cycle.

Re:PDF to Text, a Challenging Problem by gpvos on Wednesday May 14, 2025 @01:49AM (#43981225)
Postscript fits that bill better.
Re:PDF to Text, a Challenging Problem by numpad0 on Wednesday May 14, 2025 @03:09AM (#43981712)
(-1) be vector format that never gets pixelated

(0) that reproduce everywhere on any OS perfectly

(0.5) that supports (everything) any typographical engineers ever wanted past and future

Bitmap formats are out from clause -1, Office file formats disqualify from clause 0, Markdown doesn't satisfy clause 0.5. Otherwise a Word .doc format covers most of clauses 1-4.

Re:PDF to Text, a Challenging Problem by Timwi on Wednesday May 14, 2025 @03:52AM (#43982024)
> (0) that reproduce everywhere on any OS perfectly

Can somebody explain why this isn't the case for HTML? I'm frequently in a situation where a website that mimics printed pages fails to render the same between Firefox and Chrome. I wish to understand the primary culprit here. I thought all of the CSS units are completely defined?

Re:PDF to Text, a Challenging Problem by djxfade on Wednesday May 14, 2025 @06:07AM (#43982761)
Probably due to different font rendering in the OS.
Re:PDF to Text, a Challenging Problem by scajanus on Wednesday May 14, 2025 @08:12AM (#43983569)
I think this is the result of 1) it being a moving target and 2) HTML and CSS being a de facto standard rather than de jure, where the (differing) implementations define at least part of the spec.

You also can't really embed fonts in a HTML file, you rely on linking instead -- and those can rot. Apparently there has been some work towards it (base64 encoded), but support may vary. And you need to embed the whole font, I don't think you can do character subsets easily.

Re:PDF to Text, a Challenging Problem by ninalanyon on Wednesday May 14, 2025 @06:17AM (#43982818)
A subset of HTML and CSS surely does that to a large degree. Data urls solve the single file problem.
Re:PDF to Text, a Challenging Problem by majora2007 on Wednesday May 14, 2025 @09:38AM (#43984377)
Why can't this be done with epub? Single file, all files are packed within the zip, no javascript needed but can be included. Allows for markup and forms, just like pdf.
Re:PDF to Text, a Challenging Problem by currency on Wednesday May 14, 2025 @01:48PM (#43987318)
EPub is, like html, reformattable by the reader, so documents aren't fixed in the way PDFs are.
Re:PDF to Text, a Challenging Problem by GuB-42 on Wednesday May 14, 2025 @09:48AM (#43984513)
PDF is effectively digital paper, and it works really well for this. When I made PDFs 20 years ago, I knew they will always look the same on every device, including on paper, and they did, and they still do. In addition, a document is a single file, reasonably compact, looks good on any resolution, and is generally searchable. Even if not ideal, it can also support scans of paper documents in a way that can be sent to a printer on the other side of the planet and you will get the same result as if you had used a copier.

Data extraction is hard, but that's not what it is designed for, it is for people to read, like paper documents.

Far from being "mad", it is remarkably stable. It has some crazy features, and it is not designed for data extraction (but doesn't actively prevent it!). But look at the alternative. Word documents? Html? Svg? One of the zillion XML-based document formats? Markdown? Is any one of these suitable for writing, say, a scientific paper (with maths, tables, graphics...) in a way that is readable by a human on a computer or in print and will still be in decades and that is easier to process by a machine than a PDF?

Re:PDF to Text, a Challenging Problem by selcuka on Wednesday May 14, 2025 @01:25AM (#43981115)
A while ago someone asked me a C++ question and I said "sorry, never worked seriously with C++". Then I remembered that I wrote the client code for a private instant messenger in Borland C++ approximately 20 years ago, that was used by thousands of people.

So yeah, it happens.

Re:PDF to Text, a Challenging Problem by 90s_dev on Wednesday May 14, 2025 @11:31AM (#43985768)
Exactly. Every time I look at Rust, it looks foreign and unfamiliar, until I remember that I wrote Rust for a client for maybe a year straight many years ago. Then it starts to come back to me.