def runReport(self, mode): """Run the report and then preview or print it.""" self.requery() f = self.write() if mode == "preview": reportUtils.previewPDF(f) elif mode == "print": reportUtils.printPDF(f) else: raise ValueError, "Mode needs to be 'preview' or 'print'."
def onGridMouseLeftDoubleClick(self, evt): # double-click on the grid app = self.Application if self.CurrentRow >= 0: self.Form.moveToRowNumber(self.CurrentRow) # column 4 = RecNo RecNo = self.getValue(self.CurrentRow, 4) print 'RecNo = ' + str(RecNo) fileName = self.getValue(self.CurrentRow, 0) fileData = self.getValue(self.CurrentRow, 6) f = app.tempdir + os.sep + fileName print "f = " + str(f) try: handle = open(f, 'wb') handle.write(fileData) handle.close() except Exception, e: dabo.ui.exclaim("Oh No! An exception while writing the file! This is a Really, Really Bad Thing!\n" + str(traceback.format_exc())) print "f = " + str(f) reportUtils.previewPDF(f)
def onGridMouseLeftDoubleClick(self, evt): # double-click on the grid app = self.Application downloadDir = app.PreferenceManager.getValue("downloaddir") if downloadDir == None or downloadDir == '': response = dabo.ui.areYouSure(message = "I do not seem to have a download directory set. Would you like to choose one now?", defaultNo = False, cancelButton = False, requestUserAttention=True) if not response == True: dabo.exclaim("Well, I can't download anything without having a place to write the file!") return() else: result = dabo.ui.getFolder() if result == None or result == '': dabo.exclaim("Well, I can't download anything without having a place to write the file!") return() else: downloadDir = result app.PreferenceManager.setValue("downloaddir", downloadDir) if self.CurrentRow >= 0: self.Form.moveToRowNumber(self.CurrentRow) # column 4 = RecNo RecNo = self.getValue(self.CurrentRow, 4) print 'RecNo = ' + str(RecNo) fileName = self.getValue(self.CurrentRow, 0) fileData = self.getValue(self.CurrentRow, 6) f = downloadDir + os.sep + fileName print "f = " + str(f) try: handle = open(f, 'wb') handle.write(fileData) handle.close() except Exception, e: dabo.ui.exclaim("Oh No! An exception while writing the file! This is a Really, Really Bad Thing!\n" + str(traceback.format_exc())) print "f = " + str(f) if fileName.lower().endswith('.pdf'): reportUtils.previewPDF(f) else: dabo.ui.info("File " + str(fileName) + " downloaded to " + str(downloadDir))
""" This demo uses a copy of my invoice report with bogus data to show how to interact with the ReportWriter to create a PDF. Run this script to create invoice.pdf. To modify the report form, use daboide/ReportDesigner to open the invoice.rfxml file. """ from dabo.lib.reportWriter import ReportWriter from dabo.lib import reportUtils outFile = "invoice.pdf" print "Instantiating the report writer..." rw = ReportWriter() # Set some required properties: rw.ReportFormFile = "invoice.rfxml" rw.OutputFile = "%s" % outFile # (Normally, you'd also set the Cursor property, to provide the data, # but this sample just uses the test cursor inside the report form.) # Set some optional properties: rw.UseTestCursor = True rw.ShowBandOutlines = True print "Writing %s..." % outFile rw.write() print "Trying to preview it..." reportUtils.previewPDF(outFile)
def onQuickReport(self, evt): # May not have records if called via toolbar button if not self.enableQuickReport(): dabo.ui.exclaim(_("Sorry, there are no records to report on."), title=_("No Records")) return showAdvancedQuickReport = self.ShowAdvancedQuickReport showExpandedQuickReport = self.ShowExpandedQuickReport class ReportFormatDialog(dabo.ui.dOkCancelDialog): def initProperties(self): self.Caption = "Quick Report" self.mode = None self.records = None self.saveNamedReportForm = False def addControls(self): self.addObject(dabo.ui.dRadioList, RegID="radMode", Caption="Mode", Orientation="Row", Choices=["List Format", "Expanded Format"], ValueMode="Key", Keys={ "list": 0, "expanded": 1 }, SaveRestoreValue=True) self.Sizer.append1x(self.radMode) self.Sizer.appendSpacer(12) if not showExpandedQuickReport: self.radMode.enableKey("expanded", False) self.radMode.Value = "list" ## in case the setting was saved at 'expanded' previously. self.addObject( dabo.ui.dRadioList, RegID="radRecords", Caption="Report On", Orientation="Row", Choices=["All records in dataset", "Just current record"], ValueMode="Key", Keys={ "all": 0, "one": 1 }, SaveRestoreValue=True) self.Sizer.append1x(self.radRecords) self.Sizer.appendSpacer(12) if showAdvancedQuickReport: self.addObject(dabo.ui.dButton, RegID="btnAdvanced", Caption="Advanced") self.Sizer.append(self.btnAdvanced, halign="center") self.btnAdvanced.bindEvent(dEvents.Hit, self.onAdvanced) def onAdvanced(self, evt): if dabo.ui.areYouSure( "Would you like to save the report form xml " "(rfxml) to your application's reports directory? If you say " "'yes', you'll be able to modify the file and it will be used " "as the Quick Report from now on (it will no longer be auto-" "generated). The file will be generated when you click 'Yes'." "\n\nGenerate the report form file?", cancelButton=False): self.saveNamedReportForm = True def runOK(self): self.mode = self.radMode.Value self.records = self.radRecords.Value # Name the dialog unique to the active page, so that the user's settings # will save and restore uniquely. They may want to usually print just the # current record in expanded format when on the edit page, and a list # format otherwise, for example. name = "FrmQuickReport_%s" % self.PageFrame.SelectedPage.Caption d = ReportFormatDialog(self, NameBase=name) d.show() mode = d.mode records = d.records saveNamedReportForm = d.saveNamedReportForm d.release() if mode is not None: # Run the report biz = self.getBizobj() rfxml = self.getReportForm(mode) if saveNamedReportForm: filename = os.path.join( self.Application.HomeDirectory, "reports", "datanav-%s-%s.rfxml" % (biz.DataSource, mode)) if not os.path.exists( os.path.join(self.Application.HomeDirectory, "reports")): os.mkdir( os.path.join(self.Application.HomeDirectory, "reports")) open(filename, "w").write(rfxml) if records == "all": cursor = biz.getDataSet() else: cursor = biz.getDataSet(rowStart=biz.RowNumber, rows=1) outputfile = reportUtils.getTempFile() try: import dabo.dReportWriter as drw except ImportError: dabo.ui.stop( "Error importing dReportWriter. Check your terminal output." ) return rw = drw.dReportWriter(OutputFile=outputfile, ReportFormXML=rfxml, Cursor=cursor, Encoding=biz.Encoding) try: rw.write() except (UnicodeDecodeError, ), e: #error_string = traceback.format_exc() error_string = ustr(e) row_number = rw.RecordNumber dabo.ui.stop( "There was a problem having to do with the Unicode encoding " "of your table, and ReportLab's inability to deal with any encoding " "other than UTF-8. Sorry, but currently we don't have a resolution to " "the problem, other than to recommend that you convert your data to " "UTF-8 encoding. Here's the exact error message received:\n\n%s" "\n\nThis occurred in Record %s of your cursor." % (ustr(e), row_number)) return # Now, preview using the platform's default pdf viewer: reportUtils.previewPDF(outputfile)
def onQuickReport(self, evt): # May not have records if called via toolbar button if not self.enableQuickReport(): dabo.ui.exclaim(_("Sorry, there are no records to report on."), title=_("No Records")) return showAdvancedQuickReport = self.ShowAdvancedQuickReport showExpandedQuickReport = self.ShowExpandedQuickReport class ReportFormatDialog(dabo.ui.dOkCancelDialog): def initProperties(self): self.Caption = "Quick Report" self.mode = None self.records = None self.saveNamedReportForm = False def addControls(self): self.addObject(dabo.ui.dRadioList, RegID="radMode", Caption="Mode", Orientation="Row", Choices=["List Format", "Expanded Format"], ValueMode="Key", Keys={"list":0, "expanded":1}, SaveRestoreValue=True) self.Sizer.append1x(self.radMode) self.Sizer.appendSpacer(12) if not showExpandedQuickReport: self.radMode.enableKey("expanded", False) self.radMode.Value = "list" ## in case the setting was saved at 'expanded' previously. self.addObject(dabo.ui.dRadioList, RegID="radRecords", Caption="Report On", Orientation="Row", Choices=["All records in dataset", "Just current record"], ValueMode="Key", Keys={"all":0, "one":1}, SaveRestoreValue=True) self.Sizer.append1x(self.radRecords) self.Sizer.appendSpacer(12) if showAdvancedQuickReport: self.addObject(dabo.ui.dButton, RegID="btnAdvanced", Caption="Advanced") self.Sizer.append(self.btnAdvanced, halign="center") self.btnAdvanced.bindEvent(dEvents.Hit, self.onAdvanced) def onAdvanced(self, evt): if dabo.ui.areYouSure("Would you like to save the report form xml " "(rfxml) to your application's reports directory? If you say " "'yes', you'll be able to modify the file and it will be used " "as the Quick Report from now on (it will no longer be auto-" "generated). The file will be generated when you click 'Yes'." "\n\nGenerate the report form file?", cancelButton=False): self.saveNamedReportForm = True def runOK(self): self.mode = self.radMode.Value self.records = self.radRecords.Value # Name the dialog unique to the active page, so that the user's settings # will save and restore uniquely. They may want to usually print just the # current record in expanded format when on the edit page, and a list # format otherwise, for example. name = "FrmQuickReport_%s" % self.PageFrame.SelectedPage.Caption d = ReportFormatDialog(self, NameBase=name) d.show() mode = d.mode records = d.records saveNamedReportForm = d.saveNamedReportForm d.release() if mode is not None: # Run the report biz = self.getBizobj() rfxml = self.getReportForm(mode) if saveNamedReportForm: filename = os.path.join(self.Application.HomeDirectory, "reports", "datanav-%s-%s.rfxml" % (biz.DataSource, mode)) if not os.path.exists(os.path.join(self.Application.HomeDirectory, "reports")): os.mkdir(os.path.join(self.Application.HomeDirectory, "reports")) open(filename, "w").write(rfxml) if records == "all": cursor = biz.getDataSet() else: cursor = biz.getDataSet(rowStart=biz.RowNumber, rows=1) outputfile = reportUtils.getTempFile() try: import dabo.dReportWriter as drw except ImportError: dabo.ui.stop("Error importing dReportWriter. Check your terminal output.") return rw = drw.dReportWriter(OutputFile=outputfile, ReportFormXML=rfxml, Cursor=cursor, Encoding=biz.Encoding) try: rw.write() except (UnicodeDecodeError,), e: #error_string = traceback.format_exc() error_string = ustr(e) row_number = rw.RecordNumber dabo.ui.stop("There was a problem having to do with the Unicode encoding " "of your table, and ReportLab's inability to deal with any encoding " "other than UTF-8. Sorry, but currently we don't have a resolution to " "the problem, other than to recommend that you convert your data to " "UTF-8 encoding. Here's the exact error message received:\n\n%s" "\n\nThis occurred in Record %s of your cursor." % (ustr(e), row_number)) return # Now, preview using the platform's default pdf viewer: reportUtils.previewPDF(outputfile)
def onReportEnd(self, evt): """Will be called only if the user didn't cancel; you could also bind to ReportCancel to set a flag, but this seemed cleaner.""" print "report end" reportUtils.previewPDF("invoice.pdf")
""" This demo uses a copy of my invoice report with bogus data to show how to interact with the ReportWriter to create a PDF. Run this script to create invoice.pdf. To modify the report form, use daboide/ReportDesigner to open the invoice.rfxml file. """ from dabo.lib.reportWriter import ReportWriter from dabo.lib import reportUtils outFile = "invoice.pdf" print("Instantiating the report writer...") rw = ReportWriter() # Set some required properties: rw.ReportFormFile = "invoice.rfxml" rw.OutputFile = "%s" % outFile # (Normally, you'd also set the Cursor property, to provide the data, # but this sample just uses the test cursor inside the report form.) # Set some optional properties: rw.UseTestCursor = True rw.ShowBandOutlines = True print("Writing %s..." % outFile) rw.write() print("Trying to preview it...") reportUtils.previewPDF(outFile)