4

According to this question:

Displaying PDF documents on iPad - Color Problems

some PDFs don't display right on iOS devices due to colors not being in RGB. It's also mentioned that converting PDFs from CMYK to RGB could be automated using ghostscript. Anyone know how the actual command might look like?

Community
  • 1
  • 1
Robert Kovačević
  • 1,378
  • 4
  • 16
  • 23

4 Answers4

5

We use Ghostscript to convert from CMYK to RGB when generating PDFs from Postscript files. It should also work for PDF-to-PDF conversions.

The followind command line is used:

gs -sDEVICE=pdfwrite -dBATCH -dNOPAUSE -dCompatibilityLevel=1.4 -dColorConversionStrategy=/sRGB -dProcessColorModel=/DeviceRGB -dUseCIEColor=true -sOutputFile=output.pdf input.ps
SaltyNuts
  • 5,068
  • 8
  • 48
  • 80
Codo
  • 75,595
  • 17
  • 168
  • 206
  • 1
    Yes, this should work. However, since the pdfwrite device uses by default `-dColorConversionStrategy=/sRGB` and `-dProcessColorModel=/DeviceRGB` it shouldn't be required to state it on the commandline (though it doesn't do harm either). A different thing would be if you wanted PDF colors be converted to CMYK -- in this case the `-dColorConversionStrategy=/CMYK` and `-dProcessColorModel=/DeviceCMYK` would be required... – Kurt Pfeifle Dec 18 '11 at 16:04
  • I'm using the above with a 19MB pdf input, and the output is a 128KB pdf. The result is a degraded image, why is that compression/how to avoid any compression? Thx! – Gerard Bosch Aug 19 '21 at 10:17
  • @GerardBosch This is a non-trivial question. You better ask a new question in StackOverflow. – Codo Aug 19 '21 at 10:46
  • 1
    Thanks @Codo. I managed to do what I wanted (with the correct compression) by using this: `gs -sDEVICE=pdfwrite -dPDFX -dPDFSETTINGS=/prepress -dColorConversionStrategy=/sRGB -o output.pdf input.pdf`. I think the `/prepress` option provided the right settings to me. For reference, I also found this answer that talks about GS image processing/compression useful: https://superuser.com/a/373740/684037 – Gerard Bosch Aug 19 '21 at 14:56
2

I've solved the same issue here by downgrading the -dCompatibilityLevel from v1.4 to v1.3

UPDATE: v1.3 will turn all content in the PDF in just one object, this means the end user will not be able to select texts neither extract images in his viewer.

In order to keep using v1.4, I've discovered a trick on ghostscript that helps to keep the color accuracy, which is to disable PDF transparencies, they convert inaccurately because RGB doesn't have an alpha channel, so, information is lost.

So if you use: -dNOTRANSPARENCY you can still use: -dCompatibilityLevel=1.4 and it will work.

The exact command:

gs -sDEVICE=pdfwrite -dBATCH -dNOPAUSE -dCompatibilityLevel=1.4 -dNOTRANSPARENCY -dColorConversionStrategy=/sRGB -dProcessColorModel=/DeviceRGB -dColorConversionStrategyForImages=/DeviceRGB -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -dUseCIEColor=true -sOutputFile=output.pdf input.pdf

As I'm handling it with PHP, I wrote a simple class:

/**
 * Convert color profiles - PDF to PDF
 * Class Conversor
 */
class Conversor
{
    /**
     * Convert CMYK to RGB
     *
     * @param $input
     * @param $output
     * @return string
     */
    public static function gsCmykToRgb($input, $output)
    {
        $gsScript = ('gs -sDEVICE=pdfwrite \
                         -dBATCH -dNOPAUSE \
                         -dCompatibilityLevel=1.4 \
                         -dNOTRANSPARENCY \
                         -dColorConversionStrategy=/sRGB \
                         -dProcessColorModel=/DeviceRGB \
                         -dColorConversionStrategyForImages=/DeviceRGB \
                         -dTextAlphaBits=4 \
                         -dGraphicsAlphaBits=4 \
                         -dUseCIEColor=true \
                         -sOutputFile='."$output".' '."$input");
        exec($gsScript);

        return realpath($output);
    }
}

You can find everything about ghostscript here: http://www.ghostscript.com/doc/9.05/Use.htm

gritt
  • 78
  • 5
  • Devs say ([1](https://bugs.ghostscript.com/show_bug.cgi?id=700819#c1),[2](https://bugs.ghostscript.com/show_bug.cgi?id=693716#c4),[3](https://ghostscript.com/irclogs/2012/11/21.html)) that there's just no `-dColorConversionStrategyForImages` switch. – Igor Mar 18 '19 at 16:35
2

On mac, you could use sips command. For example,

sips --matchTo '/System/Library/ColorSync/Profiles/Generic RGB Profile.icc' CMYKinput.pdf --out RGBoutput.pdf

And RGB pdf can be converted into CMYK in the same way (change 'Generic RGB Profile.icc' to 'Generic CMYK Profile.icc').

Tested on OSX 10.12.

zhhan
  • 21
  • 2
1

as far I know, ghostscript is only able to convert colorspace in raster images

podofocolor

http://podofo.sourceforge.net/

is able to convert vector objects

you can try to convert (for black and white non colorful pages), cmyk to grayscale in this way:

podofocolor grayscale input.pdf output.pdf

if binaries are not in repositories, unfortunately you need to build by yourself. however, i just tried to convert a pdf to greyscale with

gs -sOutputFile=output.pdf -sDEVICE=pdfwrite -sColorConversionStrategy=Gray -dProcessColorModel=/DeviceGray -dCompatibilityLevel=1.4 input.pdf < /dev/null

and prepress preflight check said it was turned to grayscale, so, maybe I was wrong saying that gs can only convert colorspace of raster content in a pdf (since my pdf is vectorized)

Dingo
  • 2,619
  • 1
  • 22
  • 32
  • Thx, I'd like to try it, but I'm on Ubuntu 10.04, and it doesn't have podofo package. Do you know where I could find it? – Robert Kovačević Dec 13 '11 at 09:20
  • I updated my answer, after tried to convert to grayscale the colorspace of a pdf containing vector elements – Dingo Dec 13 '11 at 16:52