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 |
---|
9 | |
---|
10 | ISPDF = False |
---|
11 | if sys.platform == "win32": |
---|
12 | _STATICBOX_WIDTH = 450 |
---|
13 | PANEL_WIDTH = 500 |
---|
14 | PANEL_HEIGHT = 700 |
---|
15 | FONT_VARIANT = 0 |
---|
16 | ISPDF = True |
---|
17 | elif sys.platform == "darwin": |
---|
18 | _STATICBOX_WIDTH = 480 |
---|
19 | PANEL_WIDTH = 530 |
---|
20 | PANEL_HEIGHT = 700 |
---|
21 | FONT_VARIANT = 1 |
---|
22 | ISPDF = True |
---|
23 | |
---|
24 | class BaseReportDialog(wx.Dialog): |
---|
25 | |
---|
26 | def __init__(self, report_list, *args, **kwds): |
---|
27 | """ |
---|
28 | Initialization. The parameters added to Dialog are: |
---|
29 | |
---|
30 | :param report_list: list of html_str, text_str, image for report |
---|
31 | """ |
---|
32 | kwds["style"] = wx.RESIZE_BORDER|wx.DEFAULT_DIALOG_STYLE |
---|
33 | super(BaseReportDialog, self).__init__(*args, **kwds) |
---|
34 | kwds["image"] = 'Dynamic Image' |
---|
35 | |
---|
36 | # title |
---|
37 | self.SetTitle("Report") |
---|
38 | # size |
---|
39 | self.SetSize((720, 650)) |
---|
40 | # font size |
---|
41 | self.SetWindowVariant(variant=FONT_VARIANT) |
---|
42 | # check if tit is MAC |
---|
43 | self.is_pdf = ISPDF |
---|
44 | # report string |
---|
45 | self.report_list = report_list |
---|
46 | # wild card |
---|
47 | # pdf supporting only on MAC |
---|
48 | if self.is_pdf: |
---|
49 | self.wild_card = ' PDF files (*.pdf)|*.pdf|' |
---|
50 | self.index_offset = 0 |
---|
51 | else: |
---|
52 | self.wild_card = '' |
---|
53 | self.index_offset = 1 |
---|
54 | self.wild_card += 'HTML files (*.html)|*.html|' |
---|
55 | self.wild_card += 'Text files (*.txt)|*.txt' |
---|
56 | |
---|
57 | def _setup_layout(self): |
---|
58 | """ |
---|
59 | Set up layout |
---|
60 | """ |
---|
61 | hbox = wx.BoxSizer(wx.HORIZONTAL) |
---|
62 | |
---|
63 | # buttons |
---|
64 | button_close = wx.Button(self, wx.ID_OK, "Close") |
---|
65 | button_close.SetToolTipString("Close this report window.") |
---|
66 | hbox.Add(button_close) |
---|
67 | button_close.SetFocus() |
---|
68 | |
---|
69 | button_print = wx.Button(self, wx.NewId(), "Print") |
---|
70 | button_print.SetToolTipString("Print this report.") |
---|
71 | button_print.Bind(wx.EVT_BUTTON, self.onPrint, |
---|
72 | id=button_print.GetId()) |
---|
73 | hbox.Add(button_print) |
---|
74 | |
---|
75 | button_save = wx.Button(self, wx.NewId(), "Save") |
---|
76 | button_save.SetToolTipString("Save this report.") |
---|
77 | button_save.Bind(wx.EVT_BUTTON, self.onSave, id=button_save.GetId()) |
---|
78 | hbox.Add(button_save) |
---|
79 | |
---|
80 | # panel for report page |
---|
81 | vbox = wx.BoxSizer(wx.VERTICAL) |
---|
82 | # html window |
---|
83 | self.hwindow = html.HtmlWindow(self, style=wx.BORDER) |
---|
84 | # set the html page with the report string |
---|
85 | self.hwindow.SetPage(self.report_html) |
---|
86 | |
---|
87 | # add panels to boxsizers |
---|
88 | vbox.Add(hbox) |
---|
89 | vbox.Add(self.hwindow, 1, wx.EXPAND|wx.ALL,0) |
---|
90 | |
---|
91 | self.SetSizer(vbox) |
---|
92 | self.Centre() |
---|
93 | self.Show(True) |
---|
94 | |
---|
95 | def onPreview(self, event=None): |
---|
96 | """ |
---|
97 | Preview |
---|
98 | : event: Preview button event |
---|
99 | """ |
---|
100 | previewh = html.HtmlEasyPrinting(name="Printing", parentWindow=self) |
---|
101 | previewh.PreviewText(self.report_html) |
---|
102 | |
---|
103 | def onPrint(self, event=None): |
---|
104 | """ |
---|
105 | Print |
---|
106 | : event: Print button event |
---|
107 | """ |
---|
108 | printh = html.HtmlEasyPrinting(name="Printing", parentWindow=self) |
---|
109 | printh.PrintText(self.report_html) |
---|
110 | |
---|
111 | def OnClose(self, event=None): |
---|
112 | """ |
---|
113 | Close the Dialog |
---|
114 | : event: Close button event |
---|
115 | """ |
---|
116 | self.Close() |
---|
117 | |
---|
118 | def HTML2PDF(self, data, filename): |
---|
119 | """ |
---|
120 | Create a PDF file from html source string. |
---|
121 | Returns True is the file creation was successful. |
---|
122 | : data: html string |
---|
123 | : filename: name of file to be saved |
---|
124 | """ |
---|
125 | try: |
---|
126 | from xhtml2pdf import pisa |
---|
127 | # open output file for writing (truncated binary) |
---|
128 | resultFile = open(filename, "w+b") |
---|
129 | # convert HTML to PDF |
---|
130 | pisaStatus = pisa.CreatePDF(data, dest=resultFile) |
---|
131 | # close output file |
---|
132 | resultFile.close() |
---|
133 | self.Update() |
---|
134 | return pisaStatus.err |
---|
135 | except: |
---|
136 | logging.error("Error creating pdf: %s" % sys.exc_value) |
---|
137 | return False |
---|
138 | |
---|