Dynamic pages in pdf format

135 views
Skip to first unread message

Mehmet Kose

unread,
Mar 26, 2013, 10:44:31 AM3/26/13
to python-...@googlegroups.com
hi,
I want to publish the page contents in pdf format. "site.com/something.html" and "site.com/something.pdf" for example.
I did some research about pisa. python code that generates pdf with pisa;

import ho.pisa as pisa
def helloWorld():
    filename = "/srv/www/hello.pdf"
    pdf = pisa.CreatePDF(
    "Hello <strong>World</strong>",
    file(filename, "wb"))
    if not pdf.err:
        pisa.startViewer(filename)

if __name__=="__main__":
    pisa.showLogging()
    helloWorld()

how can I do dynamic output it into the handler?

class PdfHandler(BaseHandler):
    def get(self, slug):
        import ho.pisa as pisa
        self.set_header("Content-Type", "application/pdf")
        ...
        ...

Mehmet Kose

unread,
Mar 27, 2013, 12:31:05 PM3/27/13
to python-...@googlegroups.com
wkhtmltox looks nice. but I still do not know how I created the PDF handler in the dump :/

2013/3/27 Brian Morgan <brian.s...@gmail.com>
I have done this in pylons and not tornado, but I would recommend wkhtmltopdf (https://code.google.com/p/wkhtmltopdf/) over pisa.  It does a much better job rendering html.  With pisa I found we had to do table based layouts vs using floats and a lot of other hacks to get it to display pretty basic styling.  There is not a python api for it, but you can call it via a system call using temp files.
--
You received this message because you are subscribed to the Google Groups "Tornado Web Server" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python-tornad...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Russ Weeks

unread,
Mar 27, 2013, 12:38:23 PM3/27/13
to python-...@googlegroups.com
Brian, are you using the python bindings for wkhtmltopdf or are you running it from the command line?  Is there any way to pass in an HTML string plus a set of CSS styles, or is it strictly HTTP-based?

I've been using weasyprint because it renders better than pisa and seems to be more actively developed.  But the rendering is not as good as a proper webkit-based solution and it chokes on large HTML documents.

-Russ

Brian Morgan

unread,
Mar 27, 2013, 12:49:11 PM3/27/13
to python-...@googlegroups.com
Mehmet-
  See attached for a working example.  You will need to have wkhtmltopdf installed locally.  I have not tried this on a non-unix system.

-Brian
app.py

Brian Morgan

unread,
Mar 27, 2013, 12:50:42 PM3/27/13
to python-...@googlegroups.com, rwe...@newbrightidea.com
See my working example in my response to Mehmet.  I am passing in css styles in the html, but you can include references to other local resources like images and css files.

-Brian
Reply all
Reply to author
Forward
0 new messages