By continuing to navigate on this website, you accept the use of cookies to serve you more relevant services & content.
For more information and to change the setting of cookies on your computer, please read our Cookie Policy.

Generating PDF Files in Python using xhtml2pdf

There are many ways for generating PDF in python. In this post, I will be presenting PDF files generation with xhtml2pdf.

xhtml2pdf: xhtml2pdf is a HTML/CSS to PDF converter written in Python and therefore platform independent. xhtml2pdf supports for frameworks like Django and simple integration into Python programs. It is also usable as stand alone command line tool.

pisa: pisa is a html2pdf converter using the ReportLab Toolkit, the HTML5lib and pyPdf.

Install xhtml2pdf: You can install it with pip using the command - pip install xhtml2pdf

Next start writing a view that generates a PDF document from HTML content. Consider using the cStringIO library as a temporary holding place for your PDF file. The cStringIO library provides a file-like object interface that is particularly efficient.

from xhtml2pdf import pisa
import cStringIO as StringIO

def html_to_pdf(request):

    # Write your data here.
    html = """
    

Hello.....!

    """     # open output file for writing     result = StringIO.StringIO()     # convert HTML to PDF with the HTML to convert and file handle to recieve result.     pdf = pisa.CreatePDF(src=html,dest=result)     if not pdf.err:         # Create the HttpResponse object with the appropriate PDF headers and          # get the value of the StringIO buffer and write it to the response.         return HttpResponse(result.getvalue(),content_type='application/pdf')     else:         return HttpResponse('Errors')

PDF generation directly using HTML:

 from xhtml2pdf import pisa 
 import cStringIO as StringIO 
 from django.template.loader import get_template 
 from django.template import Context 
 def html_to_pdf_directly(request): 
     template = get_template("template_name.html") 
     context = Context({'pagesize':'A4'}) 
     html = template.render(context) 
     result = StringIO.StringIO() 
     pdf = pisa.pisaDocument(StringIO.StringIO(html), dest=result) 
     if not pdf.err: 
         return HttpResponse(result.getvalue(), content_type='application/pdf') 
     else: return HttpResponse('Errors') 

The above code converts given HTML template to PDF File.

    Posted On
  • 28 August 2013
  • By
  • Micropyramid

Need any Help in your Project?Let's Talk

Latest Comments
Related Articles
Python Arrow To Show Human Friendly Time

Arrow is a python library and command-line tool to genrerate, manipulate dates, times, timestamps.
use of arrow:
With the use of arrow, we can also ...

Continue Reading...
How to access development server publicly using Localtunnel

We do need to expose our local server to hit call-back URLs while programming with other APIs. There is a tool called local tunnel from ...

Continue Reading...
Using Python xlwt generating CSV writer and Excel files

In most of the cases, you need to export the data from your database to different formats. In this post I will show you how ...

Continue Reading...
open source packages

Subscribe To our news letter

Subscribe and Stay Updated about our Webinars, news and articles on Django, Python, Machine Learning, Amazon Web Services, DevOps, Salesforce, ReactJS, AngularJS, React Native.
* We don't provide your email contact details to any third parties