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