4

I want to find specific text or word from PDF file and replace it with another text and make a new PDF file from command-line. I tried with sed, find, awk series on text files but I want to get on PDF file. Which command line utility will help me?

10

Use pdftk to uncompress the file, then edit via sed and finally recompress it.

pdftk input.pdf output uncompressed.pdf uncompress
sed -i 's/foo/bar/g' uncompressed.pdf
# uncompressed.pdf now has all the changes
pdftk uncompressed.pdf output changed.pdf compress

Of course any text in images cannot be changes like this.

  • 5
    @denny Instead of writing "Thank you" comments, please accept the answer that helped you most by clicking the grey round check button on its left. Thanks and welcome to Ask Ubuntu. Please note that you can take the little tour to learn more about how our site works. – Byte Commander Jul 28 '16 at 6:45
  • Even uncompressed PDF does not produce text exactly as you see on the screen. I had to export from Acrobat Pro to Microsoft Word (Tool > Export), unzip the docx, then perform search & replace on document.xml directly. – Sun Oct 12 '18 at 21:15
0

A possible solution could look like this...

You can convert pdf files to pure text files by using a couple of tools from the poppler-utils package.

It can be installed via:

sudo apt-get install poppler-utils

Convert pdf file to text file

pdftotext /home/USER/Desktop/test.pdf /home/USER/Desktop/test.txt

Now you're able to do the editing and replacing on your pdf file.

After that you can convert it back by using a package called cups-pdf OR text2pdf.

Might be not the perfect solution, but should do its work.

Hope this helps!

  • Will work for text-only pdfs. However, it will most likely ignore any formatting and ignore (thus remove) images. – Fiximan Jul 28 '16 at 6:13

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.