Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't edit pdf file with sed command

Tags:

bash

sed

pdf

I have a book in PDF format and I can't change anything in it with sed. I can't really use LibreOffice to edit it because it's a 300-page document with lots of images, and my PC would crash during the opening. All I need to do is change one or two characters at only one spot (for example, changing "+2" to "+3", where "+2" has only one occurrence in the entire book) so the solution should be basic.

I uncompressed it and then tried to use sed on it, sed didn't change anything, although echo $? would return 0.

pdftk file.pdf output uncompressed_file.pdf uncompress
sed -i 's/foo/bar/g' uncompressed_file.pdf
pdftk uncompressed_file.pdf output corrected_file.pdf compress

This very code worked with another file, I suspect that some PDF files prevent editing and I'm looking for a way to bypass that.

like image 832
Pippin Avatar asked Dec 03 '25 14:12

Pippin


1 Answers

I have used a shell command to do this.

qpdf --stream-data=uncompress $1 uncompressed.pdf
sed -i "s/("$2")/("$3")/g" uncompressed.pdf
qpdf --stream-data=compress uncompressed.pdf $1

So if this is myShell.sh, then a command line such as,

myShell.sh yourFile.pdf +2 +3

should do it.

like image 58
VectorVortec Avatar answered Dec 05 '25 09:12

VectorVortec



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!