Reportlab and Pisa

| | Comments (2) | Trackbacks (0) | Previous

Reportlab and Pisa

PDF display or download through a web page is always an excellent idea to impress other if it's done on the fly, and Reportlab and Pisa gives you the great feature to do that.
In my case, i had a small file and using that file i have to create PDF, obviously with too many calculations and plots(Reportlab used). So, if you are using python then none other tool is as good as reportlab and Pisa.
I feel very comfortable to create HTML first and then convert it to PDF using Pisa. Here is the simple solution or you can say kind of HTML template to convert to PDF.
To understand this code, you need to go through Pisa documentation.

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import cStringIO
import ho.pisa as pisa
 
def convert(data, filename, open=False):
    pdfFile = file(filename, "wb")
    pdf = pisa.CreatePDF(
       cStringIO.StringIO(data.encode("ISO-8859-1")),pdfFile)
    pdfFile.close()
    if not pdf.err:
        pisa.startViewer(filename)
 
 
if __name__=="__main__":
    HTML = """<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html>
    <head>
        <meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1" />
        <style type="text/css" media="all">
            @page {
            margin: 0.5cm;
            margin-top:1.8cm;
            margin-bottom: 1cm;
            @frame footer {
            -pdf-frame-content: footerContent;
            bottom: 0.5cm;
            margin-left: 0.75cm;
            margin-right: 0.75cm;
            height: 1cm;
            }
            @frame header {
            -pdf-frame-content: headerContent;
            top: 0.2cm;
            margin-left: 0.75cm;
            margin-right: 0.75cm;
            height: 2cm;
            }
            }
            .footer .head
            {
                position:absolute;
                left:0;
                top:4;
                width:100%;
            }
            table {
            -pdf-keep-in-frame-mode: shrink;
            }
            h1 {
                page-break-before:always;
            }
        </style>
    </head>
    <body>
        <div id="headerContent" class="head">
        <table width="100%" height="100px" border="0" >
            <tr>
            <td wodth="25%"><img src="logo.gif" width="70px" height="80px"/>Company Name</td>
            <td align="center" width="50%"><font face="Helvetica-Bold" size="24"> Title </font></td>
            <td width="25%"><font face="Helvetica" size="4">Sub titles</font></td>
            </tr>
        </table>
        </div><br/></br>
            <h1>YOUR OTHER HTML CONTENT WILL GO HERE..........</h1><br/></br>
            <h1>YOUR OTHER HTML CONTENT WILL GO HERE..........</h1><br/></br>
            <h1>YOUR OTHER HTML CONTENT WILL GO HERE..........</h1><br/></br>
        <div id="footerContent">
              <div class="footer">
            This is footer.. Page <pdf:pagenumber/> 
              </div>
        </div>
 
    </body>
    </html>"""
    convert(HTML, 'pisa_example.pdf')

Say thanks to me if this code helps you :) 

 

Comments

Re: Reportlab and Pisa

Pyoneer | 26/10/2013, 16:04

Thanks for sharing your demo.
Trying to run it I found that 3 modules were missing from my system. Here is how I installed them on Linux:
easy_install html5lib
easy_install reportlab
easy_install pisa

Re: Reportlab and Pisa

Eko | 30/10/2013, 23:34

This is will be beneficial for my next article! Thanks.

Add comment

authimage

About The Author

sharmavivek82

I am a python programmer, my experience ranges from developing plone product to data/text analysis.I also worked on data crunching, python to database interaction(MySQL, Oracle), CGI, Flask, crawler and all basic python packages.