Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def renderPdf(template, content={}):
t = get_template(template)
send_data = t.render(content)
result = BytesIO()
pdf = pisa.pisaDocument(BytesIO(send_data.encode("ISO-8859-1")), result)
if not pdf.err:
return HttpResponse(result.getvalue(), content_type='application/pdf')
else:
return None
def generate_pdf_stringio(html):
""" Convert html input to pdf format """
result = StringIO.StringIO()
pdf = pisa.pisaDocument(StringIO.StringIO(
html.encode("UTF-8")), result)
if not pdf.err:
return result
return None
print("Channel query time:", time.time() - start)
site = get_current_site(request)
default_thumbnail = get_default_thumbnail()
channel_list = [get_channel_data(c, site, default_thumbnail) for c in channels]
context = Context({
"channels": channel_list
})
html = template.render(context)
result = BytesIO()
pdf = pisa.pisaDocument(BytesIO(html.encode("UTF-8")), result, encoding='UTF-8', path=settings.STATIC_ROOT)
if not pdf.err:
response = FileResponse(result.getvalue())
response['Content-Type'] = 'application/pdf'
response['Content-disposition'] = 'attachment;filename=channels.pdf'
response['Set-Cookie'] = "fileDownload=true; path=/"
print("\n\n\nTotal time:", time.time() - start, "\n\n\n")
return response
def content_to_pdf(content, dest, encoding='utf-8', **kwargs):
"""
Write into *dest* file object the given html *content*.
Return True if the operation completed successfully.
"""
from xhtml2pdf import pisa
src = StringIO(content.encode(encoding))
pdf = pisa.pisaDocument(src, dest, link_callback=fetch_resources, **kwargs)
return not pdf.err
def html_to_pdf(content, encoding="utf-8",
link_callback=fetch_resources, **kwargs):
"""
Converts html ``content`` into PDF document.
:param unicode content: html content
:returns: PDF content
:rtype: :class:`bytes`
:raises: :exc:`~easy_pdf.exceptions.PDFRenderingError`
"""
src = BytesIO(content.encode(encoding))
dest = BytesIO()
pdf = pisa.pisaDocument(src, dest, encoding=encoding,
link_callback=link_callback, **kwargs)
if pdf.err:
logger.error("Error rendering PDF document")
for entry in pdf.log:
if entry[0] == xhtml2pdf.default.PML_ERROR:
logger_x2p.error("line %s, msg: %s, fragment: %s", entry[1], entry[2], entry[3])
raise PDFRenderingError("Errors rendering PDF", content=content, log=pdf.log)
if pdf.warn:
for entry in pdf.log:
if entry[0] == xhtml2pdf.default.PML_WARNING:
logger_x2p.warning("line %s, msg: %s, fragment: %s", entry[1], entry[2], entry[3])
return dest.getvalue()
htmldata = htmldata.replace("font-size: x-small", "font-size: 8pt")
htmldata = htmldata.replace("font-size: small", "font-size: 10pt")
htmldata = htmldata.replace("font-size: medium", "font-size: 14pt")
htmldata = htmldata.replace("font-size: large", "font-size: 18pt")
htmldata = htmldata.replace("font-size: x-large", "font-size: 24pt")
htmldata = htmldata.replace("font-size: xx-large", "font-size: 36pt")
# Remove any img tags with signature:placeholder/user as the src
htmldata = re.sub(r'', '', htmldata)
# Fix up any google QR codes where a protocol-less URI has been used
htmldata = htmldata.replace("\"//chart.googleapis.com", "\"http://chart.googleapis.com")
# Switch relative document uris to absolute service based calls
htmldata = fix_relative_document_uris(dbo, htmldata)
# Do the conversion
from xhtml2pdf import pisa
out = bytesio()
pdf = pisa.pisaDocument(stringio(header + htmldata + footer), dest=out)
if pdf.err:
raise IOError(pdf.err)
return out.getvalue()
def render_to_pdf_response(template_name, context_dict):
try:
template = loader.get_template(template_name)
context = Context(context_dict)
html = template.render(context)
except TemplateDoesNotExist:
html = ''
result = StringIO.StringIO()
pdf = pisa.pisaDocument(StringIO.StringIO(html.encode('utf-8')), result)
if not pdf.err:
response = HttpResponse(result.getvalue(), mimetype='application/pdf')
else:
response = render_to_response_custom(template_name, context_dict)
return response