Ad Code



Django html to pdf using xhtml2pdf


Django html to pdf using xhtml2pdf
In somewhere if want to design an Html page which is for documentation purposes it is used dynamic content render by the back-end technology like Python, PHP Nodejs, etc.

For example, I want to create result formate, mark sheet of online examination, invoicing of the e-commerce and POS and any other documentation like that which is based on the web project create based on the order details or buy or sell basic.

This is possible because there has logic to generate Html design into the pdf excel and docs files.

Craete Html to PDF using Django:-

Before we started we assume you have already installed and set up the Django project on your local or live server.

Then you can first install the following command in the project environment or server,

pip install django-xhtml2pdf

After installing the Django-xhtml2pdf package you make an Html design, we assume you have an Html design that you want to convert into pdf using Django, but here we are given the basic structure of Html.

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "">
<html xmlns="" xmlns:py="">

    <meta http-equiv="content-type" content="text/html; charset=utf-8">
    <meta name="description" content="Invoice demo sample">
		/* important style for the xhtml2pdf*/
        @page {
            size: A4;
            margin: 1cm;
		table {
            -pdf-keep-in-frame-mode: shrink;
            -pdf-keep-with-next: true;
	<!-- contents -->

In this code style section of the head, a tag is important according to the xhtml2pdf package which makes the perfect design print Html table otherwise it may occur a design issue.

Also, you can use inside this Html code jinja template which is the default template of the Django framework and also make the dynamic Html to pdf to pass parameters in the Html view.

After this, you can make the view functions and set them to the route file to access via url using the following code:-

import the required library

from django.http import HttpResponse
from django_xhtml2pdf.utils import generate_pdf

Make the view file invoice function

def invoice_print(request):
    resp = HttpResponse(content_type='application/pdf')
    result = generate_pdf('invoice.html', file_object=resp)
    return result

In the above code, you can also pass the url parameters in the function use the inside view, and make the dynamic data.

generate_pdf() function also you can pass a dynamic context and link_callable function for the dynamic data and media and static file url.

After writing this code you make the route file via access through the url of the project.

urlpatterns = [
    path('invoice', invoice_print, name='generate_invoide'),

You can also add dynamic route param for the dynamic data access like order id and print the specific order data.

In this article, I have to use the Django-xhtml2pdf library of the Django framework and make the Html design to pdf with dynamic parameters. You can use this code with some modifications in your real-life projects also.

Post a Comment


Ad Code