Changeset 2663

Show
Ignore:
Timestamp:
02/10/06 23:15:11 (18 years ago)
Author:
jerome
Message:

The logo is now repeated on all pages but stored only once in the PDF document.
No more work today.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • pykota/trunk/bin/pkinvoice

    r2662 r2663  
    2828import os 
    2929import pwd 
    30 import grp 
     30import cStringIO 
     31 
     32try : 
     33    from reportlab.pdfgen import canvas 
     34    from reportlab.lib import pagesizes 
     35    from reportlab.lib.units import cm 
     36except ImportError :     
     37    hasRL = 0 
     38else :     
     39    hasRL = 1 
     40     
     41try : 
     42    import PIL.Image  
     43except ImportError :     
     44    hasPIL = 0 
     45else :     
     46    hasPIL = 1 
     47 
    3148from pykota.tool import PyKotaTool, PyKotaToolError, PyKotaCommandLineError, crashed, N_ 
    3249from pykota.config import PyKotaConfigError 
     
    93110class PKInvoice(PyKotaTool) :         
    94111    """A class for pkinvoice.""" 
     112    def getPageSize(self, pgsize) : 
     113        """Returns the correct page size or None if not found.""" 
     114        try : 
     115            return getattr(pagesizes, pgsize.upper()) 
     116        except AttributeError :     
     117            try : 
     118                return getattr(pagesizes, pgsize.lower()) 
     119            except AttributeError : 
     120                pass 
     121                 
     122    def pagePDF(self, invoicenumber, name, amount, vatamount) : 
     123        """Generates a new page in the PDF document.""" 
     124        sys.stderr.write("#%06i    %s     %.2f    %.2f\n" % (invoicenumber, name, amount, vatamount)) 
     125        self.canvas.doForm("background") 
     126        self.canvas.showPage() 
     127         
     128    def initPDF(self, pagesize, logo) : 
     129        """Initializes the PDF document.""" 
     130        self.pdfDocument = cStringIO.StringIO()         
     131        self.canvas = c = canvas.Canvas(self.pdfDocument, pagesize=pagesize, pageCompression=1) 
     132         
     133        c.setAuthor(pwd.getpwuid(os.geteuid())[0]) 
     134        c.setTitle("PyKota invoices") 
     135        c.setSubject("This is an invoice generated with PyKota") 
     136         
     137        xcenter = pagesize[0] / 2.0 
     138        ycenter = pagesize[1] / 2.0 
     139                     
     140        ypos = pagesize[1] - (2 * cm)             
     141         
     142        self.canvas.beginForm("background") 
     143        self.canvas.saveState() 
     144         
     145        if logo : 
     146            try :     
     147                imglogo = PIL.Image.open(logo) 
     148            except :     
     149                self.printInfo("Unable to open image %s" % logo, "warn") 
     150            else : 
     151                (width, height) = imglogo.size 
     152                multi = float(width) / (8 * cm)  
     153                width = float(width) / multi 
     154                height = float(height) / multi 
     155                xpos = xcenter - (width / 2.0) 
     156                ypos -= height 
     157                c.drawImage(logo, xpos, ypos, width, height) 
     158         
     159        # New top 
     160        xpos = pagesize[0] / 5.0 
     161        ypos -= (1 * cm) + 20 
     162         
     163        self.canvas.restoreState() 
     164        self.canvas.endForm() 
     165         
     166    def endPDF(self, fname) :     
     167        """Flushes the PDF generator.""" 
     168        self.canvas.save() 
     169        if fname != "-" :         
     170            outfile = open(fname, "w") 
     171            outfile.write(self.pdfDocument.getvalue()) 
     172            outfile.close() 
     173        else :     
     174            sys.stdout.write(self.pdfDocument.getvalue()) 
     175            sys.stdout.flush() 
     176         
    95177    def main(self, names, options) : 
    96178        """Generate invoices.""" 
     179        if not hasRL : 
     180            raise PyKotaToolError, "The ReportLab module is missing. Download it from http://www.reportlab.org" 
     181        if not hasPIL : 
     182            raise PyKotaToolError, "The Python Imaging Library is missing. Download it from http://www.pythonware.com/downloads" 
     183             
    97184        if not self.config.isAdmin : 
    98185            raise PyKotaCommandLineError, "%s : %s" % (pwd.getpwuid(os.geteuid())[0], _("You're not allowed to use this command.")) 
     
    117204            raise PyKotaCommandLineError, _("Incorrect value '%s' for the --number command line option") % options["number"] 
    118205             
     206        pagesize = self.getPageSize(options["pagesize"]) 
     207        if pagesize is None : 
     208            pagesize = self.getPageSize("a4") 
     209            self.printInfo(_("Invalid 'pagesize' option %s, defaulting to A4.") % options["pagesize"], "warn") 
     210             
    119211        if not names : 
    120212            names = [ "*" ] 
     
    126218        suffix = (options["groups"] and "Group") or "User"         
    127219        entries = getattr(self.storage, "getMatching%ss" % suffix)(",".join(names)) 
    128         nbtotal = len(entries) 
    129         for i in range(nbtotal) : 
    130             entry = entries[i] 
    131             amount = reference - entry.AccountBalance 
    132             if amount > 0.0 : 
    133                 # There's something due ! 
    134                 ht = ((amount * 10000.0) / (100.0 + vat)) / 100.0 
    135                 sys.stderr.write("%s => Due : %.2f including %.2f VAT\n" % (entry.Name, amount, amount-ht)) 
    136                  
    137             if outfname != "-" : 
    138                 percent = 100.0 * float(i) / float(nbtotal) 
    139                 self.display("\r%.02f%%" % percent) 
    140                  
    141         if outfname != "-" :         
     220        if entries : 
     221            self.initPDF(pagesize, options["logo"].strip()) 
     222            nbtotal = len(entries) 
     223            for i in range(nbtotal) : 
     224                entry = entries[i] 
     225                amount = reference - entry.AccountBalance 
     226                if amount > 0.0 : 
     227                    # There's something due ! 
     228                    ht = ((amount * 10000.0) / (100.0 + vat)) / 100.0 
     229                    vatamount = amount - ht 
     230                    self.pagePDF(number, entry.Name, amount, vatamount) 
     231                    number += 1 
     232                if outfname != "-" : 
     233                    percent = 100.0 * float(i) / float(nbtotal) 
     234                    self.display("\r%.02f%%" % percent) 
     235                     
     236            self.endPDF(outfname) 
     237             
     238        if outfname != "-" : 
    142239            self.display("\r100.00%%\r        ") 
    143240            self.display("\r%s\n" % _("Done."))