]> git.lyx.org Git - features.git/blob - lib/scripts/svg2pdftex.py
Improve the svg2*tex.py scripts.
[features.git] / lib / scripts / svg2pdftex.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 # file svg2pdftex.py
5 # This file is part of LyX, the document processor.
6 # Licence details can be found in the file COPYING.
7
8 # author Daniel Gloger
9 # author Martin Vermeer
10 # author Jürgen Spitzmüller
11
12 # Full author contact details are available in file CREDITS
13
14 # This script converts an SVG image to two files that can be processed
15 # with pdflatex into high quality PDF. It requires Inkscape.
16
17 # Usage:
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}'
23 #
24 # Note:
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.
28 #
29
30 import os, sys, re
31
32 def runCommand(cmd):
33     ''' Utility function:
34         run a command, quit if fails
35     '''
36     if os.system(cmd) != 0:
37         print "Command '%s' fails." % cmd
38         sys.exit(1)
39
40 InkscapeCmd = "inkscape"
41 InputFile = ""
42 OutputFile = ""
43
44 # We expect two or three args: the names of the input and output files
45 # and optionally the inkscape command (with path if needed).
46 args = len(sys.argv)
47 if args == 3:
48     # Two args: input and output file only
49     InputFile, OutputFile = sys.argv[1:]
50 elif args == 4:
51     # Three args: first arg is inkscape command
52     InkscapeCmd, InputFile, OutputFile = sys.argv[1:]
53 else:
54     # Invalid number of args. Exit with error.
55     sys.exit(1)
56
57 # Fail silently if the file doesn't exist
58 if not os.path.isfile(InputFile):
59     sys.exit(0)
60
61 # Strip the extension from ${OutputFile}
62 OutBase = os.path.splitext(OutputFile)[0]
63
64 # Inkscape (as of 0.48) can output SVG images as a PDF file without text, ${OutBase}.pdf,
65 # while outsourcing the text to a LaTeX file ${OutBase}.pdf_tex which includes and overlays
66 # the PDF image and can be \input to LaTeX files. We rename the latter file to ${OutputFile}
67 # (although this is probably the name it already has).
68 runCommand('%s --file=%s --export-pdf=%s.pdf --export-latex' % (InkscapeCmd, InputFile, OutBase))
69
70 os.rename('%s.pdf_tex' % OutBase, OutputFile)
71