[e8bb5b6] | 1 | """ |
---|
| 2 | Base class for reports. Child classes will need to implement |
---|
| 3 | the onSave() method. |
---|
| 4 | """ |
---|
| 5 | import wx |
---|
| 6 | import logging |
---|
| 7 | import sys |
---|
| 8 | import wx.html as html |
---|
[d0ce666f] | 9 | from sas.sasgui.guiframe.report_image_handler import ReportImageHandler |
---|
[e8bb5b6] | 10 | |
---|
[463e7ffc] | 11 | logger = logging.getLogger(__name__) |
---|
[c155a16] | 12 | |
---|
[e8bb5b6] | 13 | ISPDF = False |
---|
| 14 | if sys.platform == "win32": |
---|
| 15 | _STATICBOX_WIDTH = 450 |
---|
[69a6897] | 16 | PANEL_WIDTH = 500 |
---|
[e8bb5b6] | 17 | PANEL_HEIGHT = 700 |
---|
| 18 | FONT_VARIANT = 0 |
---|
| 19 | ISPDF = True |
---|
[6dd6e32] | 20 | # For OSX and everything else |
---|
| 21 | else: |
---|
[e8bb5b6] | 22 | _STATICBOX_WIDTH = 480 |
---|
| 23 | PANEL_WIDTH = 530 |
---|
| 24 | PANEL_HEIGHT = 700 |
---|
| 25 | FONT_VARIANT = 1 |
---|
| 26 | ISPDF = True |
---|
| 27 | |
---|
| 28 | class BaseReportDialog(wx.Dialog): |
---|
[69a6897] | 29 | |
---|
[5818dae] | 30 | def __init__(self, report_list, imgRAM, fig_urls, *args, **kwds): |
---|
[e8bb5b6] | 31 | """ |
---|
| 32 | Initialization. The parameters added to Dialog are: |
---|
[69a6897] | 33 | |
---|
[e8bb5b6] | 34 | :param report_list: list of html_str, text_str, image for report |
---|
| 35 | """ |
---|
| 36 | kwds["style"] = wx.RESIZE_BORDER|wx.DEFAULT_DIALOG_STYLE |
---|
| 37 | super(BaseReportDialog, self).__init__(*args, **kwds) |
---|
| 38 | kwds["image"] = 'Dynamic Image' |
---|
| 39 | |
---|
[5818dae] | 40 | #MemoryFSHandle for storing images |
---|
| 41 | self.imgRAM = imgRAM |
---|
| 42 | #Images location in urls |
---|
| 43 | self.fig_urls = fig_urls |
---|
[e8bb5b6] | 44 | # title |
---|
| 45 | self.SetTitle("Report") |
---|
| 46 | # size |
---|
| 47 | self.SetSize((720, 650)) |
---|
| 48 | # font size |
---|
| 49 | self.SetWindowVariant(variant=FONT_VARIANT) |
---|
| 50 | # check if tit is MAC |
---|
| 51 | self.is_pdf = ISPDF |
---|
| 52 | # report string |
---|
| 53 | self.report_list = report_list |
---|
| 54 | # wild card |
---|
[69a6897] | 55 | if self.is_pdf: # pdf writer is available |
---|
| 56 | self.wild_card = 'PDF files (*.pdf)|*.pdf|' |
---|
[e8bb5b6] | 57 | self.index_offset = 0 |
---|
| 58 | else: |
---|
| 59 | self.wild_card = '' |
---|
| 60 | self.index_offset = 1 |
---|
| 61 | self.wild_card += 'HTML files (*.html)|*.html|' |
---|
| 62 | self.wild_card += 'Text files (*.txt)|*.txt' |
---|
| 63 | |
---|
| 64 | def _setup_layout(self): |
---|
| 65 | """ |
---|
| 66 | Set up layout |
---|
| 67 | """ |
---|
| 68 | hbox = wx.BoxSizer(wx.HORIZONTAL) |
---|
[69a6897] | 69 | |
---|
[e8bb5b6] | 70 | # buttons |
---|
| 71 | button_close = wx.Button(self, wx.ID_OK, "Close") |
---|
| 72 | button_close.SetToolTipString("Close this report window.") |
---|
[d0ce666f] | 73 | button_close.Bind(wx.EVT_BUTTON, self.onClose, |
---|
| 74 | id=button_close.GetId()) |
---|
[e8bb5b6] | 75 | hbox.Add(button_close) |
---|
| 76 | button_close.SetFocus() |
---|
| 77 | |
---|
| 78 | button_print = wx.Button(self, wx.NewId(), "Print") |
---|
| 79 | button_print.SetToolTipString("Print this report.") |
---|
| 80 | button_print.Bind(wx.EVT_BUTTON, self.onPrint, |
---|
| 81 | id=button_print.GetId()) |
---|
| 82 | hbox.Add(button_print) |
---|
[a8e368b] | 83 | |
---|
[91552b5] | 84 | if sys.platform != "darwin": |
---|
[062c9ea] | 85 | button_save = wx.Button(self, wx.NewId(), "Save") |
---|
| 86 | button_save.SetToolTipString("Save this report.") |
---|
| 87 | button_save.Bind(wx.EVT_BUTTON, self.onSave, id=button_save.GetId()) |
---|
| 88 | hbox.Add(button_save) |
---|
[69a6897] | 89 | |
---|
[e8bb5b6] | 90 | # panel for report page |
---|
| 91 | vbox = wx.BoxSizer(wx.VERTICAL) |
---|
| 92 | # html window |
---|
| 93 | self.hwindow = html.HtmlWindow(self, style=wx.BORDER) |
---|
| 94 | # set the html page with the report string |
---|
| 95 | self.hwindow.SetPage(self.report_html) |
---|
[69a6897] | 96 | |
---|
[e8bb5b6] | 97 | # add panels to boxsizers |
---|
| 98 | vbox.Add(hbox) |
---|
| 99 | vbox.Add(self.hwindow, 1, wx.EXPAND|wx.ALL,0) |
---|
| 100 | |
---|
| 101 | self.SetSizer(vbox) |
---|
| 102 | self.Centre() |
---|
| 103 | self.Show(True) |
---|
| 104 | |
---|
| 105 | def onPreview(self, event=None): |
---|
| 106 | """ |
---|
| 107 | Preview |
---|
| 108 | : event: Preview button event |
---|
| 109 | """ |
---|
| 110 | previewh = html.HtmlEasyPrinting(name="Printing", parentWindow=self) |
---|
| 111 | previewh.PreviewText(self.report_html) |
---|
[69a6897] | 112 | |
---|
[e8bb5b6] | 113 | def onPrint(self, event=None): |
---|
| 114 | """ |
---|
| 115 | Print |
---|
| 116 | : event: Print button event |
---|
| 117 | """ |
---|
| 118 | printh = html.HtmlEasyPrinting(name="Printing", parentWindow=self) |
---|
| 119 | printh.PrintText(self.report_html) |
---|
| 120 | |
---|
[5818dae] | 121 | |
---|
[d0ce666f] | 122 | def onClose(self, event=None): |
---|
[e8bb5b6] | 123 | """ |
---|
| 124 | Close the Dialog |
---|
| 125 | : event: Close button event |
---|
| 126 | """ |
---|
[5818dae] | 127 | for fig in self.fig_urls: |
---|
[d0ce666f] | 128 | ReportImageHandler.remove_figure(fig) |
---|
[5818dae] | 129 | |
---|
[d0ce666f] | 130 | self.Destroy() |
---|
[69a6897] | 131 | |
---|
[e8bb5b6] | 132 | def HTML2PDF(self, data, filename): |
---|
| 133 | """ |
---|
| 134 | Create a PDF file from html source string. |
---|
[69a6897] | 135 | Returns True is the file creation was successful. |
---|
[e8bb5b6] | 136 | : data: html string |
---|
| 137 | : filename: name of file to be saved |
---|
| 138 | """ |
---|
| 139 | try: |
---|
| 140 | from xhtml2pdf import pisa |
---|
| 141 | # open output file for writing (truncated binary) |
---|
| 142 | resultFile = open(filename, "w+b") |
---|
| 143 | # convert HTML to PDF |
---|
| 144 | pisaStatus = pisa.CreatePDF(data, dest=resultFile) |
---|
| 145 | # close output file |
---|
| 146 | resultFile.close() |
---|
| 147 | self.Update() |
---|
| 148 | return pisaStatus.err |
---|
[5251ec6] | 149 | except Exception as exc: |
---|
| 150 | logger.error("Error creating pdf: %s" % exc) |
---|
[e8bb5b6] | 151 | return False |
---|