##// END OF EJS Templates
make inkscape command configurable...
MinRK -
Show More
@@ -13,6 +13,7 b' one format to another.'
13 # Imports
13 # Imports
14 #-----------------------------------------------------------------------------
14 #-----------------------------------------------------------------------------
15
15
16 import base64
16 import os
17 import os
17 import sys
18 import sys
18 import subprocess
19 import subprocess
@@ -37,16 +38,34 b" INKSCAPE_OSX_COMMAND = '/Applications/Inkscape.app/Contents/Resources/bin/inksca"
37
38
38 class SVG2PDFTransformer(ConvertFiguresTransformer):
39 class SVG2PDFTransformer(ConvertFiguresTransformer):
39 """
40 """
40 Converts all of the outputs in a notebook from one format to another.
41 Converts all of the outputs in a notebook from SVG to PDF.
41 """
42 """
42
43
43 from_format = Unicode('svg', config=True, help='Format the converter accepts')
44 from_format = Unicode('svg', config=True, help='Format the converter accepts')
44 to_format = Unicode('pdf', config=False, help='Format the converter writes')
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 def convert_figure(self, data_format, data):
66 def convert_figure(self, data_format, data):
48 """
67 """
49 Convert a single Svg figure. Returns converted data.
68 Convert a single SVG figure to PDF. Returns converted data.
50 """
69 """
51
70
52 #Work in a temporary directory
71 #Work in a temporary directory
@@ -54,24 +73,20 b' class SVG2PDFTransformer(ConvertFiguresTransformer):'
54
73
55 #Write fig to temp file
74 #Write fig to temp file
56 input_filename = os.path.join(tmpdir, 'figure.' + data_format)
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 f.write(data)
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 #Call conversion application
79 #Call conversion application
66 output_filename = os.path.join(tmpdir, 'figure.pdf')
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 to_filename=output_filename)
82 to_filename=output_filename)
69 subprocess.call(shell, shell=True) #Shell=True okay since input is trusted.
83 subprocess.call(shell, shell=True) #Shell=True okay since input is trusted.
70
84
71 #Read output from drive
85 #Read output from drive
86 # return value expects a filename
72 if os.path.isfile(output_filename):
87 if os.path.isfile(output_filename):
73 with open(output_filename, 'rb') as f:
88 with open(output_filename, 'rb') as f:
74 return f.read().encode("base64") #PDF is a nb supported binary
89 # PDF is a nb supported binary, data type, so base64 encode.
75 #data type, so base64 encode.
90 return base64.encodestring(f.read())
76 else:
91 else:
77 return TypeError("Inkscape svg to png conversion failed")
92 return TypeError("Inkscape svg to png conversion failed")
General Comments 0
You need to be logged in to leave comments. Login now