Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
up vote 5 down vote accepted

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.

share|improve this answer

I am not sure if the following suggestion will work... but it may be worth to try out:

  1. convert the PDF to PostScript using the simple pdf2ps utility
  2. convert that PostScript back to PDF while using a re-defined /setrgbcolor PostScript operator

These are the commands:

First

  pdf2ps color.pdf color.ps

This gives you color.ps as output.

Second

gs \
-o bw-from-color.pdf \
-sDEVICE=pdfwrite \
-c "/setrgbcolor{0 mul 3 1 roll 0 mul 3 1 roll 0 mul 3 1 roll 0 mul add add setgray}def" \
-f color.ps
share|improve this answer
    
I tried this and was still left with shades of gray. niklasfi wants monochrome. – frabjous Jan 23 '11 at 15:57

It's not ghostscript, but with imagemagick this is quite simple:

 convert -monochrome input.pdf output.pdf
share|improve this answer
    
+1, imageMagick is extremely simple, powerful, and FREE! – Lee Harrison Sep 26 '12 at 16:49
2  
-1. The result is pasteurized, though the delivery format is pdf. – Bryce Feb 7 '13 at 6:46
5  
The resulting pdf quality is much much worse than original. – Hindol Jul 1 '13 at 7:52

This looks like it would work:

1) Convert the file to monochrome with gs

gs -sDEVICE=psmono \
  -dNOPAUSE -dBATCH -dSAFER \
  -sOutputFile=combined.ps \
  first.pdf \
  second.ps \
  third.eps [...]

3) Convert the Postscript file back to a PDF with ps2pdf or gs

(credit to: http://www.linuxjournal.com/content/tech-tip-using-ghostscript-convert-and-combine-files)

share|improve this answer

for gray scale PDF:

By using GhostScript

IN PHP code, use this script

exec("'gs' '-sOutputFile=outputfilename.pdf' '-sDEVICE=pdfwrite' '-sColorConversionStrategy=Gray' '-dProcessColorModel=/DeviceGray' '-dCompatibilityLevel=1.4'  'inputfilename.pdf'",$output);

usefull url
http://www.linuxjournal.com/content/tech-tip-using-ghostscript-convert-and-combine-files

share|improve this answer

For pure black and white PDF, you need to convert it into ps format then into PDF for postscript:

exec(" gs -sDEVICE=psmono  -dNOPAUSE -dBATCH -dSAFER  -sOutputFile=combined.ps  $pdf");

postscript to PDF -> black and white

exec(" gs -sDEVICE=pdfwrite   -dNOPAUSE -dBATCH -dSAFER  -sOutputFile=file_pdf.pdf  filename.ps");
share|improve this answer

You must log in to answer this question.

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