I\'m using a standard example from the web (http://www.20seven.org/journal/2008/11/pdf-generation-with-pisa-in-django.html) to convert a django view / template into a PDF.>
def render_to_pdf( template_src, context_dict):
template = get_template(template_src)
context = Context(context_dict)
html = template.render(context)
result = StringIO.StringIO()
if page has an image.something:
pdf = pisa.pisaDocument(StringIO.StringIO(html.encode("UTF-8")), dest=result, link_callback=fetch_resources)
else no image.something :
pdf = pisa.pisaDocument(StringIO.StringIO(html.encode("UTF-8")),result)
if not pdf.err:
return HttpResponse(result.getvalue(), mimetype='examination_report/pdf')
return HttpResponse('We had some errors%s
' % escape(html))
def fetch_resources(uri, rel):
if os.sep == '\\': # deal with windows and wrong slashes
uri2 = os.sep.join(uri.split('/'))
else:# else, just add the untouched path.
uri2 = uri
path = '%s%s' % (settings.SITE_ROOT, uri2)
return path