title: Converting PDF to CMYK (with identify recognizing CMYK)
body: <p>I am having much trouble to get ImageMagick's <code>identify</code> to, well, identify a PDF as CMYK. </p>
  
  <p>Essentially, let's say I'm building this file, <code>test.tex</code>, with <code>pdflatex</code>:</p>
  
  <pre><code>\documentclass[a4paper,12pt]{article}
  
  %% https://tex.stackexchange.com/questions/13071
  \pdfcompresslevel=0
  
  %% http://compgroups.net/comp.text.tex/Making-a-cmyk-PDF
  %% ln -s /usr/share/color/icc/sRGB.icm .
  % \immediate\pdfobj stream attr{/N 4} file{sRGB.icm}
  % \pdfcatalog{%
  % /OutputIntents [ &lt;&lt;
  % /Type /OutputIntent
  % /S/GTS_PDFA1
  % /DestOutputProfile \the\pdflastobj\space 0 R
  % /OutputConditionIdentifier (sRGB IEC61966-2.1)
  % /Info(sRGB IEC61966-2.1)
  % &gt;&gt; ]
  % }
  
  %% http://latex-my.blogspot.com/2010/02/cmyk-output-for-commercial-printing.html
  %% https://tex.stackexchange.com/questions/9961
  \usepackage[cmyk]{xcolor}
  
  \begin{document}
  Some text here...
  \end{document}
  </code></pre>
  
  <p>If I then try to identify the resulting <code>test.pdf</code> file, I get it as RGB, no matter what options I've tried (at least according to the links in the source) - and yet, the colors in it would be saved as CMYK; for the source above: </p>
  
  <pre><code>$ grep -ia 'cmyk\|rgb\| k' test.pdf 
  0 0 0 1 k 0 0 0 1 K
  0 0 0 1 k 0 0 0 1 K
  0 0 0 1 k 0 0 0 1 K
  0 0 0 1 k 0 0 0 1 K
  FontDirectory/CMR12 known{/CMR12 findfont dup/UniqueID known{dup
  /PTEX.Fullbanner (This is pdfTeX, Version 3.1415926-1.40.11-2.2 (TeX Live 2010) kpathsea version 6.0.0)
  
  $ identify -verbose 'test.pdf[0]'
  ...
    Type: Palette
    Endianess: Undefined
    Colorspace: RGB
    Depth: 16/8-bit
    Channel depth:
      red: 8-bit
      green: 8-bit
      blue: 8-bit
    Channel statistics:
      Red:
  ...
      Green:
  ...
      Blue:
  ...
    Histogram:
           5: (12593,11565,11822) #31312D2D2E2E rgb(49,45,46)
           4: (16448,15420,15677) #40403C3C3D3D rgb(64,60,61)
           9: (20303,19275,19532) #4F4F4B4B4C4C rgb(79,75,76)
          25: (23901,23130,23387) #5D5D5A5A5B5B rgb(93,90,91)
  ...
  </code></pre>
  
  <p>The same pretty much happens if I also uncomment that <code>\immediate\pdfobj stream ...</code>
   part; and yet, if there is only one color (black) in the document, I don't see where does <code>identify</code> come up with a histogram of RGB values (although, arguably, all of them close to gray) ?! </p>
  
  <p>&nbsp;</p>
  
  <p>So nevermind this, then I though I'd better try to use <code>ghostscript</code> to convert the <code>test.pdf</code> into a new pdf, which would be recognized as CMYK by <code>identify</code> - but no luck even there: </p>
  
  <pre><code>$ gs -dNOPAUSE -dBATCH -dSAFER -sDEVICE=pdfwrite  -sOutputFile=test-gs.pdf -dUseCIEColor -sProcessColorModel=DeviceRGB -dProcessColorModel=/DeviceCMYK -sColorConversionStrategy=/CMYK test.pdf 
  
  GPL Ghostscript 9.01 (2011-02-07)
  Copyright (C) 2010 Artifex Software, Inc.  All rights reserved.
  This software comes with NO WARRANTY: see the file PUBLIC for details.
  Processing pages 1 through 1.
  Page 1
  
  
  $ identify -verbose 'test-gs.pdf[0]'
  ...
    Type: Grayscale
    Base type: Grayscale
    Endianess: Undefined
    Colorspace: RGB
    Depth: 16/8-bit
  ...
  </code></pre>
  
  <p>So the only thing that <code>identify</code> perceived as a change, is <code>Type: Grayscale</code> (from previous <code>Type: Palette</code>); but otherwise it still sees an RGB colorspace!</p>
  
  <p>Along with this, note that <code>identify</code> <strong>is</strong> capable of correctly reporting a CMYK pdf - see <a href="https://tex.stackexchange.com/questions/17762/cmyk-poster-example-fitting-pdf-page-size-to-bitmap-image-size/17843#17843">CMYK poster example: fitting pdf page size to (bitmap) image size? #17843 - TeX - LaTeX - Stack Exchange</a> for a command line example of generating such a PDF file using <code>convert</code> and <code>gs</code>. In fact, we can execute: </p>
  
  <pre><code>convert test.pdf -depth 8 -colorspace cmyk -alpha Off test-c.pdf
  </code></pre>
  
  <p>... and this <strong>will</strong> result with a PDF that will be <code>identify</code>ed as CMYK - however, the PDF will also be rasterized (default at 72 dpi). </p>
  
  <p>EDIT: I have just discovered, that if I create an .odp presentation in OpenOffice, and export it to PDF; that PDF will by default be RGB, however, the following command (from <a href="http://www.productionmonkeys.net/guides/ghostscript/examples" rel="noreferrer">ghostscript Examples | Production Monkeys</a>): </p>
  
  <pre><code># Color PDF to CMYK:
  gs -dSAFER -dBATCH -dNOPAUSE -dNOCACHE -sDEVICE=pdfwrite \
  -sColorConversionStrategy=CMYK -dProcessColorModel=/DeviceCMYK \
  -sOutputFile=output.pdf input.pdf
  </code></pre>
  
  <p>... actually will produce a CMYK pdf, reported as such by <code>identify</code> (although, the black will be rich, not plain - on all four channels); however, this command will work <em>only</em> when the slide has an added image (apparently, it is the one triggering the color conversion?!)! Funnily, I cannot get the same effect from a <code>pdflatex</code> PDF. </p>
  
  <p>&nbsp;</p>
  
  <p>So I guess my question can be asked two ways: </p>
  
  <ul>
  <li>Are there any command-line conversion methods in Linux, that will convert an RGB pdf into a CMYK pdf while preserving vectors, which is recognized as such in <code>identify</code> (and will consequently build a correct histogram of CMYK colors)</li>
  <li>Are there any other command-line Linux tools similar to <code>identify</code>, which would recognize use of CMYK colors correctly even in the original <code>test.pdf</code> from <code>pdflatex</code> (<em>and possibly build a color histogram, based on an arbitrarily chosen PDF page, like <code>identify</code> is supposed to</em>)?</li>
  </ul>
  
  <p>Thanks in advance for any answers,<br>
  Cheers!</p>
  
  <p>&nbsp; </p>
  
  <p>Some references:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/3418268/script-or-some-other-means-to-convert-rgb-to-cmyk-in-pdf">adobe - Script (or some other means) to convert RGB to CMYK in PDF? - Stack Overflow</a></li>
  <li><a href="https://tex.stackexchange.com/questions/9961/pdf-colour-model-and-latex">color - PDF colour model and LaTeX - TeX - LaTeX - Stack Exchange</a></li>
  <li><a href="https://tex.stackexchange.com/questions/13071/option-cmyk-for-xcolor-package-does-not-produce-a-cmyk-pdf">color - Option cmyk for xcolor package does not produce a CMYK PDF - TeX - LaTeX - Stack Exchange</a></li>
  <li><a href="http://compgroups.net/comp.text.tex/Making-a-cmyk-PDF" rel="noreferrer">Making a cmyk PDF - comp.text.tex | Computer Group</a></li>
  <li><a href="http://www.rhinocerus.net/forum/lang-postscript/132878-colormanagement-ghostscript.html" rel="noreferrer">colormanagement with ghostscript ? - Rhinocerus</a>:
  
  <blockquote>
    <p>Is it for instance specified as "0 0 0
    1 setcmykcolor"? Or possibly rather as "0 0 0 setrgbcolor"? In the
    latter case you would end up with a rich black for text, if DeviceRGB is
    remapped to a CIE-based color space in order to get RGB images color
    managed.</p>
  </blockquote></li>
  </ul>