nfirvine.comwiki

SVGPagesToSinglePDF

Filed in: Tutorials.SVGPagesToSinglePDF · Modified on : Fri, 27 May 11

This tutorial details how to take a number of SVGs and compile them into a single PDF.

Tools

  • Inkscape
  • psmerge from psutils
  • ps2pdf from ghostscript

Command

  1. for i in *.svg; do
  2.   inkscape -T -P ${i%\.*}.ps $i
  3. done &&
  4. psmerge -o- *.ps |
  5. ps2pdf - output.pdf

Lines 1–3 use Inkscape to export all SVGs to PostScript files. The fancy ${i%\.*} yields i minus the .svg so that we don't end up with .svg.ps. The -T switch is optional: it forces Inkscape to render text as paths; I find most programs don't render the flowText's that Inkscape uses natively well or at all.

Line 4 uses the psmerge command to merge all the PostScripts into one file. And pipes the output to...

Line 5 converts a Postscript to a PDF.

Actually, while this method "works", it isn't flawless: I was encountering problems with it adding a massive margin (like an extra inch) at the top of each page. I haven't determined why, but next is a fixed method.

Ghostscript method

  1. for i in *.svg; do
  2.   inkscape -T -P ${i%\.*}.ps $i
  3. done &&
  4. gs -sPAPERSIZE=letter -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -sOutputFile=output.pdf *.ps

Lines 1–3 are the same as above: use Inkscape to convert the SVGs to PostScripts.

Line 4 uses ghostscript to join and convert to PDF in one step.

Alternative method

Another method involves using the Image Magick convert command:

convert -adjoin -page A4 *.svg output.pdf

This is obviously much simpler and faster. However, two caveats:

  1. IM rasterizes the SVGs first, then plunks the images on separate pages. All the obvious problems of rasterization ensue.
  2. I don't think Image Magick's SVG rasterizer is as good as Inkscape's, at least not for Inkscape SVGs. ;)

Fixed method


Powered by PmWiki