##// END OF EJS Templates
fix exception raising in svg2pdf
MinRK -
Show More
@@ -1,94 +1,94
1 1 """Module containing a transformer that converts outputs in the notebook from
2 2 one format to another.
3 3 """
4 4 #-----------------------------------------------------------------------------
5 5 # Copyright (c) 2013, the IPython Development Team.
6 6 #
7 7 # Distributed under the terms of the Modified BSD License.
8 8 #
9 9 # The full license is in the file COPYING.txt, distributed with this software.
10 10 #-----------------------------------------------------------------------------
11 11
12 12 #-----------------------------------------------------------------------------
13 13 # Imports
14 14 #-----------------------------------------------------------------------------
15 15
16 16 import base64
17 17 import io
18 18 import os
19 19 import sys
20 20 import subprocess
21 21
22 22 from IPython.utils.tempdir import TemporaryDirectory
23 23 from IPython.utils.traitlets import Unicode
24 24
25 25 from .convertfigures import ConvertFiguresTransformer
26 26
27 27
28 28 #-----------------------------------------------------------------------------
29 29 # Constants
30 30 #-----------------------------------------------------------------------------
31 31
32 32 INKSCAPE_COMMAND = 'inkscape --without-gui --export-pdf="{to_filename}" "{from_filename}"'
33 33 INKSCAPE_OSX_COMMAND = '/Applications/Inkscape.app/Contents/Resources/bin/inkscape --without-gui --export-pdf="{to_filename}" "{from_filename}"'
34 34
35 35
36 36 #-----------------------------------------------------------------------------
37 37 # Classes
38 38 #-----------------------------------------------------------------------------
39 39
40 40 class SVG2PDFTransformer(ConvertFiguresTransformer):
41 41 """
42 42 Converts all of the outputs in a notebook from SVG to PDF.
43 43 """
44 44
45 45 from_format = Unicode('svg', config=True, help='Format the converter accepts')
46 46 to_format = Unicode('pdf', config=False, help='Format the converter writes')
47 47 command = Unicode(config=True,
48 48 help="""The command to use for converting SVG to PDF
49 49
50 50 This string is a template, which will be formatted with the keys
51 51 to_filename and from_filename.
52 52
53 53 The conversion call must read the SVG from {from_flename},
54 54 and write a PDF to {to_filename}.
55 55 """)
56 56
57 57 def _command_default(self):
58 58 if sys.platform == "darwin":
59 59 return INKSCAPE_OSX_COMMAND
60 60 elif sys.platform == "win32":
61 61 # windows not yet supported
62 62 return ""
63 63 else:
64 64 return INKSCAPE_COMMAND
65 65
66 66
67 67 def convert_figure(self, data_format, data):
68 68 """
69 69 Convert a single SVG figure to PDF. Returns converted data.
70 70 """
71 71
72 72 #Work in a temporary directory
73 73 with TemporaryDirectory() as tmpdir:
74 74
75 75 #Write fig to temp file
76 76 input_filename = os.path.join(tmpdir, 'figure.' + data_format)
77 77 # SVG data is unicode text
78 78 with io.open(input_filename, 'w', encoding='utf8') as f:
79 79 f.write(data)
80 80
81 81 #Call conversion application
82 82 output_filename = os.path.join(tmpdir, 'figure.pdf')
83 83 shell = self.command.format(from_filename=input_filename,
84 84 to_filename=output_filename)
85 85 subprocess.call(shell, shell=True) #Shell=True okay since input is trusted.
86 86
87 87 #Read output from drive
88 88 # return value expects a filename
89 89 if os.path.isfile(output_filename):
90 90 with open(output_filename, 'rb') as f:
91 91 # PDF is a nb supported binary, data type, so base64 encode.
92 92 return base64.encodestring(f.read())
93 93 else:
94 return TypeError("Inkscape svg to png conversion failed")
94 raise TypeError("Inkscape svg to png conversion failed")
General Comments 0
You need to be logged in to leave comments. Login now