| 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 | |
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 != "-" : |