7
7
# Author : Nicolas Bessi (Camptocamp)
8
# Contributor(s) : Florent Xicluna (Wingo SA)
9
10
# WARNING: This program as such is intended to be used by professional
10
11
# programmers who take the whole responsability of assessing all potential
41
42
from report_helper import WebKitHelper
42
43
from report.report_sxw import *
44
46
from tools.translate import _
45
47
from osv.osv import except_osv
50
def mako_template(text):
51
"""Build a Mako template.
53
This template uses UTF-8 encoding
55
# default_filters=['unicode', 'h'] can be used to set global filters
56
return Template(text, input_encoding='utf-8', output_encoding='utf-8')
47
59
class WebKitParser(report_sxw):
48
60
"""Custom class that use webkit to render HTML reports
49
61
Code partially taken from report openoffice. Thanks guys :)
67
79
_('Please install executable on your system'+
68
80
' (sudo apt-get install wkhtmltopdf) or download it from here:'+
69
81
' http://code.google.com/p/wkhtmltopdf/downloads/list and set the'+
70
' path to the executable on the Company form.')
82
' path to the executable on the Company form.'+
83
'Minimal version is 0.9.9')
72
85
if os.path.isabs(path) :
73
86
if (os.path.exists(path) and os.access(path, os.X_OK)\
99
112
command = ['wkhtmltopdf']
114
command.append('--quiet')
115
# default to UTF-8 encoding. Use <meta charset="latin-1"> to override.
116
command.extend(['--encoding', 'utf-8'])
103
118
head_file = file( os.path.join(
109
124
head_file.write(header)
110
125
head_file.close()
111
126
file_to_del.append(head_file.name)
112
command.append("--header-html '%s'"%(head_file.name))
127
command.extend(['--header-html', head_file.name])
114
129
foot_file = file( os.path.join(
120
135
foot_file.write(footer)
121
136
foot_file.close()
122
137
file_to_del.append(foot_file.name)
123
command.append("--footer-html '%s'"%(foot_file.name))
138
command.extend(['--footer-html', foot_file.name])
125
140
if webkit_header.margin_top :
126
command.append('--margin-top %s'%(webkit_header.margin_top))
141
command.extend(['--margin-top', str(webkit_header.margin_top).replace(',', '.')])
127
142
if webkit_header.margin_bottom :
128
command.append('--margin-bottom %s'%(webkit_header.margin_bottom))
143
command.extend(['--margin-bottom', str(webkit_header.margin_bottom).replace(',', '.')])
129
144
if webkit_header.margin_left :
130
command.append('--margin-left %s'%(webkit_header.margin_left))
145
command.extend(['--margin-left', str(webkit_header.margin_left).replace(',', '.')])
131
146
if webkit_header.margin_right :
132
command.append('--margin-right %s'%(webkit_header.margin_right))
147
command.extend(['--margin-right', str(webkit_header.margin_right).replace(',', '.')])
133
148
if webkit_header.orientation :
134
command.append("--orientation '%s'"%(webkit_header.orientation))
149
command.extend(['--orientation', str(webkit_header.orientation).replace(',', '.')])
135
150
if webkit_header.format :
136
command.append(" --page-size '%s'"%(webkit_header.format))
151
command.extend(['--page-size', str(webkit_header.format).replace(',', '.')])
138
153
for html in html_list :
139
154
html_file = file(os.path.join(tmp_dir, str(time.time()) + str(count) +'.body.html'), 'w')
145
160
command.append(out)
146
161
generate_command = ' '.join(command)
148
status = commands.getstatusoutput(generate_command)
163
status = subprocess.call(command, stderr=subprocess.PIPE) # ignore stderr
150
165
raise except_osv(
151
166
_('Webkit raise an error' ),
154
169
except Exception:
155
170
for f_to_del in file_to_del :
156
171
os.unlink(f_to_del)
158
pdf = file(out).read()
173
pdf = file(out, 'rb').read()
159
174
for f_to_del in file_to_del :
160
175
os.unlink(f_to_del)
237
252
if not template and report_xml.report_webkit_data :
238
253
template = report_xml.report_webkit_data
239
254
if not template :
240
raise except_osv(_('Webkit Report template not found !'), _(''))
255
raise except_osv(_('Error!'), _('Webkit Report template not found !'))
241
256
header = report_xml.webkit_header.html
242
257
footer = report_xml.webkit_header.footer_html
243
258
if not header and report_xml.header:
276
292
company= user.company_id
278
294
#default_filters=['unicode', 'entity'] can be used to set global filter
279
body_mako_tpl = Template(template ,input_encoding='utf-8')
295
body_mako_tpl = mako_template(template)
280
296
helper = WebKitHelper(cursor, uid, report_xml.id, context)
282
298
html = body_mako_tpl.render( helper=helper,
288
304
msg = exceptions.text_error_template().render()
289
305
netsvc.Logger().notifyChannel('Webkit render', netsvc.LOG_ERROR, msg)
290
306
raise except_osv(_('Webkit render'), msg)
291
head_mako_tpl = Template(header, input_encoding='utf-8')
307
head_mako_tpl = mako_template(header)
293
309
head = head_mako_tpl.render(
305
321
exceptions.text_error_template().render())
308
foot_mako_tpl = Template(footer ,input_encoding='utf-8')
324
foot_mako_tpl = mako_template(footer)
310
326
foot = foot_mako_tpl.render(