Live data from Hacker News

So you want to modify the text of a PDF by hand (2020)

gist.github.com

71–80 of 103 posts

Re: So you want to modify the text of a PDF by hand (2020)

#71
post #38

Anybody trying to do this is missing the point of PDF: It’s a page-description format and therefore only represents the marks on a page , not document structure . One should not attempt to edit a PDF, one should edit the document from which the PDF is generated.

I'll stop trying to edit PDFs when people stop sending me PDFs that I want to edit. Somehow it became "unprofessional" to just send meant-to-be-editable documents around for everyone to enjoy, so this is where we end up...

It isn't "somehow" there are some legitimate reasons why.

When I send a client a final document (that's not intended to be edited) in a .PDF format you can almost guarantee that it will look the same to them as it did for me. When I send someone a Word document, I can't guarantee that it will look the same between different versions of Word, Mac Word, Pages, Google doc etc.

I'm not saying .PDF formats are perfect, but they're certainly more consistently presented to the end user.

Re: So you want to modify the text of a PDF by hand (2020)

#72
post #33
post #27

Earlier quoted context omitted.

After you've finished editing, just run it through qpdf without parameters, as explained in the beginning of the article, and it will recompress the data and recreate the xref table. No need for yet another tool.

I guess you could, but this is the source of the errors (actually warnings) that the article mentions. Probably best to fix the file with the provided tool (fix-qdf is distributed with qpdf) rather than get in the habit of ignoring warnings.

OK. I have never needed or used fix-qdf and forgot about it, so your typo (fix-pdf) made me think it was from a different package.

Re: So you want to modify the text of a PDF by hand (2020)

#73

You can do this: pdf2ps a.pdf # convert to postscript "a.ps" vim a.ps # edit postscript by hand ps2pdf a.ps # convert back to pdf Some complex pdf (with embedded javascript, animations, etc) fail to work correctly after this back and forth. Yet for "plain" documents this works alright. You can easily remove watermarks, change some words and numbers, etc. Spacing is harder to modify. Of course you need to know some po…

in vim?

It doesn't matter, just use any text editor.

Re: So you want to modify the text of a PDF by hand (2020)

#75

You can do this: pdf2ps a.pdf # convert to postscript "a.ps" vim a.ps # edit postscript by hand ps2pdf a.ps # convert back to pdf Some complex pdf (with embedded javascript, animations, etc) fail to work correctly after this back and forth. Yet for "plain" documents this works alright. You can easily remove watermarks, change some words and numbers, etc. Spacing is harder to modify. Of course you need to know some po…

If you can put javascript and animations in pdf, what's stopping you from making a frontend in it? I wonder what are the frontiers of things you can do in pdf. Honestly, it seems like only malware authors benefit from the complexity of pdfs.

The most widespread javascript packaging format turns out to be PDF[1]

[1] source: made it up

Re: So you want to modify the text of a PDF by hand (2020)

#76

You can do this: pdf2ps a.pdf # convert to postscript "a.ps" vim a.ps # edit postscript by hand ps2pdf a.ps # convert back to pdf Some complex pdf (with embedded javascript, animations, etc) fail to work correctly after this back and forth. Yet for "plain" documents this works alright. You can easily remove watermarks, change some words and numbers, etc. Spacing is harder to modify. Of course you need to know some po…

If you can put javascript and animations in pdf, what's stopping you from making a frontend in it? I wonder what are the frontiers of things you can do in pdf. Honestly, it seems like only malware authors benefit from the complexity of pdfs.

MacOS‘s Quartz (original 2D rendering) is based on pdf: https://www.prepressure.com/pdf/basics/osx-quartz

Re: So you want to modify the text of a PDF by hand (2020)

#77

What people often miss about PDF is that it's closer to an image format in some ways than to a Word document. Word documents, PDFs and images are in document editing what DAW projects, midis and mp3 files are in music and what Java source code, JVM bytecode and pure x86 machine code are in software. The primary purpose of a PDF file is to tell you what to display (or print), with perfect clarity, in much fewer bytes…

The original goal of PDF was to have a portable print fidelity copy; WYSIWYG for real. You could take a PDF file and print it on a laser printer, a linotype, or a screen and it would look the same.

If you printed it on a postscript printer it would look exactly the same (or better, if you used type 1 fonts).

Re: So you want to modify the text of a PDF by hand (2020)

#78
post #5

This topic comes up periodically as most people think PDFs are some impenetrable binary format, but they’re really not. They are a graph of objects of different types. The types themselves are well described in the official spec (I’m a sadist, I read it for fun). My advice is always to convert the pdf to a version without compressed data like the author here has. My tool of choice is mutool (mutool clean -d in.pdf ou…

> I’m a sadist, I read it for fun.

I think this is called masochist. Now, if you participated in writing the spec or were making others read it...

Re: So you want to modify the text of a PDF by hand (2020)

#79
Some small PDF files are saved as uncompressed text. Invoices are a typical example.

This means that we can open those files, read them as one single string and match the expected text in unit tests. I've got a few projects doing that and it was fine.

If the text is compressed, pipe its content to qpdf first.

Re: So you want to modify the text of a PDF by hand (2020)

#80
post #48

If you don't mind using java, you can use the open source Apache PDFBox library https://pdfbox.apache.org/ It's relatively performant and it's a mature and supported codebase that can accomplish most pdf tasks.

Except text substitution

https://pdfbox.apache.org/2.0/migration.html#why-was-the-rep...

> ReplaceText example has been removed as it gave the incorrect illusion that text can be replaced easily

Post reply on HN