pdf.py
60 lines
| 2.3 KiB
| text/x-python
|
PythonLexer
Jonathan Frederic
|
r11742 | """ | ||
Contains writer for writing nbconvert output to PDF. | ||||
""" | ||||
#----------------------------------------------------------------------------- | ||||
#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 subprocess | ||||
import os | ||||
Thomas Robitaille
|
r11911 | from IPython.utils.traitlets import Integer, List, Bool | ||
Jonathan Frederic
|
r11742 | |||
Jonathan Frederic
|
r11747 | from .base import PostProcessorBase | ||
Jonathan Frederic
|
r11742 | |||
#----------------------------------------------------------------------------- | ||||
# Classes | ||||
#----------------------------------------------------------------------------- | ||||
Jonathan Frederic
|
r11747 | class PDFPostProcessor(PostProcessorBase): | ||
Jonathan Frederic
|
r11742 | """Writer designed to write to PDF files""" | ||
iteration_count = Integer(3, config=True, help=""" | ||||
How many times pdflatex will be called. | ||||
""") | ||||
MinRK
|
r12058 | command = List(["pdflatex", "{filename}"], config=True, help=""" | ||
Jonathan Frederic
|
r11745 | Shell command used to compile PDF.""") | ||
Jonathan Frederic
|
r11747 | verbose = Bool(False, config=True, help=""" | ||
Whether or not to display the output of the compile call. | ||||
""") | ||||
def call(self, input): | ||||
Jonathan Frederic
|
r11742 | """ | ||
Consume and write Jinja output a PDF. | ||||
See files.py for more... | ||||
""" | ||||
Thomas Robitaille
|
r11922 | command = [c.format(filename=input) for c in self.command] | ||
MinRK
|
r12058 | self.log.info("Building PDF: %s", command) | ||
with open(os.devnull, 'rb') as null: | ||||
stdout = subprocess.PIPE if not self.verbose else None | ||||
Paul Ivanov
|
r11961 | for index in range(self.iteration_count): | ||
MinRK
|
r12058 | p = subprocess.Popen(command, stdout=stdout, stdin=null) | ||
out, err = p.communicate() | ||||
if p.returncode: | ||||
if self.verbose: | ||||
MinRK
|
r12061 | # verbose means I didn't capture stdout with PIPE, | ||
# so it's already been displayed and `out` is None. | ||||
MinRK
|
r12058 | out = u'' | ||
else: | ||||
out = out.decode('utf-8', 'replace') | ||||
self.log.critical(u"PDF conversion failed: %s\n%s", command, out) | ||||
return | ||||