mirror of https://github.com/interlegis/sigi.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
27 lines
904 B
27 lines
904 B
import cStringIO as StringIO
|
|
import os
|
|
from cgi import escape
|
|
|
|
import ho.pisa as pisa
|
|
from django.conf import settings
|
|
from django.http import HttpResponse
|
|
from django.template import Context
|
|
from django.template.loader import get_template
|
|
from django.utils.translation import ugettext as _
|
|
|
|
|
|
def fetch_resources(uri, rel):
|
|
path = os.path.join(settings.MEDIA_ROOT, uri.replace(settings.MEDIA_URL, ""))
|
|
return path
|
|
|
|
|
|
def render_to_pdf(template_src, context_dict):
|
|
template = get_template(template_src)
|
|
context = Context(context_dict)
|
|
html = template.render(context)
|
|
result = StringIO.StringIO()
|
|
|
|
pdf = pisa.pisaDocument(StringIO.StringIO(html.encode('utf-8')), result, link_callback=fetch_resources)
|
|
if not pdf.err:
|
|
return HttpResponse(result.getvalue(), content_type='application/pdf')
|
|
return HttpResponse(_(u'We had some errors<pre>%s</pre>') % escape(html))
|
|
|