[reportlab-users] pdfgen gives me AttributeError: jpeg_fh

Gregory Piñero gregpinero at gmail.com
Sat Dec 1 22:59:54 EST 2007


For now I got this to work by switching the relevant lines to:

img=img.resize((width/2,height/2),Image.BILINEAR)
c.drawInlineImage(img, x=width/2,y=0, width=width/2,
height=height/2)

But I guess the other method still needs to get fixed, no?

-Greg

On Dec 1, 2007 4:04 PM, Gregory Piñero <gregpinero at gmail.com> wrote:

> (sorry if this gets sent twice, my email is acting funny)

>

>

> Hi guys,

>

> Here's the error I'm getting, any idea what's going on?

>

> Traceback (most recent call last):

> File "<string>", line 59, in <module>

> File "<string>", line 46, in put_in_front_picture

> File "/usr/lib/python2.5/site-packages/reportlab/pdfgen/canvas.py",

> line 629, in drawImage

> imgObj = pdfdoc.PDFImageXObject(name, image, mask=mask)

> File "/usr/lib/python2.5/site-packages/reportlab/pdfbase/pdfdoc.py",

> line 1842, in __init__

> self.loadImageFromSRC(source)

> File "/usr/lib/python2.5/site-packages/reportlab/pdfbase/pdfdoc.py",

> line 1891, in loadImageFromSRC

> fp = im.jpeg_fh()

> File "/usr/lib/python2.5/site-packages/PIL/Image.py", line 493, in __getattr__

> raise AttributeError(name)

> AttributeError: jpeg_fh

>

> I've pasted my code below if it helps. Thanks.

>

> -Greg

>

>

>

> from cStringIO import StringIO

> from reportlab import rl_config

> from reportlab.pdfgen import canvas

> from reportlab.lib.pagesizes import letter, A4

> from reportlab.lib.units import inch

> from reportlab.lib.colors import black, lightgrey, lightslategray

> from PIL import Image

>

> tmp = StringIO()

>

> #Canvas Setup

> c = canvas.Canvas(tmp, pagesize=letter, pageCompression=0)

> #If output size is important set pageCompression=1, but remember that,

> compressed documents

> #will be smaller, but slower to generate. Note that images are always

> compressed, and this option will only

> #save space if you have a very large amount of text and vector

> graphics on each page.

> #Do I need 'MacRomanEncoding' for Macs?

> #Note: 0,0 coordinate in canvas is bottom left.

> width, height = letter #keep for later

> c.setAuthor('Utility Mill - utilitymill.com/utility/greeting_card_generator')

> c.setTitle('Beautiful, expensive greeting card created at utilitymill.com')

>

> def add_fold_lines(c):

> """Add lines to show where to fold card"""

> # choose some colors

> c.setFillColor(lightgrey)

> # line settings

> c.setLineWidth(1)

> c.setDash(3,3) #3 points on, 3 off

> # draw some lines

> c.line(width/2,0,width/2,height) #vertical

> c.line(0,height/2,width,height/2) #horz.

>

> def put_in_front_picture(c,str_img,img_url):

> assert not (str_img and img_url)

> if img_url:

> import urllib2

> data=urllib2.urlopen(url).read()

> else:

> data=str_img

> image_file = StringIO(data)

> img = Image.open(image_file)

> img.load()

> #resize image:

> img.resize((width/2,height/2),Image.BILINEAR)

> c.drawImage(img, x=width/2,y=0, width=width/2,

> height=height/2,mask=None)

>

> def write_text(c,front_text,inside_text):

> #call canvas.getAvailableFonts() to see all fonts

> # change color

> c.setFillColor(black)

> c.setFont("Helvetica", 18)

> c.drawString(int(.6*width), int(.4*height), front_text)

> c.setFont("Helvetica", 14)

> c.rotate(180) #write text upside down so it's right when folded.

> c.drawString(int(.1*width), int(.7*height), front_text)

>

> add_fold_lines(c)

> put_in_front_picture(c,IMG_FILE,IMG_URL)

> write_text(c,TEXT_FRONT,TEXT_INSIDE)

>

> #The showPage method saves the current page of the canvas

> c.showPage()

> #The save method stores the file and closes the canvas.

> c.save()

>

> tmp.seek(0)

> print 'Content-Type: application/pdf'

> print tmp.read()

>

>

>

> --

> Gregory Piñero

> Chief Innovation Officer

> Blended Technologies

> (www.blendedtechnologies.com)

>




--
Gregory Piñero
Chief Innovation Officer
Blended Technologies
(www.blendedtechnologies.com)


More information about the reportlab-users mailing list