File: svg2pdftex.py

package info (click to toggle)
lyx 2.5.0~RC2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 138,212 kB
  • sloc: cpp: 244,227; ansic: 106,398; xml: 72,791; python: 39,384; sh: 7,666; makefile: 6,586; pascal: 2,143; perl: 2,101; objc: 1,084; tcl: 163; sed: 16
file content (89 lines) | stat: -rw-r--r-- 3,024 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/python3

# file svg2pdftex.py
# This file is part of LyX, the document processor.
# Licence details can be found in the file COPYING.

# author Daniel Gloger
# author Martin Vermeer
# author Jürgen Spitzmüller

# Full author contact details are available in file CREDITS

# This script converts an SVG image to two files that can be processed
# with pdflatex into high quality PDF. It requires Inkscape.

# Usage:
#   python svg2pdftex.py [--unstable] [inkscape_command] inputfile.svg outputfile.pdf_tex
# This command generates
#   1. outputfile.pdf     -- the converted PDF file (text from SVG stripped)
#   2. outputfile.pdf_tex -- a TeX file that can be included in your
#                             LaTeX document using '\input{outputfile.pdf_text}'
# use --unstable for inkscape < 1.0
#
# Note:
#   Do not use this command as
#     python svg2pdftex.py [inkscape_command] inputfile.svg outputfile.pdf
#   the real PDF file would be overwritten by a TeX file named outputfile.pdf.
#


import os, sys, re, subprocess

def runCommand(cmd):
    ''' Utility function:
        run a command, quit if fails
    '''
    res = subprocess.check_call(cmd)
    if res != 0:
        print("Command '%s' fails (exit code: %i)." % (res.cmd, res.returncode))
        sys.exit(1)

InkscapeCmd = "inkscape"
InputFile = ""
OutputFile = ""
unstable = False

# We expect two to four args: the names of the input and output files
# and optionally the inkscape command (with path if needed) and --unstable.
args = len(sys.argv)
if args == 3:
    # Two args: input and output file only
    InputFile, OutputFile = sys.argv[1:]
elif args == 4:
    # Three args: check whether we have --unstable as first arg
    if sys.argv[1] == "--unstable":
        unstable = True
        InputFile, OutputFile = sys.argv[2:]
    else:
        InkscapeCmd, InputFile, OutputFile = sys.argv[1:]
elif args == 5:
    # Four args: check whether we have --unstable as first arg
    if sys.argv[1] != "--unstable":
        # Invalid number of args. Exit with error.
        sys.exit(1)
    else:
        unstable = True
        InkscapeCmd, InputFile, OutputFile = sys.argv[2:]
else:
    # Invalid number of args. Exit with error.
    sys.exit(1)

# Fail silently if the file doesn't exist
if not os.path.isfile(InputFile):
    sys.exit(0)

# Strip the extension from ${OutputFile}
OutBase = os.path.splitext(OutputFile)[0]

# Inkscape (as of 0.48) can output SVG images as a PDF file without text, ${OutBase}.pdf,
# while outsourcing the text to a LaTeX file ${OutBase}.pdf_tex which includes and overlays
# the PDF image and can be \input to LaTeX files. We rename the latter file to ${OutputFile}
# (although this is probably the name it already has).
if unstable:
    runCommand([r'%s' % InkscapeCmd, '--file=%s' % InputFile, '--export-pdf=%s.pdf' % OutBase, '--export-latex'])
else:
    runCommand([r'%s' % InkscapeCmd, '%s' % InputFile, '--export-filename=%s.pdf' % OutBase, '--export-latex'])

os.rename('%s.pdf_tex' % OutBase, OutputFile)