General Computing
linux command-line pdf ghostscript conversion
Updated Mon, 18 Jul 2022 14:08:34 GMT

Converting a PDF to black & white with ghostscript

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?


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 <>

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.

Comments (3)

  • +0 – It should be {setcolor} rather than {/setcolor} since PostScript uses no slash when procedures are called during bind. Other than that: Great answer thank you. — Aug 17, 2020 at 17:21  
  • +1 – It did not work for me with gs 9.26. The output was in color, regardless of whether the argument was {setcolor} or {/setcolor} as per Hermann's comment above. — Mar 06, 2021 at 17:44  
  • +0 – I concur with @XavierStuvw It seems the behavior of gs has changed since 2011. The solution by @KurtPfeifle below that converts a ps to a black-white pdf with gs ... -c "/setrgbcolor{0 ... still works, however. — Dec 28, 2021 at 19:27