Convert PDF to Black & White – How to Convert a PDF to Black & White with Ghostscript

command lineconversionghostscriptlinuxpdf

Similarly to this question:

Convert a PDF to greyscale on the command line in FLOSS?

I have a PDF-document and want to convert it to pure black and white. So I want to discard halftones. To convert to grayscale with ghostscript I can use this command:

gs \
 -sOutputFile=output.PDF \
 -sDEVICE=pdfwrite \
 -sColorConversionStrategy=Gray \
 -dProcessColorModel=/DeviceGray \
 -dCompatibilityLevel=1.4 \
  input.PDF < /dev/null

What do I have to change to get monochrome e.g. only the colors black and white and no halftones?

Best Answer

The last suggestion indeed only converts to grayscale and then only works if the underlying doc uses setrgbcolor. This did not work for me, since I had a doc, that used setcolor.

I had success with redefining setcolor to always set the color to 0,0,0:

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

It has been 15+ years since I did any PostScript hacking, so the above may be lame, incorrect or even accidental - if you know how to do better, please suggest.

Related Question