HTML to PDF Django Tutorial – Converting HTML to PDF

Hi friends, Welcome to HTML To PDF Django Tutorial. Sharing HTML files is very difficult task because of incompatibilities across different browsers.So we need to have  convert HTML files into PDF format for easily sharing.In this tutorial we will learn how to convert HTML files into PDF in python.

Files of a web page is written in HTML(Hyper Text Markup Language).Portable Document Format (PDF) is a file format used to present and exchange documents reliably, independent of software, hardware, or operating system

When we convert a webpage into pdf ,all the associated files such as HTML,images, Adobe FLA files, Cascading Style Sheets, text files, image maps, and forms are also concerned in conversion process.The converted pdf files are similar to the original webpage.So now let’s start converting HTML files into PDF.

Converting HTML To PDF Django Tutorial

Converting HTML to Pdf in Django is not more difficult.Now, we will learn how to implement it.

xhtml2pdf library

  • Python provides xhtml2pdf library to convert html files into pdf files.
  • xhtml2pdf is a html2pdf converter using the ReportLab Toolkit, the HTML5lib and pyPdf.
  • It supports HTML 5 and CSS 2.1 (and some of CSS 3). It is completely written in pure Python so it is platform independent.
  • The main benefit of this tool that a user with Web skills like HTML and CSS is able to generate PDF templates very quickly without learning new technologies.
  •   For more information about xhtml2pdf refer this link.

Installing xhtml2pdf

This can be installed similar to other typical python library.Go to terminal and run the following command.

Creating a new project

Now create a new project and named it as your wish.If you are getting problem in creating new project then don’t worry just check this link.

Opening Project in PyCharm

Now  open PyCharm(i am using PyCharm IDE but you can use anyone) and open the project we created with power shell.

HTML to PDF Django
HTML to PDF Django

Creating utils.py file

Now come inside your app i.e., HTMLtoPDF  and create a utils.py file.

Inside this file we will implement render_to_pdf function.

Creating a Template

Now create a directory templates and inside this create a html file and named it as invoice.html.Now write the following code in invoice.html .

“A Django Template is a sequence of Text that helps in separation of the presentation layer of a document from its data”. For understanding templates deeply you can read this link.

Rendering Template

  • Now for rendering template we have to create a view.
  • Now inside  our app(HTMLtoPDF)  create a views.py file.
  • If you are not familiar with what is view then don’t worry – Views are just python function. Views take user request and give them back something. Like user will request there profile and view will show him his profile.And if you want to know more about views then this link is helpful for you.
  • Write the following code.

What we did?

  • We have just created a view GeneratePdf(View) and pass the view as parameter.
  • Define a get() method but of course you can use any other method.
  • Inside the function we create our template from render_to_pdf.
  • And then finally rendering the template in HttpResponse.
  • content_type=’application/pdf’ means MIME types. application/pdf is used for pdf format.

Defining a URL

Now we need to define a URL to see our template.For this we have to do such things –

  • Inside urls.py file of the project and write the below code.

Defining Template Directory in Settings

And now this is the most important point to be noted.If we don’t do this then get a template not found error.For avoiding this error, we have to set templates/ in settings.py file.It is essential to render the pdf.

Running Your Project

  • Now we have to run our project to see output.
  • For running, go to terminal and run the following command.

  • After running this command you will see the following information.
HTML to PDF Django
HTML to PDF Django
  • Now click on server that will navigate you to the browser.
  • Now enter this URL http://127.0.0.1:8000/pdf/ and this will show the template which we have created.
HTML to PDF Django
HTML to PDF Django

Finally, our project is running successfully and you can see now how our HTML file converted into PDF.

So that’s all for this HTML To PDF Django Tutorial. Feel free to leave your comments if having any queries regarding this HTML To PDF Django Tutorial. And you like this then please share with your friends.We will do some more cool stuffs in upcoming tutorials. Thank You 🙂

Leave a Comment