Skip to content

Instantly share code, notes, and snippets.

@rootwork
Last active December 28, 2023 10:15
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rootwork/a96494719b2c7028698a1a9ddbea3623 to your computer and use it in GitHub Desktop.
Save rootwork/a96494719b2c7028698a1a9ddbea3623 to your computer and use it in GitHub Desktop.
Creating a 300dpi CMYK PDF from an SVG (on Linux)

Creating a 300dpi CMYK PDF from an SVG (on Linux)

SVGs generated from the web, and any SVGs from Inkscape, will be in the RGB colorspace. To convert it to CMYK for printing, there are basically two major steps: Converting the SVG to a PDF, and then converting the PDF from RGB to CMYK.

Note that CMYK and RGB are literally different types of color; there is not and cannot be a 1:1 correspondence between their colors, so if you need to regularly design for print you should be using CMYK from the beginning (not tacking it on at the end) -- and for that, unfortunately, the only option is Adobe Illustrator.

Anyway, this is what worked for me to convert an RGB SVG to a CMYK PDF with a specific DPI.

Untested options

I haven't tried the Gnome command rsvg-convert (part of librsvg), which apparently can export to PDF, so I don't know if it would perform better than what I ended up with. Will test this in the future.

I also haven't tried the ExportPDFCMYK Inkscape extension (Linux-only) because I discovered it after the fact, but it promises to automate the manual steps I did take and is worth a try.

Tested and verified option

Prerequisites: Imagemagick and GhostScript, both of which you almost certainly have on Linux already. I had Imagemagick v7.x, but 6.x should work as well. For GhostScript, v9.5 is what was installed with my distro. Then you'll need Inkscape, which you may or may not already have.

  1. Open the SVG in Inkscape. The one bonus to this workflow is you don't have to convert text to paths before you export! So you can keep your easily-editable SVG.
  2. File...Save As...Portable Document Format (*.pdf) -- not Scribus CMYK PDF, that won't work unless your SVG is extremely simple (see details on Scribus in the next section).
  3. Options:
    • PDF version 1.5 (select)
    • Convert text to paths (select)
    • Rasterize filter effects (check)
    • Resolution: 300
    • Other settings as default unless you need to add page bleed
  4. Note that after you hit OK it may hang for a few moments; it runs a bunch of stuff in the background but can be processor-intensive.
  5. Once you have your PDF, head to the command line. Use the following command, where <input.pdf> is the file you just saved and <output.pdf> is the final CMYK version:
gs -q -sDEVICE=pdfwrite -sColorConversionStrategy=CMYK -o <output.pdf> <input.pdf>
  1. Always open the PDF in Document Viewer or the like to make sure everything converted correctly. File...Properties will tell you the print size ("paper size") in mm; at the command line use units '<###> mm' 'inches' to convert. (units docs)

If you use imagemagick's identify -verbose on the final PDF, it will (always!) report the DPI as 72, because in the background it uses GhostScript to convert it to an image first using default resolution. It will, however, correctly report the colorspace as CMYK.

Alternate option

In some cases I had issues where Inkscape was not correctly exporting filters to PDF (possibly related to this bug or this one) and after a lot of experimentation I couldn't resolve it. One workaround is by exporting from Inkscape to a hi-res PNG. This will mean your final PDF will contain raster images rather than vector images -- but if the resolution is high enough, this may be acceptable. The workflow in this case is:

  1. File...Export PNG Image..., make sure "Page" is set as the export area at the top, and change the DPI under image size to something high -- I recommend at least twice what your final resolution should be, so for 300dpi I exported here at 600dpi.
  2. At the command line, use ImageMagick to convert to PDF, specifying your resolution (in this case 300x300 = 300dpi):
convert <input.png> -alpha off -units PixelsPerInch -density 300x300 <output.pdf>
  1. Now that you have a PDF, convert it to CMYK starting at step 5 above.

Other details and things already attempted:

Approaches that tried to do SVG-RGB to PDF-CMYK all at once tended to sacrifice resolution, text paths, filters, or all three:

  • Saving from Inkscape into Scribus CMYK PDF doesn't seem to allow us to set a DPI other than the default (72), doesn't natively convert text to paths, and doesn't handle SVG filters at all. The same is true if you open the SVG directly in Scribus. (Reference.)

  • Saving as EPS (vector) or TIFF (raster) and then using imagemagick or Glimpse (GIMP) to convert to PDF appears to be unreliable, especially with SVG filters.

  • Using cairosvg directly (what Inkscape uses internally) seems to choke on all sorts of Inkscape attributes, and doesn't automatically convert text to paths which is a drag.

  • Tools like pdftk, pdfjam, cpdf etc. are useful for merging and other actions on an existing PDF, but won't convert to PDF.

  • cyan just made enormous files and also seemed to downgrade resolution. (It also has a terrible UI.)

  • Krita and rgb2cmyk.org only export to image formats (JPEG, PNG, TIFF).

  • Getting a trial edition of Illustrator for a one-off conversion would also be an option.

Resources and prior art:

I considered just titling this "RGB SVG 🢡 CMYK PDF w/DPI". Sorry for all the acronyms in this domain of practice.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment