[Zope] PDF from external method

Brad Clements bkc@murkworks.com
Wed, 24 Oct 2001 13:21:31 -0400


On 24 Oct 2001 at 15:14, Juergen R. Plasser / HEXAGON wrote:

> how can I pass a pdf generated by an external method (using Reportlab) back 
> to Zope?

Easy, our external method just gets the output from reportlab in a string like object, then 
writes the data to RESPONSE.


 def externalmethod(self,REQUEST,RESPONSE, ... other stuff):
 <snip>

    report = BufferedBillingReport(orgid=orgid,startDate=start,endDate=end,showActualAmount=showActualAmount,invoiceNumber=invoiceNumber,trackURL=trackURL,mode=mode,carrier=carrier,vendorinvoice=vendorinvoice)
    data = report.getFileData()
    siz = len(data)
    RESPONSE.setHeader("Content-Type","application/pdf")
    RESPONSE.setHeader("Content-Length",siz)
    RESPONSE.write(data)


(some stolen code)

class   buffer:
    def __init__(self):
        self._buffer = []
        self._size = 0

    def write(self,line):
        self._buffer.append(str(line))

    def read(self):
        return string.join(self._buffer,'')

    def tell(self):
        return len(self._buffer)

class   BufferedBillingReport:
    """billing report that writes to a buffer and returns the buffer object"""
    
    def __init__(self,**kw):
        """Initialize it"""
        self.buffer = buffer()
        self.report = apply(BillingReport,(GetDB(),self.buffer,),kw)
        self.report.generateReport()

    def getFileData(self):
        """Return the contents of the buffer"""
        return self.buffer.read()


class   BillingReport(PDFLayouter):
    """Generate a PDF billing report for specified Org"""

<snip>





Brad Clements,                bkc@murkworks.com   (315)268-1000
http://www.murkworks.com                          (315)268-9812 Fax
netmeeting: ils://ils.murkworks.com               AOL-IM: BKClements