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