Show More
@@ -1,94 +1,95 b'' | |||
|
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 | 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}"' | |
|
34 | ||
|
32 | INKSCAPE_APP = '/Applications/Inkscape.app/Contents/Resources/bin/inkscape' | |
|
35 | 33 | |
|
36 | 34 | #----------------------------------------------------------------------------- |
|
37 | 35 | # Classes |
|
38 | 36 | #----------------------------------------------------------------------------- |
|
39 | 37 | |
|
40 | 38 | class SVG2PDFTransformer(ConvertFiguresTransformer): |
|
41 | 39 | """ |
|
42 | 40 | Converts all of the outputs in a notebook from SVG to PDF. |
|
43 | 41 | """ |
|
44 | 42 | |
|
45 | 43 | from_format = Unicode('svg', config=True, help='Format the converter accepts') |
|
46 | 44 | to_format = Unicode('pdf', config=False, help='Format the converter writes') |
|
45 | ||
|
47 | 46 | command = Unicode(config=True, |
|
48 | 47 | help="""The command to use for converting SVG to PDF |
|
49 | 48 | |
|
50 | 49 | This string is a template, which will be formatted with the keys |
|
51 | 50 | to_filename and from_filename. |
|
52 | 51 | |
|
53 | 52 | The conversion call must read the SVG from {from_flename}, |
|
54 | 53 | and write a PDF to {to_filename}. |
|
55 | 54 | """) |
|
56 | 55 | |
|
57 | 56 | def _command_default(self): |
|
57 | return self.inkscape + \ | |
|
58 | ' --without-gui --export-pdf="{to_filename}" "{from_filename}"' | |
|
59 | ||
|
60 | inkscape = Unicode(config=True, help="The path to Inkscape, if necessary") | |
|
61 | def _inkscape_default(self): | |
|
58 | 62 | if sys.platform == "darwin": |
|
59 | return INKSCAPE_OSX_COMMAND | |
|
60 | elif sys.platform == "win32": | |
|
61 | # windows not yet supported | |
|
62 | return "" | |
|
63 | else: | |
|
64 | return INKSCAPE_COMMAND | |
|
63 | if os.path.isfile(INKSCAPE_APP): | |
|
64 | return INKSCAPE_APP | |
|
65 | return "inkscape" | |
|
65 | 66 | |
|
66 | 67 | |
|
67 | 68 | def convert_figure(self, data_format, data): |
|
68 | 69 | """ |
|
69 | 70 | Convert a single SVG figure to PDF. Returns converted data. |
|
70 | 71 | """ |
|
71 | 72 | |
|
72 | 73 | #Work in a temporary directory |
|
73 | 74 | with TemporaryDirectory() as tmpdir: |
|
74 | 75 | |
|
75 | 76 | #Write fig to temp file |
|
76 | 77 | input_filename = os.path.join(tmpdir, 'figure.' + data_format) |
|
77 | 78 | # SVG data is unicode text |
|
78 | 79 | with io.open(input_filename, 'w', encoding='utf8') as f: |
|
79 | 80 | f.write(data) |
|
80 | 81 | |
|
81 | 82 | #Call conversion application |
|
82 | 83 | output_filename = os.path.join(tmpdir, 'figure.pdf') |
|
83 | 84 | shell = self.command.format(from_filename=input_filename, |
|
84 | 85 | to_filename=output_filename) |
|
85 | 86 | subprocess.call(shell, shell=True) #Shell=True okay since input is trusted. |
|
86 | 87 | |
|
87 | 88 | #Read output from drive |
|
88 | 89 | # return value expects a filename |
|
89 | 90 | if os.path.isfile(output_filename): |
|
90 | 91 | with open(output_filename, 'rb') as f: |
|
91 | 92 | # PDF is a nb supported binary, data type, so base64 encode. |
|
92 | 93 | return base64.encodestring(f.read()) |
|
93 | 94 | else: |
|
94 | 95 | raise TypeError("Inkscape svg to png conversion failed") |
General Comments 0
You need to be logged in to leave comments.
Login now