Source

shinypress / shinypress / pdf.py

from os.path import basename
from shinypress.jinja import env
from string import lowercase
from StringIO import StringIO

from random import choice

def selection(seq, n):
    return (choice(seq) for i in xrange(n))

template = env.from_string(r"""
%% for page in range(1, n_pages+1) %%
\begin{frame}
\includegraphics[width=\linewidth,page=<page>]{<filename>}
\end{frame}
%% endfor %%
""")

def render(spec):
    n_pages, filename = spec.split(' ', 1)
    n_pages = int(n_pages)
    content = open(filename, 'rb').read()
    f = StringIO(content)
    f.name = ''.join(selection(lowercase, 12)) + '.pdf'
    filename = basename(f.name)
    return (template.render(filename=filename, n_pages=n_pages), [f])