Search code examples
linuxpdfprintingcommand-linegrayscale

Convert PDF to GRAYSCALE format for printer from linux command line


I'm trying to convert a PDF document to Grayscale, because I need the printer to recognize each page containing colors or not.

I'm using ghostscript from linux command line.

  1. I convert PDF to PS to grep all the informations:

    pdftops -level3sep input.pdf

It generates a .ps file: input.ps

  1. I convert this PS file to PDF again, converting it to grayscale with ghostscript:

    gs -o output.pdf -sDEVICE=pdfwrite -dColorConversionStrategy=/Gray -dProcessColorModel=/DeviceGray input.ps

This command generates a PDF file.

When I open this last file, I see that all the pages are in grayscale. The command to test color pages says that are in grayscale, too:

gs -o - -sDEVICE=inkcov output.pdf

For each page, it returned me CMYK values:

Page 1
 0.00000  0.00000  0.00000  0.92538 CMYK OK
Page 2
 0.00000  0.00000  0.00000  0.06737 CMYK OK
Page 3
 0.00000  0.00000  0.00000  0.63738 CMYK OK
Page 4
 0.00000  0.00000  0.00000  0.75010 CMYK OK

and so on... CMYK: 0,0,0,[something] -> each page is in grayscale.

But when I go to print the document, the printer recognizes each page as sRGB and count them as color pages, not as grayscale pages.

If i check each page with this script, it returns me that all the pages are sRGB, too:

#!/bin/bash

FILE=$1
PAGES=$(pdfinfo ${FILE} | grep 'Pages:' | sed 's/Pages:\s*//')

GRAYPAGES=""
COLORPAGES=""
DOUBLECOLORPAGES=""

echo "Pages: $PAGES"
N=1
while (test "$N" -le "$PAGES")
do
    COLORSPACE=$( identify -format "%[colorspace]" "$FILE[$((N-1))]" )
    echo "$N: $COLORSPACE"
    if [[ $COLORSPACE == "Gray" ]]
    then
        GRAYPAGES="$GRAYPAGES $N"
    else
        COLORPAGES="$COLORPAGES $N"
        # For double sided documents also list the page on the other side of the sheet:
        if [[ $((N%2)) -eq 1 ]]
        then
            DOUBLECOLORPAGES="$DOUBLECOLORPAGES $N $((N+1))"
            #N=$((N+1))
        else
            DOUBLECOLORPAGES="$DOUBLECOLORPAGES $((N-1)) $N"
        fi
    fi
    N=$((N+1))
done

echo $DOUBLECOLORPAGES
echo $COLORPAGES
echo $GRAYPAGES

The result is:

andrea@release:~/fotocopia$ ./script.sh output.pdf
Pages: 4
1: sRGB
2: sRGB
3: sRGB
4: sRGB
1 2 1 2 3 4 3 4
1 2 3 4

So the question is: how can I do to make the printer (rather than the script) recognize the pages as grayscale? Is there a method through linux command line that allows me to do it?

Thanks in advance


Solution

  • I forgot to post the solution. I'm so sorry.

    The problem was not related to the conversion by GhostScript, but to the software used to open the PDF file.

    With the Chrome embedded PDF reader I had this problem, while with the original Adobe Reader plugin (overridden on chrome through chrome extensions) I was able to print correctly in greyscale and to let the printer recognize the format.

    I hope this will help people with the same issue