[reportlab-users] getRGBdata
Ulrich Wisser
reportlab-users@reportlab.com
Thu, 15 Apr 2004 15:13:03 +0200
Hello,
could please someone point out how to put a graphic in the header of my
first page? Please find my code below. I tried with several diffrent jpg
and gif and png graphic files. I always do get the same error:
File "/home/zope/lib/python2.1/reportlab/pdfgen/canvas.py", line 586,
in drawImage
rawdata = image.getRGBData()
AttributeError: PDFImage instance has no attribute 'getRGBData'
Please replace PDFImage with Image or PIL_Image. Just in case it
matters, this is a Zope External Script.
I have installed:
Zope 2.6.1
Python 2.1.3
PIL 1.1.4
ReportLab 1.19
Linux 2.4.20
TIA
Ulrich
from reportlab.platypus.paragraph import Paragraph
from reportlab.platypus.tables import Table
from reportlab.platypus.doctemplate import *
from reportlab.platypus import Flowable, PageBreak, Image
from reportlab.lib.units import inch, cm
from reportlab.lib.pagesizes import A4
from reportlab.lib import colors
from reportlab.lib.styles import getSampleStyleSheet
from PIL import Image as PIL_Image
from reportlab.pdfgen.pdfimages import PDFImage
from reportlab.pdfgen import canvas
import cStringIO
def putLogo(canvas, doc):
canvas.saveState()
image = PDFImage('/home/zope/Extensions/pdflogo.jpg', 4*cm, 2*cm)
canvas.drawImage(image, 1*cm, doc.pagesize[1] - 1*cm)
# image = PIL_Image.open('/home/zope/Extensions/pdflogo.jpg')
# (width, height) = image.size
# # scale it to be 0.75 inch high
# multi = ((height + 0.0) / (0.75 * cm))
# width = int(width / multi)
# height = int(height / multi)
# canvas.drawImage(image, 1*cm, doc.pagesize[1] - 1*cm, width, height)
canvas.restoreState()
def pdf1(self):
try:
report = cStringIO.StringIO ()
document = SimpleDocTemplate(report, pagesize=A4)
styles = getSampleStyleSheet()
style = styles["Normal"]
text = Paragraph("Hello World", style)
document.build([text], onFirstPage=putLogo)
self.REQUEST.RESPONSE.setHeader('Content-type', 'application/pdf')
return report.getvalue()
except:
import traceback, sys, cgi
content = sys.stdout = sys.stderr = cStringIO.StringIO()
self.REQUEST.RESPONSE.setHeader('Content-Type', 'text/html')
traceback.print_exc()
sys.stdout = sys.__stdout__
sys.stderr = sys.__stderr__
content =
'<html><head></head><body><pre>%s</pre></body></html>' %
cgi.escape(content.getvalue())
return content