##// END OF EJS Templates
Fix svg2pdf filter
Thomas Kluyver -
Show More
@@ -1,64 +1,58 b''
1 """Module containing a preprocessor that converts outputs in the notebook from
1 """Module containing a preprocessor 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 from .base import Preprocessor
16 from .base import Preprocessor
17 from IPython.utils.traitlets import Unicode
17 from IPython.utils.traitlets import Unicode
18
18
19 #-----------------------------------------------------------------------------
19 #-----------------------------------------------------------------------------
20 # Classes
20 # Classes
21 #-----------------------------------------------------------------------------
21 #-----------------------------------------------------------------------------
22
22
23 class ConvertFiguresPreprocessor(Preprocessor):
23 class ConvertFiguresPreprocessor(Preprocessor):
24 """
24 """
25 Converts all of the outputs in a notebook from one format to another.
25 Converts all of the outputs in a notebook from one format to another.
26 """
26 """
27
27
28 from_format = Unicode(config=True, help='Format the converter accepts')
28 from_format = Unicode(config=True, help='Format the converter accepts')
29 to_format = Unicode(config=True, help='Format the converter writes')
29 to_format = Unicode(config=True, help='Format the converter writes')
30
30
31 def __init__(self, **kw):
31 def __init__(self, **kw):
32 """
32 """
33 Public constructor
33 Public constructor
34 """
34 """
35 super(ConvertFiguresPreprocessor, self).__init__(**kw)
35 super(ConvertFiguresPreprocessor, self).__init__(**kw)
36
36
37
37
38 def convert_figure(self, data_format, data):
38 def convert_figure(self, data_format, data):
39 raise NotImplementedError()
39 raise NotImplementedError()
40
40
41
41
42 def preprocess_cell(self, cell, resources, cell_index):
42 def preprocess_cell(self, cell, resources, cell_index):
43 """
43 """
44 Apply a transformation on each cell,
44 Apply a transformation on each cell,
45
45
46 See base.py
46 See base.py
47 """
47 """
48
48
49 # Loop through all of the datatypes of the outputs in the cell.
49 # Loop through all of the datatypes of the outputs in the cell.
50 for index, cell_out in enumerate(cell.get('outputs', [])):
50 for output in cell.get('outputs', []):
51 for data_type, data in list(cell_out.items()):
51 if output.output_type in {'execute_result', 'display_data'} \
52 # this must run *before* extract outputs,
52 and self.from_format in output.data \
53 # so figure_name and filename do not exist
53 and self.to_format not in output.data:
54 self._convert_figure(cell_out, resources, data_type, data)
55 return cell, resources
56
54
55 output.data[self.to_format] = self.convert_figure(
56 self.from_format, output.data[self.from_format])
57
57
58 def _convert_figure(self, cell_out, resources, data_type, data):
58 return cell, resources
59 """
60 Convert a figure and output the results to the cell output
61 """
62 if not self.to_format in cell_out and data_type == self.from_format:
63 data = self.convert_figure(data_type, data)
64 cell_out[self.to_format] = data
@@ -1,82 +1,82 b''
1 """Module containing a preprocessor that converts outputs in the notebook from
1 """Module containing a preprocessor that converts outputs in the notebook from
2 one format to another.
2 one format to another.
3 """
3 """
4
4
5 # Copyright (c) IPython Development Team.
5 # Copyright (c) IPython Development Team.
6 # Distributed under the terms of the Modified BSD License.
6 # Distributed under the terms of the Modified BSD License.
7
7
8 import base64
8 import base64
9 import io
9 import io
10 import os
10 import os
11 import sys
11 import sys
12 import subprocess
12 import subprocess
13
13
14 from IPython.utils.tempdir import TemporaryDirectory
14 from IPython.utils.tempdir import TemporaryDirectory
15 from IPython.utils.traitlets import Unicode
15 from IPython.utils.traitlets import Unicode
16
16
17 from .convertfigures import ConvertFiguresPreprocessor
17 from .convertfigures import ConvertFiguresPreprocessor
18
18
19
19
20 INKSCAPE_APP = '/Applications/Inkscape.app/Contents/Resources/bin/inkscape'
20 INKSCAPE_APP = '/Applications/Inkscape.app/Contents/Resources/bin/inkscape'
21
21
22
22
23 class SVG2PDFPreprocessor(ConvertFiguresPreprocessor):
23 class SVG2PDFPreprocessor(ConvertFiguresPreprocessor):
24 """
24 """
25 Converts all of the outputs in a notebook from SVG to PDF.
25 Converts all of the outputs in a notebook from SVG to PDF.
26 """
26 """
27
27
28 def _from_format_default(self):
28 def _from_format_default(self):
29 return 'image/svg+xml'
29 return 'image/svg+xml'
30 def _to_format_default(self):
30 def _to_format_default(self):
31 return 'application/pdf'
31 return 'application/pdf'
32
32
33 command = Unicode(config=True,
33 command = Unicode(config=True,
34 help="""The command to use for converting SVG to PDF
34 help="""The command to use for converting SVG to PDF
35
35
36 This string is a template, which will be formatted with the keys
36 This string is a template, which will be formatted with the keys
37 to_filename and from_filename.
37 to_filename and from_filename.
38
38
39 The conversion call must read the SVG from {from_flename},
39 The conversion call must read the SVG from {from_flename},
40 and write a PDF to {to_filename}.
40 and write a PDF to {to_filename}.
41 """)
41 """)
42
42
43 def _command_default(self):
43 def _command_default(self):
44 return self.inkscape + \
44 return self.inkscape + \
45 ' --without-gui --export-pdf="{to_filename}" "{from_filename}"'
45 ' --without-gui --export-pdf="{to_filename}" "{from_filename}"'
46
46
47 inkscape = Unicode(config=True, help="The path to Inkscape, if necessary")
47 inkscape = Unicode(config=True, help="The path to Inkscape, if necessary")
48 def _inkscape_default(self):
48 def _inkscape_default(self):
49 if sys.platform == "darwin":
49 if sys.platform == "darwin":
50 if os.path.isfile(INKSCAPE_APP):
50 if os.path.isfile(INKSCAPE_APP):
51 return INKSCAPE_APP
51 return INKSCAPE_APP
52 return "inkscape"
52 return "inkscape"
53
53
54
54
55 def convert_figure(self, data_format, data):
55 def convert_figure(self, data_format, data):
56 """
56 """
57 Convert a single SVG figure to PDF. Returns converted data.
57 Convert a single SVG figure to PDF. Returns converted data.
58 """
58 """
59
59
60 #Work in a temporary directory
60 #Work in a temporary directory
61 with TemporaryDirectory() as tmpdir:
61 with TemporaryDirectory() as tmpdir:
62
62
63 #Write fig to temp file
63 #Write fig to temp file
64 input_filename = os.path.join(tmpdir, 'figure.' + data_format)
64 input_filename = os.path.join(tmpdir, 'figure.svg')
65 # SVG data is unicode text
65 # SVG data is unicode text
66 with io.open(input_filename, 'w', encoding='utf8') as f:
66 with io.open(input_filename, 'w', encoding='utf8') as f:
67 f.write(data)
67 f.write(data)
68
68
69 #Call conversion application
69 #Call conversion application
70 output_filename = os.path.join(tmpdir, 'figure.pdf')
70 output_filename = os.path.join(tmpdir, 'figure.pdf')
71 shell = self.command.format(from_filename=input_filename,
71 shell = self.command.format(from_filename=input_filename,
72 to_filename=output_filename)
72 to_filename=output_filename)
73 subprocess.call(shell, shell=True) #Shell=True okay since input is trusted.
73 subprocess.call(shell, shell=True) #Shell=True okay since input is trusted.
74
74
75 #Read output from drive
75 #Read output from drive
76 # return value expects a filename
76 # return value expects a filename
77 if os.path.isfile(output_filename):
77 if os.path.isfile(output_filename):
78 with open(output_filename, 'rb') as f:
78 with open(output_filename, 'rb') as f:
79 # PDF is a nb supported binary, data type, so base64 encode.
79 # PDF is a nb supported binary, data type, so base64 encode.
80 return base64.encodestring(f.read())
80 return base64.encodestring(f.read())
81 else:
81 else:
82 raise TypeError("Inkscape svg to pdf conversion failed")
82 raise TypeError("Inkscape svg to pdf conversion failed")
General Comments 0
You need to be logged in to leave comments. Login now