[reportlab-users] Page background image with Platypus?
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):
81. response = callback(request, *callback_args, **callback_kwargs)
File "/root/journal/gazette/views.py" in some_view
AttributeError at /gazette/test/
GazettePageTemplate instance has no attribute 'build'
This is my class so far:
def __init__(self, id, pageSize=defaultPageSize):
self.pageWidth = pageSize
self.pageHeight = pageSize
frame1 = Frame(inch,
self.pageWidth - 2*inch,
self.pageHeight - 518, id='cover')
PageTemplate.__init__(self, id, [frame1]) # note lack of onPage
def beforeDrawPage(self, canv):
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')
from reportlab.rl_config import defaultPageSize
(MAXWIDTH, MAXHEIGHT) = defaultPageSize
image = Image('/root/journal/gazette/paper1.png')
#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
# Get the value of the StringIO buffer and write it to the response.
pdf = buffer.getvalue()
Shouldn't the build method be inherited from PageTemplate? What am I missing?
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):
> 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
More information about the reportlab-users