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