This repository has been archived on 2026-10-23. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
mbftns/main/pdf.py
T
2012-02-02 23:27:36 +01:00

34 lines
1.2 KiB
Python

#coding: utf-8
# Ref:
# - http://stackoverflow.com/questions/1377446/html-to-pdf-for-a-django-site
# - http://stackoverflow.com/questions/2179958/django-pisa-adding-images-to-pdf-output ?
# - http://web.archive.org/web/20090413054650/http://www.xhtml2pdf.com/doc/pisa-en.html
import os
from cgi import escape
from django.template.loader import render_to_string
from django.template import RequestContext
from django.conf import settings
from django.http import HttpResponse
import cStringIO as StringIO
import ho.pisa as pisa
def render_to_pdf(template_src, context_dict, context_instance):
html = render_to_string(template_src, context_dict, context_instance=context_instance)
result = StringIO.StringIO()
pdf = pisa.pisaDocument(
StringIO.StringIO(html.encode("UTF-8")),
dest=result,
link_callback=fetch_resources,
encoding='UTF-8')
if not pdf.err:
return HttpResponse(result.getvalue(), mimetype='application/pdf')
return HttpResponse('We had some errors<pre>%s</pre>' % escape(html))
def fetch_resources(uri, rel):
# Replace the url with the physical path.
path = os.path.join(settings.STATIC_ROOT, uri.replace(settings.STATIC_URL, ""))
return path