diff --git a/IPython/nbconvert/transformers/svg2pdf.py b/IPython/nbconvert/transformers/svg2pdf.py index a692601..b630b8e 100644 --- a/IPython/nbconvert/transformers/svg2pdf.py +++ b/IPython/nbconvert/transformers/svg2pdf.py @@ -13,6 +13,7 @@ one format to another. # Imports #----------------------------------------------------------------------------- +import base64 import os import sys import subprocess @@ -37,16 +38,34 @@ INKSCAPE_OSX_COMMAND = '/Applications/Inkscape.app/Contents/Resources/bin/inksca class SVG2PDFTransformer(ConvertFiguresTransformer): """ - Converts all of the outputs in a notebook from one format to another. + Converts all of the outputs in a notebook from SVG to PDF. """ from_format = Unicode('svg', config=True, help='Format the converter accepts') to_format = Unicode('pdf', config=False, help='Format the converter writes') + command = Unicode(config=True, + help="""The command to use for converting SVG to PDF + + This string is a template, which will be formatted with the keys + to_filename and from_filename. + + The conversion call must read the SVG from {from_flename}, + and write a PDF to {to_filename}. + """) + + def _command_default(self): + if sys.platform == "darwin": + return INKSCAPE_OSX_COMMAND + elif sys.platform == "win32": + # windows not yet supported + return "" + else: + return INKSCAPE_COMMAND def convert_figure(self, data_format, data): """ - Convert a single Svg figure. Returns converted data. + Convert a single SVG figure to PDF. Returns converted data. """ #Work in a temporary directory @@ -54,24 +73,20 @@ class SVG2PDFTransformer(ConvertFiguresTransformer): #Write fig to temp file input_filename = os.path.join(tmpdir, 'figure.' + data_format) - with open(input_filename, 'w') as f: + with open(input_filename, 'wb') as f: f.write(data) - #Determine command (different on Mac OSX) - command = INKSCAPE_COMMAND - if sys.platform == 'darwin': - command = INKSCAPE_OSX_COMMAND - #Call conversion application output_filename = os.path.join(tmpdir, 'figure.pdf') - shell = command.format(from_filename=input_filename, + shell = self.command.format(from_filename=input_filename, to_filename=output_filename) subprocess.call(shell, shell=True) #Shell=True okay since input is trusted. #Read output from drive + # return value expects a filename if os.path.isfile(output_filename): with open(output_filename, 'rb') as f: - return f.read().encode("base64") #PDF is a nb supported binary - #data type, so base64 encode. + # PDF is a nb supported binary, data type, so base64 encode. + return base64.encodestring(f.read()) else: return TypeError("Inkscape svg to png conversion failed")