Changeset 4037

Show
Ignore:
Timestamp:
04/14/2008 01:00:20 PM (3 months ago)
Author:
paul
Message:

Previously, the Image expression would only allow filenames or paths.
Now, it can be a buffer which the report writer will convert into the
proper image for reportlab. This is handy for images kept in databases,
for example.

Note: I couldn't do this without saving temp files to disk, because
of bugs in some versions of reportlab. But at least the appdev
doesn't need to worry about it.

Index: dabo/lib/reportWriter.py
===================================================================
--- dabo/lib/reportWriter.py (revision 4035)
+++ dabo/lib/reportWriter.py (working copy)
@@ -44,6 +44,7 @@

del(_failedLibs)
#######################################################


+import cStringIO

import reportlab.pdfgen.canvas as canvas
import reportlab.graphics.shapes as shapes
import reportlab.lib.pagesizes as pagesizes

@@ -56,8 +57,8 @@

from dabo.dLocalize import _
from dabo.lib.caselessDict import CaselessDict?
from reportlab.lib.utils import ImageReader?

-from StringIO import StringIO

from PIL import Image as PILImage

+import reportUtils


# The below block tried to use the experimental para.Paragraph which
# handles more html tags, including hyperlinks. However, I couldn't

@@ -1117,20 +1118,33 @@

p.rect(-1, -1, width+2, height+2)
c.clipPath(p, stroke=stroke)


- imageFile = obj.getProp("expr")
- if imageFile and not os.path.exists(imageFile):
- imageFile = os.path.join(self.HomeDirectory?, imageFile)
- if imageFile:
- imageFile = str(imageFile)
+ img = obj.getProp("expr")
+ if isinstance(img, basestring) and "\0" not in img:
+ # this is a path to image file, not the image itself
+ if not os.path.exists(img):
+ img = os.path.join(self.HomeDirectory?, img)
+ else:
+ # convert stream to PIL image:
+ #buffer = cStringIO.StringIO()
+ #buffer.write(img)
+ #buffer.seek(0)
+ #img = PILImage.open(buffer)
+ # (pkm: the above doesn't work in all reportlab versions. Safer to save a
+ # temp file unfortunately.)
+ imageFileName = reportUtils.getTempFile(ext="png")
+ imageFile = open(imageFileName, "wb")
+ imageFile.write(img)
+ imageFile.close()
+ img = imageFileName


- if imageFile and mode == "clip":
+ if mode == "clip":

# Need to set w,h to None for the drawImage, which will draw it in its
# "natural" state 1:1 pixel:point, which could flow out of the object's
# width/height, resulting in clipping.
width, height = None, None


- if imageFile:
- c.drawImage(imageFile, 0, 0, width, height, mask)
+ if img:
+ c.drawImage(img, 0, 0, width, height, mask)


elif objType == "BarGraph?":

# Do these imports here so as not to require the huge matplotlib unless

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/dabo/lib/reportWriter.py

    r4035 r4037  
    4545####################################################### 
    4646 
     47import cStringIO 
    4748import reportlab.pdfgen.canvas as canvas 
    4849import reportlab.graphics.shapes as shapes 
     
    5758from dabo.lib.caselessDict import CaselessDict 
    5859from reportlab.lib.utils import ImageReader 
    59 from StringIO import StringIO 
    6060from PIL import Image as PILImage 
     61import reportUtils 
    6162 
    6263# The below block tried to use the experimental para.Paragraph which 
     
    11181119            c.clipPath(p, stroke=stroke) 
    11191120     
    1120             imageFile = obj.getProp("expr") 
    1121             if imageFile and not os.path.exists(imageFile): 
    1122                 imageFile = os.path.join(self.HomeDirectory, imageFile) 
    1123             if imageFile: 
    1124                 imageFile = str(imageFile)   
    1125  
    1126             if imageFile and mode == "clip": 
     1121            img = obj.getProp("expr") 
     1122            if isinstance(img, basestring) and "\0" not in img: 
     1123                # this is a path to image file, not the image itself 
     1124                if not os.path.exists(img): 
     1125                    img = os.path.join(self.HomeDirectory, img) 
     1126            else: 
     1127                # convert stream to PIL image: 
     1128                #buffer = cStringIO.StringIO() 
     1129                #buffer.write(img) 
     1130                #buffer.seek(0)              
     1131                #img = PILImage.open(buffer) 
     1132                # (pkm: the above doesn't work in all reportlab versions. Safer to save a  
     1133                #       temp file unfortunately.) 
     1134                imageFileName = reportUtils.getTempFile(ext="png") 
     1135                imageFile = open(imageFileName, "wb") 
     1136                imageFile.write(img) 
     1137                imageFile.close() 
     1138                img = imageFileName 
     1139 
     1140            if mode == "clip": 
    11271141                # Need to set w,h to None for the drawImage, which will draw it in its 
    11281142                # "natural" state 1:1 pixel:point, which could flow out of the object's 
     
    11301144                width, height = None, None 
    11311145 
    1132             if imageFile
    1133                 c.drawImage(imageFile, 0, 0, width, height, mask) 
     1146            if img
     1147                c.drawImage(img, 0, 0, width, height, mask) 
    11341148 
    11351149        elif objType == "BarGraph":