看官方的論壇上很多人在問如何在web client上實現報表的直接打印,想想現在的打印的確是不方便,總是把文件下載下來,然後打開文件打印。
html的報表容易想到,因爲可以直接在web client打開一個新窗口,用js進行打印。於是就改改openerp代碼,實現一個簡單的html報表的。供大家參考。
時間倉促,勿笑話。
(我用的web client是embedded模式)
OE新版(6.1)的web client的整個結構和以前都不一樣了,所以要先研究報表的controller.
在Reports類裏,看到:
('Content-Disposition', 'attachment; filename="%s.%s"' % (action['report_name'], report_struct['format']))
這樣的http header的定義。這是要直接下載。修改先:
header_list = [
('Content-Type', report_mimetype),
('Content-Length', len(report))]
if report_struct['format'] not in ['html']:
header_list.append(('Content-Disposition', 'attachment; filename="%s.%s"' % (action['report_name'], report_struct['format'])))
return req.make_response(report,
headers=header_list,
cookies={'fileToken': int(token)})
沒什麼好說的。
改完,重啓服務,測試。發現點擊報表按鈕後,瀏覽器無任何反映,也沒有打開新窗口。
看來6.1的確是不一樣。
跟蹤代碼,發現報表這個action在web client的處理函數名爲ir_actions_report_xml。
內部又調用:
self.session.get_file({
url: '/web/report',
data: {action: JSON.stringify(action)},
complete: $.unblockUI,
success: function(){
if (!self.dialog && on_closed) {
on_closed();
}
self.dialog_stop();
},
error: session.webclient.crashmanager.on_rpc_error
})
看名字就知道,這貨肯定把所有的報表下載下來,而不是打開新窗口。考慮修改。
post_data = {
url: '/web/report',
data: {action: JSON.stringify(action)},
complete: $.unblockUI,
success: function(){
if (!self.dialog && on_closed) {
on_closed();
}
self.dialog_stop();
},
error: session.webclient.crashmanager.on_rpc_error
};
if(action.report_type == 'mako2html'){
self.session.open_report_page(post_data)
}else{
self.session.get_file(post_data)
}
get_file函數通過jquery調用了服務端的report view.我們也需要做同樣的事情:
在core.js裏:
/**
*Open an html report.
*未處理error
**/
open_report_page: function(options) {
var token = new Date().getTime();
params = options.data;
params['session_id'] = this.session_id;
params['token'] = token;
$.post(options.url, params, function(data){
report_window=window.open('','','width=100,height=100');
report_window.document.write(data);
report_window.focus();
options.complete();
if (options.success) { options.success(); }
});
},
ok, 可以在報表模板裏寫任何js東西,包括打印的代碼。
我並不是用js代碼直接打印,是通過Lodop控件,推薦大家使用。
可以直接打印,套打,導出excel。。。。
至此,就可以完成html報表的直接打印了。