svg2pdf.py
77 lines
| 3.0 KiB
| text/x-python
|
PythonLexer
|
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 | ||||
#----------------------------------------------------------------------------- | ||||
import os | ||||
|
r11385 | import sys | ||
import subprocess | ||||
|
r11367 | from IPython.utils.tempdir import TemporaryDirectory | ||
|
r11386 | from IPython.utils.traitlets import Unicode | ||
|
r11367 | |||
from .convertfigures import ConvertFiguresTransformer | ||||
#----------------------------------------------------------------------------- | ||||
# Constants | ||||
#----------------------------------------------------------------------------- | ||||
|
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}"' | ||||
|
r11367 | |||
#----------------------------------------------------------------------------- | ||||
# Classes | ||||
#----------------------------------------------------------------------------- | ||||
|
r11422 | class SVG2PDFTransformer(ConvertFiguresTransformer): | ||
|
r11367 | """ | ||
Converts all of the outputs in a notebook from one format to another. | ||||
""" | ||||
|
r11386 | from_format = Unicode('svg', config=True, help='Format the converter accepts') | ||
to_format = Unicode('pdf', config=False, help='Format the converter writes') | ||||
|
r11367 | |||
def convert_figure(self, data_format, data): | ||||
""" | ||||
Convert a single Svg figure. Returns converted data. | ||||
""" | ||||
#Work in a temporary directory | ||||
with TemporaryDirectory() as tmpdir: | ||||
#Write fig to temp file | ||||
input_filename = os.path.join(tmpdir, 'figure.' + data_format) | ||||
with open(input_filename, 'w') as f: | ||||
f.write(data) | ||||
|
r11384 | #Determine command (different on Mac OSX) | ||
command = INKSCAPE_COMMAND | ||||
if sys.platform == 'darwin': | ||||
command = INKSCAPE_OSX_COMMAND | ||||
|
r11367 | #Call conversion application | ||
output_filename = os.path.join(tmpdir, 'figure.pdf') | ||||
|
r11384 | shell = command.format(from_filename=input_filename, | ||
to_filename=output_filename) | ||||
|
r11367 | subprocess.call(shell, shell=True) #Shell=True okay since input is trusted. | ||
#Read output from drive | ||||
if os.path.isfile(output_filename): | ||||
|
r11374 | with open(output_filename, 'rb') as f: | ||
return f.read().encode("base64") #PDF is a nb supported binary | ||||
#data type, so base64 encode. | ||||
|
r11367 | else: | ||
return TypeError("Inkscape svg to png conversion failed") | ||||