[reportlab-users] Patch to allow use without file

David Fraser reportlab-users@reportlab.com
Mon, 16 Jun 2003 10:48:09 +0200


This is a multi-part message in MIME format.
--------------000802010804080703080209
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 7bit

Hi

Thanks for the great toolkit, have found it very useful.

We are serving PDF documents over the web.
Rather than using the routine of creating a StringIO object that looks
like a file and passing that to the server, I did a simple patch to the
code that allows use to call getpdfdata to retrieve the data directly.

I have attached a patch to version 1.17, but have the CVS version and so
could make a patch for that if others are interested.

Please let me know what you think of this

David

PS resending for the third time, apologies if all of these get through!



--------------000802010804080703080209
Content-Type: text/plain;
 name="reportlab-getpdfdata.patch"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="reportlab-getpdfdata.patch"

diff -u -r -x '*.pyc' original/reportlab/pdfbase/pdfdoc.py reportlab/pdfbase/pdfdoc.py
--- original/reportlab/pdfbase/pdfdoc.py	Thu Nov 28 20:36:46 2002
+++ reportlab/pdfbase/pdfdoc.py	Fri Jun  6 09:20:04 2003
@@ -216,17 +216,7 @@
         return self._ID
 
     def SaveToFile(self, filename, canvas):
-        # realize delayed fonts
-        for fnt in self.delayedFonts:
-            fnt.addObjects(self)
-        # add info stuff to signature
-        self.info.digest(self.signature)
-        ### later: maybe add more info to sig?
-        # prepare outline
-        self.Reference(self.Catalog)
-        self.Reference(self.info)
-        outline = self.outline
-        outline.prepare(self, canvas)
+        txt = self.GetPDFData(canvas)
         if callable(getattr(filename, "write",None)):
             myfile = 0
             f = filename
@@ -235,13 +225,27 @@
             myfile = 1
             filename = str(filename)
             f = open(filename, "wb")
-        txt = self.format()
         f.write(txt)
         if myfile:
             f.close()
             markfilename(filename) # do platform specific file junk
         if getattr(canvas,'_verbosity',None): print 'saved', filename
 
+    def GetPDFData(self, canvas):
+        # realize delayed fonts
+        for fnt in self.delayedFonts:
+            fnt.addObjects(self)
+        # add info stuff to signature
+        self.info.digest(self.signature)
+        ### later: maybe add more info to sig?
+        # prepare outline
+        self.Reference(self.Catalog)
+        self.Reference(self.info)
+        outline = self.outline
+        outline.prepare(self, canvas)
+        txt = self.format()
+        return txt
+
     def inPage(self):
         """specify the current object as a page (enables reference binding and other page features)"""
         if self.inObject is not None:
diff -u -r -x '*.pyc' original/reportlab/pdfgen/canvas.py reportlab/pdfgen/canvas.py
--- original/reportlab/pdfgen/canvas.py	Wed Nov  6 13:32:42 2002
+++ reportlab/pdfgen/canvas.py	Fri Jun  6 09:21:26 2003
@@ -749,6 +749,13 @@
         if len(self._code): self.showPage()
         self._doc.SaveToFile(self._filename, self)
 
+    def getpdfdata(self):
+        """Returns the PDF data that would normally be written to a file.
+           If there is current data a ShowPage is executed automatically.
+           After this operation the canvas must not be used further."""
+        if len(self._code): self.showPage()
+        return self._doc.GetPDFData(self)
+
     def setPageSize(self, size):
         """accepts a 2-tuple in points for paper size for this
         and subsequent pages"""
Only in reportlab/platypus: tables.pdf



--------------000802010804080703080209--