django-orchestra-test/orchestra/utils/html.py

25 lines
747 B
Python
Raw Normal View History

2015-05-15 14:19:24 +00:00
import textwrap
2015-04-05 10:46:24 +00:00
from orchestra.utils.sys import run
def html_to_pdf(html, pagination=False):
""" converts HTL to PDF using wkhtmltopdf """
context = {
'pagination': textwrap.dedent("""\
2015-05-15 14:19:24 +00:00
--footer-center "Page [page] of [topage]" \\
--footer-font-name sans \\
--footer-font-size 7 \\
--footer-spacing 7"""
) if pagination else '',
}
cmd = textwrap.dedent("""\
PATH=$PATH:/usr/local/bin/
xvfb-run -a -s "-screen 0 2480x3508x16" wkhtmltopdf -q \\
--use-xserver \\
%(pagination)s \\
2015-05-15 14:19:24 +00:00
--margin-bottom 22 \\
2015-05-30 14:44:05 +00:00
--margin-top 20 - - \
""") % context
return run(cmd, stdin=html.encode('utf-8')).stdout