Is there any command line tool (or toolchain) for linux where I can automatically blackout a given regexp in a pdf, or alternatively (maybe better) introduce noise in the match to make the parts matching the regexp unreadable in the pdf.

Currently I use gimp and do it manually, but this is very annoying and I am looking for a way to automate it (and one step further use it in a script).

  • Is that for printing, or are you going to distribute the PDF file? If you distribute the PDF file, you can't just hide something by putting a black rectangle on it: the text is still in the file. It can only work if you fully rasterize, which has many downsides: it makes the file bigger, makes the quality less good, removes hyperlinks, makes the file non-searchable, makes it inaccessible to blind people, … Nov 30, 2021 at 9:28
  • Just for printing.
    – student
    Nov 30, 2021 at 9:48

1 Answer 1


This can be achieved with various levels of success depending on how complex the formatting of the PDF file is.

Start by converting the PDF file to HTML using Poppler's pdftohtml:

pdftohtml -noframes -s <input.pdf>

Obscure specific text using your regexp:

sed -i 's/<regexp>/<replacement>/g' <input.html>

Convert HTML file back to PDF:

wkhtmltopdf --no-background --enable-local-file-access <input.html> <output.pdf>

You may have to play around with various options in wkhtmltopdf to adjust page margins, size and orientation, etc. See man wkhtmltopdf.

  • If the pdf is a scanned document (with ocr layer) this results probably in a html file including the original pdf as image and thus doesn't work. In other cases the format may be screwed up by the html conversion.
    – student
    Dec 29, 2021 at 17:23
  • I think the solution should work somehow with the ocr layer which contains information about the position of the matches.
    – student
    Dec 29, 2021 at 17:24

You must log in to answer this question.

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