Generate PDF with XHTML2PDF Pisa in Django Examples


Generating PDF using XHTML2PDF pisa in Django is pretty easy. Here are example / cases how to generate PDF in Django. First things to do is installing XHTML2PDF and PIL with Zlib support.

In this example, we will generate templates/app/test.html.

views.py :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import os
import StringIO

from xhtml2pdf import pisa

from django.conf import settings
from django.http import HttpResponse
from django.shortcuts import render
from django.template.loader import get_template
from django.template.context import Context
from django.utils.html import escape


def fetch_resources(uri, rel):
    """
    Callback to allow xhtml2pdf/reportlab to retrieve Images,Stylesheets, etc.
    `uri` is the href attribute from the html link element.
    `rel` gives a relative path, but it’s not used here.

    """
    if uri.startswith(settings.MEDIA_URL):
        path = os.path.join(settings.MEDIA_ROOT,
                            uri.replace(settings.MEDIA_URL, ""))
    elif uri.startswith(settings.STATIC_URL):
        path = os.path.join(settings.STATIC_ROOT,
                            uri.replace(settings.STATIC_URL, ""))
    else:
        path = os.path.join(settings.STATIC_ROOT,
                            uri.replace(settings.STATIC_URL, ""))

        if not os.path.isfile(path):
            path = os.path.join(settings.MEDIA_ROOT,
                                uri.replace(settings.MEDIA_URL, ""))

            if not os.path.isfile(path):
                raise UnsupportedMediaPathException(
                                    ‘media urls must start with %s or %s’ % (
                                    settings.MEDIA_ROOT, settings.STATIC_ROOT))

    return path


def render_to_pdf(template_src, context_dict):
    """Function to render html template into a pdf file"""
    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")),
                                            dest=result,
                                            encoding=’UTF-8′,
                                            link_callback=fetch_resources)
    if not pdf.err:
        response = HttpResponse(result.getvalue(),
                                                    mimetype=’application/pdf’)

        return response

    return HttpResponse(‘We had some errors<pre>%s</pre>’ % escape(html))


def download_pdf(request):
    """Build briefing packages format and export as HTML and PDF."""
    response = HttpResponse(content_type=’application/pdf’)
    return generate_pdf(‘app/test.html’, file_object=response)

Example test.hml :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
{% extends ‘base.html’ %}
{% load url from future %}

{% block css %}
    <link href="{{ STATIC_URL }}app/styles/style.css" rel="stylesheet" type="text/css">    
{% endblock %}

{% block content %}
<div id="wrap">
    <div id="page_1">
    <div id="header">
        <div id="logo">
              <img src='{{ STATIC_URL }}app/images/logo.png’ class="top_image" />
        </div>
        </div>

….
{% endblock %}

Another complete example of PISA XTML2PDF usage :

https://github.com/chrisglass/xhtml2pdf/tree/master/doc


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.