2015-05-15 14:19:24 +00:00
|
|
|
import textwrap
|
|
|
|
|
2015-10-01 16:02:26 +00:00
|
|
|
from django.templatetags.static import static
|
2021-05-12 11:55:47 +00:00
|
|
|
from django.utils.html import format_html
|
2023-10-24 16:59:02 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2015-10-01 16:02:26 +00:00
|
|
|
|
2015-04-05 10:46:24 +00:00
|
|
|
from orchestra.utils.sys import run
|
2014-09-03 22:01:44 +00:00
|
|
|
|
|
|
|
|
2015-05-28 09:43:57 +00:00
|
|
|
def html_to_pdf(html, pagination=False):
|
2014-09-03 22:01:44 +00:00
|
|
|
""" converts HTL to PDF using wkhtmltopdf """
|
2015-05-28 09:43:57 +00:00
|
|
|
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 \\
|
2015-05-28 09:43:57 +00:00
|
|
|
--footer-spacing 7"""
|
|
|
|
) if pagination else '',
|
|
|
|
}
|
|
|
|
cmd = textwrap.dedent("""\
|
|
|
|
PATH=$PATH:/usr/local/bin/
|
2023-10-24 16:59:02 +00:00
|
|
|
xvfb-run -a -s "-screen 0 2480x3508x16" wkhtmltopdf -q \\
|
|
|
|
--enable-local-file-access \\
|
2015-05-28 09:43:57 +00:00
|
|
|
--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 - - \
|
2015-05-28 09:43:57 +00:00
|
|
|
""") % context
|
|
|
|
return run(cmd, stdin=html.encode('utf-8')).stdout
|
2015-10-01 16:02:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_on_site_link(url):
|
|
|
|
context = {
|
2016-02-17 12:32:30 +00:00
|
|
|
'title': _("View on site %s") % url,
|
2015-10-01 16:02:26 +00:00
|
|
|
'url': url,
|
2021-05-12 11:55:47 +00:00
|
|
|
'image': format_html('<img src="{}"></img>', static('orchestra/images/view-on-site.png')),
|
2015-10-01 16:02:26 +00:00
|
|
|
}
|
2021-05-12 11:55:47 +00:00
|
|
|
return format_html('<a href="{url}" title="{title}">{image}</a>', **context)
|