~openerp-indonesia-team/openerp-indonesia/openerp-addons-Indonesia

« back to all changes in this revision

Viewing changes to report_webkit/webkit_report.py

  • Committer: Iman(ADSOFT)
  • Date: 2011-05-06 23:31:40 UTC
  • Revision ID: alle@openerp.co.id-20110506233140-egef2xos807fw784
updates

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
# All Right Reserved
6
6
#
7
7
# Author : Nicolas Bessi (Camptocamp)
 
8
# Contributor(s) : Florent Xicluna (Wingo SA)
8
9
#
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
29
30
#
30
31
##############################################################################
31
32
 
32
 
import commands
 
33
import subprocess
33
34
import os
34
35
import report
35
36
import tempfile
41
42
from report_helper import WebKitHelper
42
43
from report.report_sxw import *
43
44
import addons
 
45
import tools
44
46
from tools.translate import _
45
47
from osv.osv import except_osv
46
48
 
 
49
 
 
50
def mako_template(text):
 
51
    """Build a Mako template.
 
52
 
 
53
    This template uses UTF-8 encoding
 
54
    """
 
55
    # default_filters=['unicode', 'h'] can be used to set global filters
 
56
    return Template(text, input_encoding='utf-8', output_encoding='utf-8')
 
57
 
 
58
 
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')
71
84
                            ) 
72
85
        if os.path.isabs(path) :
73
86
            if (os.path.exists(path) and os.access(path, os.X_OK)\
98
111
        else:
99
112
            command = ['wkhtmltopdf']
100
113
 
101
 
        command.append('-q')
 
114
        command.append('--quiet')
 
115
        # default to UTF-8 encoding.  Use <meta charset="latin-1"> to override.
 
116
        command.extend(['--encoding', 'utf-8'])
102
117
        if header :
103
118
            head_file = file( os.path.join(
104
119
                                  tmp_dir,
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])
113
128
        if footer :
114
129
            foot_file = file(  os.path.join(
115
130
                                  tmp_dir,
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])
124
139
            
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(',', '.')])
137
152
        count = 0
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)
147
162
        try:
148
 
            status = commands.getstatusoutput(generate_command)
149
 
            if status[0] :
 
163
            status = subprocess.call(command, stderr=subprocess.PIPE) # ignore stderr
 
164
            if status :
150
165
                raise except_osv(
151
166
                                _('Webkit raise an error' ), 
152
 
                                status[1]
 
167
                                status
153
168
                            )
154
169
        except Exception:
155
170
            for f_to_del in file_to_del :
156
171
                os.unlink(f_to_del)
157
172
 
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)
161
176
 
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:
250
265
            header = u"""
251
266
<html>
252
267
    <head>
 
268
        <meta content="text/html; charset=UTF-8" http-equiv="content-type"/>
253
269
        <style type="text/css"> 
254
270
            ${css}
255
271
        </style>
276
292
        company= user.company_id
277
293
        
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)
281
297
        try :
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)
292
308
        try :
293
309
            head = head_mako_tpl.render(
294
310
                                        company=company,
305
321
                exceptions.text_error_template().render())
306
322
        foot = False
307
323
        if footer :
308
 
            foot_mako_tpl = Template(footer ,input_encoding='utf-8')
 
324
            foot_mako_tpl = mako_template(footer)
309
325
            try :
310
326
                foot = foot_mako_tpl.render(
311
327
                                            company=company,
327
343
                                            time=time,
328
344
                                            helper=helper,
329
345
                                            css=css,
330
 
                                            _debug=html,
 
346
                                            _debug=tools.ustr(html),
331
347
                                            formatLang=self.formatLang,
332
348
                                            setLang=self.setLang,
333
349
                                            _=self.translate_call,