2018-08-29 16:26:21 +00:00
|
|
|
#!/usr/bin/python3
|
2016-09-03 14:17:45 +00:00
|
|
|
|
|
|
|
# file svg2pdftex.py
|
2017-08-26 14:10:55 +00:00
|
|
|
# This file is part of LyX, the document processor.
|
|
|
|
# Licence details can be found in the file COPYING.
|
|
|
|
|
|
|
|
# author Daniel Gloger
|
|
|
|
# author Martin Vermeer
|
2017-08-28 06:00:56 +00:00
|
|
|
# author Jürgen Spitzmüller
|
2017-08-26 14:10:55 +00:00
|
|
|
|
|
|
|
# Full author contact details are available in file CREDITS
|
|
|
|
|
2017-08-28 06:00:56 +00:00
|
|
|
# This script converts an SVG image to two files that can be processed
|
|
|
|
# with pdflatex into high quality PDF. It requires Inkscape.
|
2016-09-03 14:17:45 +00:00
|
|
|
|
|
|
|
# Usage:
|
2020-05-15 12:08:46 +00:00
|
|
|
# python svg2pdftex.py [--unstable] [inkscape_command] inputfile.svg outputfile.pdf_tex
|
2016-09-03 14:17:45 +00:00
|
|
|
# This command generates
|
2017-08-28 06:00:56 +00:00
|
|
|
# 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}'
|
2020-05-15 12:08:46 +00:00
|
|
|
# use --unstable for inkscape < 1.0
|
2016-09-03 14:17:45 +00:00
|
|
|
#
|
|
|
|
# Note:
|
|
|
|
# Do not use this command as
|
2017-08-28 06:00:56 +00:00
|
|
|
# python svg2pdftex.py [inkscape_command] inputfile.svg outputfile.pdf
|
|
|
|
# the real PDF file would be overwritten by a TeX file named outputfile.pdf.
|
2016-09-03 14:17:45 +00:00
|
|
|
#
|
|
|
|
|
2017-11-24 10:56:41 +00:00
|
|
|
|
2017-09-10 15:02:58 +00:00
|
|
|
import os, sys, re, subprocess
|
2016-09-03 14:17:45 +00:00
|
|
|
|
|
|
|
def runCommand(cmd):
|
|
|
|
''' Utility function:
|
|
|
|
run a command, quit if fails
|
|
|
|
'''
|
2017-09-12 05:57:29 +00:00
|
|
|
res = subprocess.check_call(cmd)
|
|
|
|
if res != 0:
|
2017-11-23 07:16:10 +00:00
|
|
|
print("Command '%s' fails (exit code: %i)." % (res.cmd, res.returncode))
|
2016-09-03 14:17:45 +00:00
|
|
|
sys.exit(1)
|
|
|
|
|
2017-08-28 06:00:56 +00:00
|
|
|
InkscapeCmd = "inkscape"
|
|
|
|
InputFile = ""
|
|
|
|
OutputFile = ""
|
2020-05-15 12:08:46 +00:00
|
|
|
unstable = False
|
2016-09-03 14:17:45 +00:00
|
|
|
|
2020-05-15 12:08:46 +00:00
|
|
|
# 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.
|
2017-08-28 06:00:56 +00:00
|
|
|
args = len(sys.argv)
|
|
|
|
if args == 3:
|
|
|
|
# Two args: input and output file only
|
|
|
|
InputFile, OutputFile = sys.argv[1:]
|
|
|
|
elif args == 4:
|
2020-05-15 12:08:46 +00:00
|
|
|
# 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:]
|
2017-08-28 06:00:56 +00:00
|
|
|
else:
|
|
|
|
# Invalid number of args. Exit with error.
|
|
|
|
sys.exit(1)
|
2016-09-03 14:17:45 +00:00
|
|
|
|
|
|
|
# Fail silently if the file doesn't exist
|
2017-08-28 06:00:56 +00:00
|
|
|
if not os.path.isfile(InputFile):
|
2016-09-03 14:17:45 +00:00
|
|
|
sys.exit(0)
|
|
|
|
|
2017-08-28 06:00:56 +00:00
|
|
|
# Strip the extension from ${OutputFile}
|
|
|
|
OutBase = os.path.splitext(OutputFile)[0]
|
2016-09-03 14:17:45 +00:00
|
|
|
|
2017-08-28 06:00:56 +00:00
|
|
|
# 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).
|
2020-05-15 12:08:46 +00:00
|
|
|
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'])
|
2016-09-03 14:17:45 +00:00
|
|
|
|
2017-08-28 06:00:56 +00:00
|
|
|
os.rename('%s.pdf_tex' % OutBase, OutputFile)
|
2016-09-03 14:17:45 +00:00
|
|
|
|