fpdf controller/view question

425 views
Skip to first unread message

Dave S

unread,
Apr 22, 2016, 9:29:58 PM4/22/16
to web2py-users
Back when, I played around with the fpdf tutorial, and built a controller that created the "Hello, World" example.  Recently, I revisited that and added some real data, taking the last 25 rows from my table-of-interest.  And I got that to display nicely, in a manner similar to my regular page using an  SQLTABLE(records).  I even managed to do the even/odd striping.  But of course, a lot of that example was having formatting stuff ("view") in my controller.  So I went to move it into a real view file, but when I browsed to that location, the browser had the pdf viewer open but no document.

The view looked like

{{
import os
 
from gluon.contrib.fpdf import FPDF


  response
.headers['Content-Type'] = "application/pdf"
  pdf
= FPDF()
  pdf
.add_page()
  pdf
.set_font('Arial', 'I', 14)
  pdf
.cell(40, 10, "Hello, ExampleServer World")
  pdf
.ln()
  pdf
.set_font('Arial', '', 10)
  pdf
.set_fill_color(210,210,240)
 
print "found %d results" % (len(results))
 
for row in results:
    fill
= 1 if row.id % 2 else 0
    pdf
.cell(20, 5, str(row.id), '', 0, '', fill)
    pdf
.cell(50, 5, row.eventtime.isoformat(), '', 0, '', fill)
    pdf
.cell(40, 5, row.eventstatus, '', 0, '', fill)
    pdf
.cell(20, 5, row.client, '', 0, '', fill)
    pdf
.cell(9, 5, str(fill), '', 1, 'C', fill)
 
pass
 
print "trying to do pdf,output"
 
= pdf.output('tut01.pdf', 'S')
}}


I got things to work again by changing the last line to
  stuff = open("/tmp/stuff.pdf", 'w')
  stuff
.write(pdf.output('tut01.pdf', 'S'))
  stuff
.close()
  response
.stream("/tmp/stuff.pdf")


why didn't the first version work?  That's what generic.pdf does via gluon/contrib/generic.py's html_to_pdf(), I believe, which I was using as a guide.

I will note that I did have problems with generic.pdf, but I need to research that more.  The first issue I could work around, not having PIL available to process my logo's .png (removed the logo, which was called out in layout.html since this is an older app), but there was something else that gave me an error, as well. When I go back and look at that, I may have another question for the list.

/dps


Reply all
Reply to author
Forward
0 new messages