104

I have a colour PDF file, and I'm going to print it out and then photocopy it in black and white. I'd like to know what it's like in B&W before photocopying it. Is it possible to 'greyscale' a PDF on the command line using free software? I'm using Ubuntu 9.10.

Amandasaurus
  • 1,937
  • 4
  • 19
  • 19

3 Answers3

214

Better:

gs \
 -sOutputFile=output.pdf \
 -sDEVICE=pdfwrite \
 -sColorConversionStrategy=Gray \
 -dProcessColorModel=/DeviceGray \
 -dCompatibilityLevel=1.4 \
 -dNOPAUSE \
 -dBATCH \
 input.pdf
Eroen
  • 6,383
  • 1
  • 17
  • 25
  • 2
    Agreed, this gives much better results than convert, but sometimes it rotates the pdf which is a bit annoying! – tdc Aug 07 '12 at 18:08
  • 21
    Just realised you can disable that with `-dAutoRotatePages=/None` – tdc Aug 07 '12 at 18:15
  • 2
    I just ran this command on a 58MB PDF that was already greyscale (came from a scanner) and the resulting output was 10MB and looked exactly the same. Nice! – Archie Dec 19 '12 at 02:33
  • 1
    Works well on Windows, too! Just remove the `\\` and put everything on the same line. – ixe013 Aug 19 '14 at 04:23
  • 1
    In fact, this fails with this error `GPL Ghostscript 9.10: Unable to convert color space to Gray, reverting strategy to LeaveColorUnchanged.` – jjmerelo Dec 15 '15 at 17:56
  • Works great. But output image size is almost 3x for my file. I have a complicated vector-drawing colorful file with 3.5 MB size that became 10 MB! – saeedgnu May 13 '17 at 06:30
  • This is really fantastic. Perfect for scanned documents for subsequent print. – xaratustra May 07 '20 at 09:00
  • Some people complains that the size of the *grayscale* image is not lower than the *original*. If, besides turning the image grayscale, you want to compress it, add the following option `-dPDFSETTINGS=/ebook` . Instead of `/ebook` you can also use `/screen` for a more radical compression (i.e. lower quality). Enjoy it! – loved.by.Jesus Sep 09 '20 at 13:21
  • This may be better than the imagemagick convert, but it does *not* render losslessly. in particular, fonts are lost. text is now rendered, probably as graphics primitives, not as fonts. `-dPDFSETTINGS=/screen` is really loq quality, which the original file may not have been. – ivo Welch May 02 '23 at 21:33
40

ImageMagick can do this.

convert -colorspace GRAY color.pdf gray.pdf

via this email

Iain
  • 4,718
  • 2
  • 27
  • 41
  • 38
    That significantly reduces quality. @goyinux' solution is better. – Johannes Weiss Feb 12 '13 at 16:41
  • 8
    Convert will actually rasterize the contents of the pdf. So unless the pdf already encapsulates only raster images (e.g. a scanned document), this approach is a big no-no. – m000 Sep 19 '14 at 12:24
  • 4
    Unless you use `-density 400 -quality 100` parameters - that works well – burtek Dec 13 '15 at 20:51
  • 2
    Really, is there anything ImageMagick _can't_ do? :) – Joshua Grosso Reinstate CMs Aug 18 '17 at 03:02
  • 1
    `-density 400 -quality 100` creates HUGE files. +1 for @goyinux's solution. – Stanimir Stoyanov Jun 27 '18 at 10:45
  • The big files print great (only using black toner) while the original and gs-converted file use all toners and create a vague mess on my printer. – rew Jan 10 '19 at 11:59
  • What's the reason for `convert` to reduce the quality of rasterized images? I tried using the options `convert -density 300 -quality 100`, in order to prevent this behaviour of `convert`. But the quality of the output image is still much worse than the quality of the input image... so are there options for `convert`, that prevent the encapsulated images from quality loss, when converting `pdf`'-files encapsulating rasterized images? – ArchLinuxTux Apr 30 '19 at 16:45
  • Another problem with `convert ` is that OCRed pdf files lose the text layer. That is, one loses the OCR. While the method with `gs` keeps OCR. – loved.by.Jesus Sep 09 '20 at 13:07
  • ``convert -density 200 -colorspace GRAY`` worked like a charm for me – Jean Carlo Machado Mar 24 '21 at 09:31
16

Here’s a little script which in addition to the grayscale conversion can concatenate multiple input files. To use the script, put the following lines in a file, e.g. "convert2gray.sh"

#!/bin/bash
gs -sOutputFile=converted.pdf -sDEVICE=pdfwrite -sColorConversionStrategy=Gray -dProcessColorModel=/DeviceGray -dCompatibiltyLevel=1.4 -dNOPAUSE -dBATCH $@

and make it executable

chmod +x convert2gray.sh

Then

./convert2gray.sh input1.pdf input2.pdf … lastinput.pdf

will produce a single PDF "converted.pdf", which contains all pages from the input files converted to grayscale.

I had to print out mutliple files all in grayscale and found this the easiest way, since you can print out everything after inpection with one command.

ysis
  • 161
  • 1
  • 3