Welcome, guest | Sign In | My Account | Store | Cart

Most people know how to write a page with HTML and CSS. Why not using these skills to dynamically generate PDF documents using it? The open source project "pisa" http://www.htmltopdf.org enables you to to this quite simple in a pythonic way.

Python, 49 lines
 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
import cStringIO
import ho.pisa as pisa
import os

# Shortcut for dumping all logs on screen
pisa.showLogging()

def HTML2PDF(data, filename, open=False):

    """
    Simple test showing how to create a PDF file from
    PML Source String. Also shows errors and tries to start
    the resulting PDF
    """

    pdf = pisa.CreatePDF(
        cStringIO.StringIO(data),
        file(filename, "wb"))

    if open and (not pdf.err):
        os.startfile(str(filename))

    return not pdf.err

if __name__=="__main__":
    HTMLTEST = """
    <html><body>
    <p>Hello <strong style="color: #f00;">World</strong>
    <hr>
    <table border="1" style="background: #eee; padding: 0.5em;">
        <tr>
            <td>Amount</td>
            <td>Description</td>
            <td>Total</td>
        </tr>
        <tr>
            <td>1</td>
            <td>Good weather</td>
            <td>0 EUR</td>
        </tr>
        <tr style="font-weight: bold">
            <td colspan="2" align="right">Sum</td>
            <td>0 EUR</td>
        </tr>
    </table>
    </body></html>
    """

    HTML2PDF(HTMLTEST, "test.pdf", open=True)

For the conversion these third party modules are used: HTML5lib, Reportlab and TG CSS Parser. All components are written in pure Python to ensure platform independency. More Informations on http://www.htmltopdf.org

This approach is very useful for web applications. Examples for integration with WSGI, Django, Turbogears, CherryPy and simple CGI are provided.

2 comments

Michael Shepanski 14 years, 2 months ago  # | flag

os.startfile is a windows only command. Other than that, works great.

Rahul 13 years, 3 months ago  # | flag

Dose this work for complex web pages. I want to convert webpages directly to python. I tried to copy the html source of the webpage and inserted in the HTML TEST part. But it shows an error...Is there any way out of this ?