Ask HN: It seems to be impossible to find-replace text in a PDF at scale?

1 points by unsupp0rted ↗ HN
I have a bunch of generated PDF invoices, which all have the same typo, that I'd like to fix.

So far I have tried:

* pdftk via bash script

* qpdf via bash script

* PyPDF2

* pdf-lib via node

In all cases, it appears the PDF content is not in plain text, so the text replacement fails.

Often I'm getting:

> Unsupported contents type: <class 'PyPDF2.generic.TextStringObject'>

I'm surprised it's this hard to do a find-replace inside a PDF. Isn't there a Homebrew util for this?

GPT4 has been having me write a hundred variations of scripts, but not a single one addresses the "this isn't plain text" problem.

How would you go about doing find-replace of a text string in a PDF at scale (on Mac)?

1 comment

[ 4.1 ms ] story [ 17.0 ms ] thread
Are you aware of the fact that PDF can be anything between clean text-based LaTeX content and scanned graphical images, to just name two of the extremes of each possibility and not taking into consideration the immense variety possible in between?

Unless you restrict your ambitions to a clearly defined type of PDF this mission is supposedly impossible. Have a look at https://en.wikipedia.org/wiki/PDF to get an idea what kind of input data you are actually dealing with.

PDF is a wonderful display output container at the end of the publishing chain, but nothing you'd ever desire as input for further textual manipulation.