2 # -*- coding: utf-8 -*-
5 # This file is part of LyX, the document processor.
6 # Licence details can be found in the file COPYING.
9 # author Martin Vermeer
10 # author Jürgen Spitzmüller
12 # Full author contact details are available in file CREDITS
14 # This script converts an SVG image to two files that can be processed
15 # with pdflatex into high quality PDF. It requires Inkscape.
18 # python svg2pdftex.py [inkscape_command] inputfile.svg outputfile.pdf_tex
19 # This command generates
20 # 1. outputfile.pdf -- the converted PDF file (text from SVG stripped)
21 # 2. outputfile.pdf_tex -- a TeX file that can be included in your
22 # LaTeX document using '\input{outputfile.pdf_text}'
25 # Do not use this command as
26 # python svg2pdftex.py [inkscape_command] inputfile.svg outputfile.pdf
27 # the real PDF file would be overwritten by a TeX file named outputfile.pdf.
30 import os, sys, re, subprocess
34 run a command, quit if fails
36 res = subprocess.check_call(cmd)
38 print "Command '%s' fails (exit code: %i)." % (res.cmd, res.returncode)
41 InkscapeCmd = "inkscape"
45 # We expect two or three args: the names of the input and output files
46 # and optionally the inkscape command (with path if needed).
49 # Two args: input and output file only
50 InputFile, OutputFile = sys.argv[1:]
52 # Three args: first arg is inkscape command
53 InkscapeCmd, InputFile, OutputFile = sys.argv[1:]
55 # Invalid number of args. Exit with error.
58 # Fail silently if the file doesn't exist
59 if not os.path.isfile(InputFile):
62 # Strip the extension from ${OutputFile}
63 OutBase = os.path.splitext(OutputFile)[0]
65 # Inkscape (as of 0.48) can output SVG images as a PDF file without text, ${OutBase}.pdf,
66 # while outsourcing the text to a LaTeX file ${OutBase}.pdf_tex which includes and overlays
67 # the PDF image and can be \input to LaTeX files. We rename the latter file to ${OutputFile}
68 # (although this is probably the name it already has).
69 runCommand([r'%s' % InkscapeCmd, '--file=%s' % InputFile, '--export-pdf=%s.pdf' % OutBase, '--export-latex'])
71 os.rename('%s.pdf_tex' % OutBase, OutputFile)