]> git.lyx.org Git - lyx.git/blob - lib/scripts/svg2pdftex.py
whitespace.
[lyx.git] / lib / scripts / svg2pdftex.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 # file svg2pdftex.py
5 #
6 # This script converts an SVG image to something that pdflatex can process
7 # into high quality PDF.
8
9 # Usage:
10 #   python svg2pdftex.py ${base}.svg ${base}.pdft
11 # This command generates
12 #   ${base}.pdf  the converted pdf file
13 #   ${base}.pdft a tex file that can be included in your latex document
14 #       using '\input{${base}.pdft}'
15 #
16 # Note:
17 #   Do not use this command as
18 #     python svg2pdftex.py file.svg file.pdf
19 #   the real pdf file will be overwritten by a tex file named file.pdf.
20 #
21
22
23
24 import os, sys, re
25
26
27 def runCommand(cmd):
28     ''' Utility function:
29         run a command, quit if fails
30     '''
31     if os.system(cmd) != 0:
32         print "Command '%s' fails." % cmd
33         sys.exit(1)
34
35
36 # We expect two args, the names of the input and output files.
37 if len(sys.argv) != 3:
38     sys.exit(1)
39
40 input, output = sys.argv[1:]
41
42 # Fail silently if the file doesn't exist
43 if not os.path.isfile(input):
44     sys.exit(0)
45
46 # Strip the extension from ${output}
47 outbase = os.path.splitext(output)[0]
48
49
50
51 # Inkscape 0.48 can output the image as a PDF file ${base}.pdf and place the text 
52 # in a LaTeX file ${base}.pdf_tex, which is renamed to ${output}, for typesetting 
53 # by pdflatex itself. 
54 runCommand('inkscape --file=%s --export-pdf=%s.pdf --export-latex' % (input, outbase))
55
56 os.rename('%s.pdf_tex' % outbase, output)
57