Inkscape SVG の各レイヤを PDF にエクスポートする
Inkscape で作った SVG の各レイヤを個別の PDF ファイルに書き出し、最後にそれらを 1 つの PDF ファイルにまとめる Python スクリプト。
Inkscape と gswin32c に PATH が通っていると仮定。Windows 用なので、他の環境で動かすには修正が要る。
import os, sys from xml.etree.ElementTree import * argv = sys.argv argc = len(argv) if(argc < 2): print 'Usage: svg2pdf.py hoge.svg' quit() path = argv[1] base, ext = os.path.splitext(path) tree = parse(path) pdflist = "" SVG_NS = "http://www.w3.org/2000/svg" for layer in tree.findall('.//{%s}g' % SVG_NS): lid = layer.get("id") if lid.find("layer") < 0: continue cmd = "inkscape -C -f \"%s\" -A %s_%s.pdf -j -i \"%s\"" \ % (path, base, lid, lid) print cmd os.system(cmd) pdflist += " %s_%s.pdf" % (base, lid) cmd = "gswin32c -dNOPAUSE -sDEVICE=pdfwrite " \ + "-dAutoFilterColorImages=false " \ + "-dColorImageFilter=/FlateEncode " \ + "-sOUTPUTFILE=%s.pdf -dBATCH %s" \ % (base, pdflist) print cmd os.system(cmd)
個別に書き出した PDF は放ったらかし。非表示のレイヤは真っ白なページとして出力される。
- 2013.02.06 追記
レイヤでないオブジェクトを PDF に吐き出すことがあるので、
if lid.find("layer") < 0: continue
の2行を加えた。