[reportlab-users] Page background image with Platypus?

Erik Wickstrom erik at erikwickstrom.com
Wed Oct 24 12:56:12 EDT 2007


I tried this approach - but I'm starting to get other problems:

Traceback (most recent call last):
File "/usr/local/lib/python2.4/site-packages/django/core/handlers/base.py"
in _real_get_response
81. response = callback(request, *callback_args, **callback_kwargs)
File "/root/journal/gazette/views.py" in some_view
101. doc.build(elements)

AttributeError at /gazette/test/
GazettePageTemplate instance has no attribute 'build'

This is my class so far:
class GazettePageTemplate(PageTemplate):
def __init__(self, id, pageSize=defaultPageSize):
self.pageWidth = pageSize[0]
self.pageHeight = pageSize[1]
frame1 = Frame(inch,
3*inch,
self.pageWidth - 2*inch,
self.pageHeight - 518, id='cover')
PageTemplate.__init__(self, id, [frame1]) # note lack of onPage

def beforeDrawPage(self, canv):
self.canv.drawImage('/root/journal/gazette/paper1.png',0,0,8.5*inch,11*inch)

def some_view(request):
response = HttpResponse(mimetype='application/pdf')
response['Content-Disposition'] = 'attachment; filename=gorilla-gazette.pdf'

buffer = StringIO()

# Our container for 'Flowable' objects
elements = []

# A large collection of style sheets pre-made for us
styles = getSampleStyleSheet()

# A basic document for us to write to 'hello_platypus.pdf'
#doc = SimpleDocTemplate(buffer)#'hello_platypus.pdf')
doc = GazettePageTemplate(buffer)#'hello_platypus.pdf')
doc.pagesize=landscape(letter)

from reportlab.rl_config import defaultPageSize

(MAXWIDTH, MAXHEIGHT) = defaultPageSize

image = Image('/root/journal/gazette/paper1.png')
print MAXWIDTH
print MAXHEIGHT
#elements.append(KeepInFrame(MAXWIDTH, MAXHEIGHT, [image]))
elements.append(KeepInFrame(11*inch, 19*inch, [image]))
# Create two 'Paragraph' Flowables and add them to our 'elements'
elements.append(Paragraph("The Platypus", styles['Heading1']))
elements.append(Paragraph("Very <i>Special</i>!", styles['Normal']))

# Write the document to disk
doc.build(elements)
# Get the value of the StringIO buffer and write it to the response.
pdf = buffer.getvalue()
buffer.close()
response.write(pdf)
return response

Shouldn't the build method be inherited from PageTemplate? What am I missing?

Thanks!
Erik


On 10/8/07, Andy Robinson <andy at reportlab.com> wrote:

> On 08/10/2007, Erik Wickstrom <erik at erikwickstrom.com> wrote:

> > I've try fiddling with the size parameters. The largest I can get is with

> > about a 2 inch border around the image of blank page. My text is also not

> > "layering" on top of the background image, it's being pushed onto the next

> > page instead.

> >

> > The actual dimensions of the image are 8.5x11 inches, so it should fit

> > perfectly.

> >

>

> Erik, sorry I missed this thread. The discussion so far assumes you

> want a huge image as part of the story, but I think you want it as a

> background, like a watermark?

>

> Your best bet is to treat your background image as part of a

> PageTemplate, overriding the beforeDrawPage method.

>

> If you've been using SimpleDocTemplate so far, you may have missed

> this because it creates page templates for you. It's best to directly

> use BaseDocTemplate and set it up with your own PageTemplates.

> There's some code in reportlab\tools\docco\rltemplate.py showing how

> we create a realistic document template with some custom page

> templates that 'do stuff' when drawn. These ones user afterDrawPage

> to add a company address, but you are going to just call something

> like this...

>

> class EriksPageTemplate(PageTemplate):

> ...snip...

> def beforeDrawPage(self, canv):

> self.canv.drawImage("watermark.jpg", 0, 0, 8.5*in, 11*in)

>

> This is also the right way to do headers, footers and any other

> decoration of pages.

>

> If it only appears on some pages, you can have several page templates,

> and put in action flowables to switch to your template in the story

> before you need it.

>

> I hope this points you the right way.

>

>

> Andy Robinson

> CEO/Chief Architect

> ReportLab Europe Ltd.

> _______________________________________________

> reportlab-users mailing list

> reportlab-users at reportlab.com

> http://two.pairlist.net/mailman/listinfo/reportlab-users

>



More information about the reportlab-users mailing list