Ubuntu – Convert pdf to monochrome black-and-white via command line

command lineghostscriptinkscapepdfprinting

I have a 65-page PDF file that has text in a dark grey. (Actual text, not a picture/image of text.) Printing on my B/W printer, the text gets all blurry, because the printer is trying to make it less than black. I can solve this problem page-per-page by importing the pages to inkscape, selecting all paths, clicking on "Black", which converts all colours including the greytones to pure black, and exporting back to PDF.

Is there a way to do these steps in inkscape in the command line (for batch processing)?

Is there a way to achieve the same result (convert pdf to black and white, 1-bit greyscale) without inkscape?


Edit. There are similar questions on Unix.SE and on superuser, which use Ghostscript. However, these only convert the files to greyscale via the option -sColorConversionStrategy=Gray. The option -sColorConversionStrategy=/Mono results in the error Unrecoverable error: rangecheck in .putdeviceprops. Since my file is greyscale already, these don't solve my problem.

Best Answer

Adapting this answer over on SuperUser, this can be achieved by converting the PDF to PostScript and back using a redefined setrgbcolor command:

gs -o <output-file.pdf> -sDEVICE=pdfwrite \
-c "/osetrgbcolor {/setrgbcolor} bind def /setrgbcolor {pop [0 0 0] osetrgbcolor} def" \
-f <input-file.ps>