14

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?

1

2 Answers 2

23

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.

4
  • 6
    @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, 2016 at 6:45
  • 1
    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, 2018 at 21:15
  • the replaced word should be of the same length, otherwise it doesn't look good :) Jan 25, 2021 at 18:26
  • this doesn't work for me on Ubuntu 22.04
    – con
    Apr 21, 2023 at 20:45
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!

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .