##// END OF EJS Templates
make inkscape command configurable...
make inkscape command configurable and fix b64 treatment of PDFs

File last commit:

r11571:93f89316
r11571:93f89316
Show More
svg2pdf.py
92 lines | 3.4 KiB | text/x-python | PythonLexer
Jonathan Frederic
Added writers and supporting code.
r11367 """Module containing a transformer that converts outputs in the notebook from
one format to another.
"""
#-----------------------------------------------------------------------------
# Copyright (c) 2013, the IPython Development Team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file COPYING.txt, distributed with this software.
#-----------------------------------------------------------------------------
#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
MinRK
make inkscape command configurable...
r11571 import base64
Jonathan Frederic
Added writers and supporting code.
r11367 import os
Jonathan Frederic
Missing imports
r11385 import sys
import subprocess
Jonathan Frederic
Added writers and supporting code.
r11367 from IPython.utils.tempdir import TemporaryDirectory
Jonathan Frederic
To and from formats configurable
r11386 from IPython.utils.traitlets import Unicode
Jonathan Frederic
Added writers and supporting code.
r11367
from .convertfigures import ConvertFiguresTransformer
#-----------------------------------------------------------------------------
# Constants
#-----------------------------------------------------------------------------
Jonathan Frederic
Mac OSX support for svg converter
r11384 INKSCAPE_COMMAND = 'inkscape --without-gui --export-pdf="{to_filename}" "{from_filename}"'
INKSCAPE_OSX_COMMAND = '/Applications/Inkscape.app/Contents/Resources/bin/inkscape --without-gui --export-pdf="{to_filename}" "{from_filename}"'
Jonathan Frederic
Added writers and supporting code.
r11367
#-----------------------------------------------------------------------------
# Classes
#-----------------------------------------------------------------------------
Jonathan Frederic
Rename SVG2PDFTransformer class
r11422 class SVG2PDFTransformer(ConvertFiguresTransformer):
Jonathan Frederic
Added writers and supporting code.
r11367 """
MinRK
make inkscape command configurable...
r11571 Converts all of the outputs in a notebook from SVG to PDF.
Jonathan Frederic
Added writers and supporting code.
r11367 """
Jonathan Frederic
To and from formats configurable
r11386 from_format = Unicode('svg', config=True, help='Format the converter accepts')
to_format = Unicode('pdf', config=False, help='Format the converter writes')
MinRK
make inkscape command configurable...
r11571 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
Jonathan Frederic
Added writers and supporting code.
r11367
def convert_figure(self, data_format, data):
"""
MinRK
make inkscape command configurable...
r11571 Convert a single SVG figure to PDF. Returns converted data.
Jonathan Frederic
Added writers and supporting code.
r11367 """
#Work in a temporary directory
with TemporaryDirectory() as tmpdir:
#Write fig to temp file
input_filename = os.path.join(tmpdir, 'figure.' + data_format)
MinRK
make inkscape command configurable...
r11571 with open(input_filename, 'wb') as f:
Jonathan Frederic
Added writers and supporting code.
r11367 f.write(data)
#Call conversion application
output_filename = os.path.join(tmpdir, 'figure.pdf')
MinRK
make inkscape command configurable...
r11571 shell = self.command.format(from_filename=input_filename,
Jonathan Frederic
Mac OSX support for svg converter
r11384 to_filename=output_filename)
Jonathan Frederic
Added writers and supporting code.
r11367 subprocess.call(shell, shell=True) #Shell=True okay since input is trusted.
#Read output from drive
MinRK
make inkscape command configurable...
r11571 # return value expects a filename
Jonathan Frederic
Added writers and supporting code.
r11367 if os.path.isfile(output_filename):
Jonathan Frederic
base64 encode PDF before writing back to the nb structure
r11374 with open(output_filename, 'rb') as f:
MinRK
make inkscape command configurable...
r11571 # PDF is a nb supported binary, data type, so base64 encode.
return base64.encodestring(f.read())
Jonathan Frederic
Added writers and supporting code.
r11367 else:
return TypeError("Inkscape svg to png conversion failed")