From html view to pdf.

335 views
Skip to first unread message

annet

unread,
Oct 24, 2011, 3:44:04 AM10/24/11
to web2py-users
I am working on a timetable function:

Controller:

def classtimesweek():

rows=db((db.lesrooster.bedrijf_id==1)&(db.lesrooster.dag_id==db.dag.id)&(db.lesrooster.level_id==db.level.id))
\
.select(db.lesrooster.ALL,db.dag.dag,db.level.level,orderby=db.lesrooster.dag_id|
db.lesrooster.tijd)
return dict(rows=rows)


View:

<table id="init" class="display" style="margin-top: 12px;">
<thead>
...
</thead>
<tbody>
...
</tbody>
</table>


Now I would like the user to be able to download the timetable as a
pdf and print it. What is the most straightforward way to do this?


Kind regards,

Annet.

chris_g

unread,
Oct 24, 2011, 10:23:16 PM10/24/11
to web2py-users
Annet,

When I explored this a few years ago, I ended up going with an XML
solution called RML (report markup language). This was developed by
the makers of reportlab, but there are also free implemntations of RML
available (such as the z3c implementation.)
Once I had installed rml2pdf developed XML templates, I used a pdf
view like this in order to render my RML to pdf.
RML is very well documented, and I think you will find examples that
are very similar to your requirements.

Chris


{{
import gluon.contenttype
import StringIO
import z3c.rml.rml2pdf as rml2pdf

resp = StringIO.StringIO()
rml = response.render('%s/%s.xml' %
(request.controller,request.function))
filename = '%s.pdf'%request.function
resp = rml2pdf.parseString(rml, filename=filename) #creates the
pdf
response.headers['Content-Type'] =
gluon.contenttype.contenttype('.pdf')
response.headers['Content-disposition'] = "attachment; filename=
\"%s\"" % filename
response.write(resp.read(), escape=False)
}}

apple

unread,
Oct 25, 2011, 7:42:12 AM10/25/11
to web2py-users
Very easy using pisa. You also need to install reportlab and html5lib
but once these have been installed it is simple:

# to directly get the html for a view
html = response.render("myview.html", locals())

filename = "C://outputpdf.pdf"
f = file(filename, "wb")
pdf = pisa.CreatePDF(html, f)
f.close()
if not pdf.err:
pisa.startViewer(filename)

The only thing is I found there was actually a pdf generator included
with web2py called pyfpdf. I was all set to replace pisa with this but
now it seems to have disappeared from the book and I can find no
mention of it. Would be useful to have a pdf generator included in the
box with web2py - why was it removed?

Massimo Di Pierro

unread,
Oct 25, 2011, 9:16:40 AM10/25/11
to web2py-users
web2py has a generic.pdfIf you just return html and view the page
with .pdf it will be serialized in pdf using pyfpdf. Not all html can
be serialized this way.

annet

unread,
Oct 25, 2011, 9:25:24 AM10/25/11
to web2py-users
Thanks for your replies.

Massimo,

How do I use generic.pdf in case these are my controller and view:


Controller:

def classtimesweek():
rows=db((db.lesrooster.bedrijf_id==1)&(db.lesrooster.dag_id==db.dag.id)&
\
(db.lesrooster.level_id==db.level.id))\
.select(db.lesrooster.ALL,db.dag.dag,db.level.level,orderby=db.lesrooster.dag_id|
\
db.lesrooster.tijd)
return dict(rows=rows)

View:

{{extend 'viewlayout.html'}}

<table id="init" class="display" style="margin-top: 12px;">
<thead>
...
</thead>
<tbody>
...
</tbody>
</table>



kind regards,

Annet.

Paolo Caruccio

unread,
Oct 25, 2011, 11:18:07 AM10/25/11
to web...@googlegroups.com
I used succesfully web2py-appreport.


Ciao.

Paolo

Massimo Di Pierro

unread,
Oct 25, 2011, 12:17:08 PM10/25/11
to web2py-users
First time I see this. Cool!


On Oct 25, 10:18 am, Paolo Caruccio <paolo.carucci...@gmail.com>
wrote:

Bruno Rocha

unread,
Oct 25, 2011, 1:19:25 PM10/25/11
to web...@googlegroups.com

http://labs.blouweb.com/web2pyappreport

my friend Lucas created it. now it can work with Pisa and Pyfpdf

Ovidio Marinho

unread,
Oct 25, 2011, 2:41:41 PM10/25/11
to web...@googlegroups.com
I used appreport  and very good, this could be integrated with web2py.
      


       Ovidio Marinho Falcao Neto
                Web Developer
             ovid...@gmail.com 
                 ITJP - itjp.net.br
               83   8826 9088 - Oi
               83   9334 0266 - Claro
                        Brasil



2011/10/25 Massimo Di Pierro <massimo....@gmail.com>
Reply all
Reply to author
Forward
0 new messages