How to load images files style css in pdf using pisa xhtml2pdf on Django


This problem is widely affected by many developer that use pisa xhtml2pdf with Django. Usually, they failed to load images and styles into pdf using Django templates. There are two posibilites here why this is happen.

1. Incorrect of generate PDF without include resources
We should pointing our resource (STATIC and MEDIA) path into link_resource while generating PDF using xhtml2pdf pisa.
To ease our development, we can use utility that created by pisa creator. Django-Xhtml2PDF utility :
https://github.com/chrisglass/django-xhtml2pdf/blob/master/django_xhtml2pdf/utils.py

But, you should patch it “utils.py” (I already submit pull request and waiting) to make it works by :

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
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

For detail, check it here :
https://github.com/yodiaditya/django-xhtml2pdf/blob/master/django_xhtml2pdf/utils.py.

Example usage of utils.py for generate pdf:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
from utils import generate_pdf

from django.http import HttpResponse
from django.shortcuts import render


def show_html(request):
    return render(request, ‘pdf/show_html.html’)


def download(request):
    """Build data and export as PDF."""
    response = HttpResponse(content_type=’application/pdf’)
    return render_to_pdf_response(‘pdf_generator/download.html’,
                                                response, ‘pdf-file-name’)

2. Install PIL with zlib
XHTML2PDF use PIL for generate images. Basically, using “pip install PIL” will not install zlib library. To make it run perfectly, just read my another articles :

Install PIL with zlib decoder

Now you can inserting images and css into PDF using xhtml2pdf 🙂


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.