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

83

46

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.

Rory

Posted 2010-02-04T11:16:16.697

Reputation: 1 547

Answers

36

ImageMagick can do this.

convert -colorspace GRAY color.pdf gray.pdf

via this email

Iain

Posted 2010-02-04T11:16:16.697

Reputation: 4 399

7Convert 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 – 2014-09-19T12:24:09.633

2Unless you use -density 400 -quality 100 parameters - that works well – burtek – 2015-12-13T20:51:47.197

2Really, is there anything ImageMagick can't do? :) – BalinKingOfMoria Reinstate CMs – 2017-08-18T03:02:16.943

-density 400 -quality 100 creates HUGE files. +1 for @goyinux's solution. – Stanimir Stoyanov – 2018-06-27T10:45:13.513

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 – 2019-01-10T11:59:35.963

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? – Arch Linux Tux – 2019-04-30T16:45:03.070

28That significantly reduces quality. @goyinux' solution is better. – Johannes Weiss – 2013-02-12T16:41:00.863

163

Better:

gs \
 -sOutputFile=output.pdf \
 -sDEVICE=pdfwrite \
 -sColorConversionStrategy=Gray \
 -dProcessColorModel=/DeviceGray \
 -dCompatibilityLevel=1.4 \
 -dNOPAUSE \
 -dBATCH \
 input.pdf

goyinux

Posted 2010-02-04T11:16:16.697

Reputation:

1Works well on Windows, too! Just remove the \\ and put everything on the same line. – ixe013 – 2014-08-19T04:23:03.347

1In fact, this fails with this error GPL Ghostscript 9.10: Unable to convert color space to Gray, reverting strategy to LeaveColorUnchanged. – jjmerelo – 2015-12-15T17:56:42.270

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 – 2017-05-13T06:30:09.767

1Agreed, this gives much better results than convert, but sometimes it rotates the pdf which is a bit annoying! – tdc – 2012-08-07T18:08:41.187

10Just realised you can disable that with -dAutoRotatePages=/None – tdc – 2012-08-07T18:15:43.910

1I 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 – 2012-12-19T02:33:17.953

14

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

Posted 2010-02-04T11:16:16.697

Reputation: 141

FWIW, pdftk can concatenate PDFs, too. – user1338062 – 2018-06-14T07:07:39.520