##// END OF EJS Templates
Toggle line numbers in menu...
Toggle line numbers in menu use 'Toggle' text, instead of switching between Show/Hide

File last commit:

r19095:476fadaf
r19320:98a32711
Show More
svg2pdf.py
83 lines | 2.8 KiB | text/x-python | PythonLexer
Paul Ivanov
replace 'transformer' with 'preprocessor'
r12219 """Module containing a preprocessor that converts outputs in the notebook from
Jonathan Frederic
Added writers and supporting code.
r11367 one format to another.
"""
MinRK
address review from takluyver...
r18602 # Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.
Jonathan Frederic
Added writers and supporting code.
r11367
MinRK
make inkscape command configurable...
r11571 import base64
MinRK
fix unicode file-writing in svg2pdf
r11578 import io
Jonathan Frederic
Added writers and supporting code.
r11367 import os
Jonathan Frederic
Missing imports
r11385 import sys
import subprocess
Thomas Kluyver
Ensure that we're writing unicode in svg2pdf preprocessor...
r19095 from IPython.utils.py3compat import cast_unicode_py2
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
Paul Ivanov
replace 'transformer' with 'preprocessor'
r12219 from .convertfigures import ConvertFiguresPreprocessor
Jonathan Frederic
Added writers and supporting code.
r11367
MinRK
tweak inkscape configurability...
r11580 INKSCAPE_APP = '/Applications/Inkscape.app/Contents/Resources/bin/inkscape'
Jonathan Frederic
Added writers and supporting code.
r11367
Paul Ivanov
replace 'transformer' with 'preprocessor'
r12219 class SVG2PDFPreprocessor(ConvertFiguresPreprocessor):
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 """
MinRK
handle 'application/pdf' in nbconvert...
r15346
def _from_format_default(self):
MinRK
address review from takluyver...
r18602 return 'image/svg+xml'
MinRK
handle 'application/pdf' in nbconvert...
r15346 def _to_format_default(self):
return 'application/pdf'
MinRK
tweak inkscape configurability...
r11580
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):
MinRK
tweak inkscape configurability...
r11580 return self.inkscape + \
' --without-gui --export-pdf="{to_filename}" "{from_filename}"'
inkscape = Unicode(config=True, help="The path to Inkscape, if necessary")
def _inkscape_default(self):
MinRK
make inkscape command configurable...
r11571 if sys.platform == "darwin":
MinRK
tweak inkscape configurability...
r11580 if os.path.isfile(INKSCAPE_APP):
return INKSCAPE_APP
return "inkscape"
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
Thomas Kluyver
Fix svg2pdf filter
r19051 input_filename = os.path.join(tmpdir, 'figure.svg')
MinRK
fix unicode file-writing in svg2pdf
r11578 # SVG data is unicode text
with io.open(input_filename, 'w', encoding='utf8') as f:
Thomas Kluyver
Ensure that we're writing unicode in svg2pdf preprocessor...
r19095 f.write(cast_unicode_py2(data))
Jonathan Frederic
Added writers and supporting code.
r11367
#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:
Jakob Gager
Correct TypeError message in svg2pdf
r13427 raise TypeError("Inkscape svg to pdf conversion failed")