Convert PDF file from RGBA to CMYK

Converting PDF file from RGBA to CMYK for printing should be straightforward thing I know but things sometimes get messy. As long time user of Inkscape I was surprised when I found that I can’t save my work in pdf file in CMYK color format. Ok I admit I used Inkscape mostly for my web design needs and never for printing something but still I was surprised that feature is missing.



Inkscape extension

I know Inkscape has some cool extensions so I tried to find one for saving in CMYK. After short search I found one extension, followed instructions and installed it just to find out that it’s buggy and it’s not working. Ok it returned some errors for debug but solving one problem just led me to another one. After fixing some six or seven issues I had enough and decided to find another, better way.

Online Converter

There are many online converters giving you option to convert PDF from CMYK for free like PDF2CMYK but I needed local solution, what if I’m not online? But for most of you guys this will be the best solution it’s simple, fast and easy. Doesn’t require any work, opening terminal, messing with the code breaking and fixing things.

Scribus

One more way to do it is using Scribus to export your image into CMYK PDF, but it’s time consuming, why should i open Scribus to do that simple task.

Ghostscript

I continued googling around for solution until i read about ghostscript. Ghostscript is interpreter for the PostScript language and for PDF. It is bundled with almost all linux distributions and it has some cool terminal tools. Some serious documentation research lead me to terminal command that works like a charm.

 gs -dSAFER -dBATCH \
	    -dNOPAUSE -dNOCACHE -sDEVICE=pdfwrite \
	    -sColorConversionStrategy=CMYK \
	    -dProcessColorModel=/DeviceCMYK \
	    -sOutputFile=cmyk_drawing.pdf \
	    drawing.pdf

I just saved my work from Inkscape into basic PDF RGB file, started terminal and used command providing names for source and output files and it had done the job. Ok now I had something that is working but seeing this monster of command in terminal is enough to scare shit of me and thought of memorizing it was even worst. This is place where my developer instinct kicked in. I thought this is what is my job, solving problems like this. I need solution to simplify this and save myself much needed time. This command is far too long and complicated to memorize or work with it if I had more than one file that needs to be converted. But, I could make my own command.

Developing solution

I’m linux user for some twelve years and i had some experience with bash scripting before so I decided to make little neat bash script which will do the job. Script will be simple it will take one argument which is name of pdf file which I want to convert from RGB to CMYK colors. It will check if user has supplied file name as argument, if file exists in current directory and it will call ghostscript command to convert file. If file can’t be found it will output error in terminal. Opened my terminal, started nano editor and in a few minutes I had my script ready.

#!/bin/bash

# check if user supplied file name
if [ -z "$1" ]
  then
    echo "No file name supplied"
else

# get file name from argument 1
file=$1

# check if file exists
if [ -f "$file" ]
    then
	echo "$file found."
  	echo "converting $file from RGB to CMYK..."
	 gs -dSAFER -dBATCH \
	    -dNOPAUSE -dNOCACHE -sDEVICE=pdfwrite \
	    -sColorConversionStrategy=CMYK \
	    -dProcessColorModel=/DeviceCMYK \
	    -sOutputFile=cmyk_$file \
	    $file
    else
	echo "$file not found."
    fi
fi

I saved my bash script to file named tocmyk which will be also the name of new command I will be calling in terminal to convert my PDF files from RGB to CMYK colors. I made my script executable and moved it to /usr/bin directory so I can access it from every location in filesystem from terminal like every other command.

chmod +x tocmyk
sudo mv tocmyk /usr/bin

After this I restarted terminal to get access to new command. Converting PDF from RGB to CMYK is peace of cake now, just start terminal go to directory where your source PDF file is and use the command like this:

tocmyk drawing.pdf

After this you will see that script has created new file in the same directory named cmyk_drawing.pdf and this one is in CMYK colors. I know my way isn’t best or easiest for everyone but it worked for me and I hope you guys learned some basic bash script from this example. Linux is free and powerful tool and possibilities to add your own things and customize it for your own needs are almost endless.



Share with your friends