Converting an HTML Page to a PDF and Downloading it in Django

Posted by

Convert HTML page to PDF and download as PDF in Django

Converting HTML page to PDF in Django

In this article, we will learn how to convert an HTML page to a PDF file and provide a download link for the PDF in a Django application.

First, we need to install the necessary dependencies. We can use the django-wkhtmltopdf package to achieve this. Install it using the command:

pip install django-wkhtmltopdf

Next, we need to add 'wkhtmltopdf' to the INSTALLED_APPS in the settings.py file.

INSTALLED_APPS = [
    ...
    'wkhtmltopdf',
    ...
]

Now, we can create a view that returns the HTML page as a PDF file. We can use the pdftemplate_view decorator to achieve this. For example:

from wkhtmltopdf.views import PDFTemplateView

class PDFDownloadView(PDFTemplateView):
    template_name = 'my_template.html'

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        # Add context data if needed
        return context

pdf_download_view = PDFDownloadView.as_view()

Finally, we can add a URL pattern for the PDF download view and create a link in a template to download the PDF file. For example:

# urls.py
from django.urls import path
from .views import pdf_download_view

urlpatterns = [
    ...
    path('download-pdf/', pdf_download_view, name='download_pdf'),
    ...
]

# my_template.html
Download PDF

Now, when a user visits the /download-pdf/ URL, they will be able to download the HTML page as a PDF file.

With these steps, we have successfully converted an HTML page to a PDF file and provided a download link for the PDF in a Django application.

0 0 votes
Article Rating
12 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@reynaldrodriguez8705
6 months ago

hey mine is just displayed in the page i tried to generate it but its not generating it just displayed

def generate_pdf(request, travel_id):

destinations = Destination.objects.all()

pdf = pdfkit.from_url(request.build_absolute_uri(reverse('pdf_file')), False, configuration=config)

response = HttpResponse(pdf,content_type = 'application/pdf')

response['Content-Disposition'] = 'attachment;filename="Travel_request.pdf"'

context = {'travel_id': travel_id, 'destinations': destinations}

return render(request, 'requester/pdf.html', context)

@Eng-Mohamed-Ahmed
6 months ago

Thanks bro but also is reading same browser is same or nor?

@venkatrao235
6 months ago

tq sir , is it work on server after deployment and how wkhtmltopdf work on server

@codingmaster4535
6 months ago

can this code also download html having charts as pdf?

@_Edit889
6 months ago

This tutorial saved me so much time and frustration. I had been trying to figure out how to convert Django web pages to PDF format on my own, but this video made it so much easier.

@faruktuncel
6 months ago

I really appreciate the fact that the code examples in this tutorial are so well-commented and easy to understand. Makes it so much easier to implement in my own projects.

@sametdulger1897
6 months ago

I'm so glad I stumbled upon this tutorial! Now I can create professional-looking PDFs of my Django web pages with just a few lines of code. Thanks for sharing!

@bay_noob3983
6 months ago

This video was so easy to follow along with, even for someone like me who doesn't have much experience with Django. Highly recommend it

@safakknal
6 months ago

As a beginner to Django, I found this tutorial to be incredibly helpful. It's great to know that I can create PDFs of my web pages with just a few lines of code.

@ercanerdem3189
6 months ago

I had no idea that you could convert HTML pages to PDF format so easily in Django. Thanks for sharing this useful information!

@EmirYand
6 months ago

Thanks to this video, I was able to add a PDF download button to my Django web app in just a few minutes. Such a game-changer!

@antoniom.sparkman8748
6 months ago

Thanks for your valuable tips. It really helps me.