如何使用python代码中的单个操作生成和下载两个报告。
def print_reports(self, cr, uid, ids, context):
'I have to do it here'
...
return {
'type': 'ir.actions.report.xml',
'report_name': 'certificate_of_origin',
'datas': datas,
}
我只需点击一下即可打印两个版本的certificate_of_origin
,但我每次只能打印一个版本。
答案 0 :(得分:2)
为此,您需要处理js代码。
在表单视图中添加以下代码:
<button string="Reports_print" name="print_reports" type="object"/>
然后我们需要使用python代码从js
生成请求的报告,而不使用:
return {
'type': 'ir.actions.report.xml',
'report_name': 'my_report',
'datas': datas,
'nodestroy': True
}
为此你的方法`print_reports&#39;应该是:
def print_reports(self, cr, uid, ids, context):
"""DO NOT EDIT !"""
需要从js
代码中捕获按钮点击事件。
在你的js脚本中 /modulename/static/js/script.js 执行此操作:
openerp.MODULENAME=function(instance)
{
var QWEB=instance.web.qweb,_t=instance.web._t;
instance.web.DataSet.include({
call_button:function(method, args){
var id = args[0];
if(String(method)=='print_reports'){
//get_reports should be created in modele_name class
new instance.web.Model('modele_name',this.get_context()).ca ll('get_reports',[id],{context:this.get_context()}).done(function(reports){
for(var b=0; b<reports.length; b+=2)
download('data:application/pdf;base64,'+reports[b],reports[b+1]+'.pdf','application/pdf');
});
}
return this._super(method, args);
}
});
};
我们从get_reports
调用python方法js
,结果报告为64 base string
get_reports
方法在发送之前生成并格式化报告
他们到js
做到如下:
def get_reports(self, cr, uid, ids, context):
#Get datas used in the reports from modele
datas = {
'ids': ids,
'model': 'modele_name',
'form': {
'key': value,
...
}
}
pdf1 = self.pool.get('ir.actions.report.xml').render_report(cr,
uid,
ids,
"report_name1",
datas,
context=None)
pdf2 = self.pool.get('ir.actions.report.xml').render_report(cr,
uid,
ids,
"report_name2",
datas,
context=None)
...
#We send 'report naem ' to name downloaded report ('reports[b+1]+'.pdf')
return pdf1[0].encode('base64'), 'report_name1', pdf2[0].encode(
'base64'), 'report_name2',...
值可以在RML代码中使用,如下所示:datas [&#39; form&#39;] [key] 。
要下载它们,我使用了download.js
脚本,
有很多方法可以做到这一点,但我发现download.js是最简单的方法
download.js
的内容:
//download.js v3.0, by dandavis; 2008-2014. [CCBY2] see http://danml.com/download.html for tests/usage
// v1 landed a FF+Chrome compat way of downloading strings to local un-named files, upgraded to use a hidden frame and optional mime
// v2 added named files via a[download], msSaveBlob, IE (10+) support, and window.URL support for larger+faster saves than dataURLs
// v3 added dataURL and Blob Input, bind-toggle arity, and legacy dataURL fallback was improved with force-download mime and base64 support
// data can be a string, Blob, File, or dataURL
function download(data, strFileName, strMimeType) {
var self = window, // this script is only for browsers anyway...
u = "application/octet-stream", // this default mime also triggers iframe downloads
m = strMimeType || u,
x = data,
D = document,
a = D.createElement("a"),
z = function(a){return String(a);},
B = self.Blob || self.MozBlob || self.WebKitBlob || z,
BB = self.MSBlobBuilder || self.WebKitBlobBuilder || self.BlobBuilder,
fn = strFileName || "download",
blob,
b,
ua,
fr;
//if(typeof B.bind === 'function' ){ B=B.bind(self); }
if(String(this)==="true"){ //reverse arguments, allowing download.bind(true, "text/xml", "export.xml") to act as a callback
x=[x, m];
m=x[0];
x=x[1];
}
//go ahead and download dataURLs right away
if(String(x).match(/^data\:[\w+\-]+\/[\w+\-]+[,;]/)){
return navigator.msSaveBlob ? // IE10 can't do a[download], only Blobs:
navigator.msSaveBlob(d2b(x), fn) :
saver(x) ; // everyone else can save dataURLs un-processed
}//end if dataURL passed?
try{
blob = x instanceof B ?
x :
new B([x], {type: m}) ;
}catch(y){
if(BB){
b = new BB();
b.append([x]);
blob = b.getBlob(m); // the blob
}
}
function d2b(u) {
var p= u.split(/[:;,]/),
t= p[1],
dec= p[2] == "base64" ? atob : decodeURIComponent,
bin= dec(p.pop()),
mx= bin.length,
i= 0,
uia= new Uint8Array(mx);
for(i;i<mx;++i) uia[i]= bin.charCodeAt(i);
return new B([uia], {type: t});
}
function saver(url, winMode){
if ('download' in a) { //html5 A[download]
a.href = url;
a.setAttribute("download", fn);
a.innerHTML = "downloading...";
D.body.appendChild(a);
setTimeout(function() {
a.click();
D.body.removeChild(a);
if(winMode===true){setTimeout(function(){ self.URL.revokeObjectURL(a.href);}, 250 );}
}, 66);
return true;
}
//do iframe dataURL download (old ch+FF):
var f = D.createElement("iframe");
D.body.appendChild(f);
if(!winMode){ // force a mime that will download:
url="data:"+url.replace(/^data:([\w\/\-\+]+)/, u);
}
f.src = url;
setTimeout(function(){ D.body.removeChild(f); }, 333);
}//end saver
if (navigator.msSaveBlob) { // IE10+ : (has Blob, but not a[download] or URL)
return navigator.msSaveBlob(blob, fn);
}
if(self.URL){ // simple fast and modern way using Blob and URL:
saver(self.URL.createObjectURL(blob), true);
}else{
// handle non-Blob()+non-URL browsers:
if(typeof blob === "string" || blob.constructor===z ){
try{
return saver( "data:" + m + ";base64," + self.btoa(blob) );
}catch(y){
return saver( "data:" + m + "," + encodeURIComponent(blob) );
}
}
// Blob but not URL:
fr=new FileReader();
fr.onload=function(e){
saver(this.result);
};
fr.readAsDataURL(blob);
}
return true;
} /* end download() */
为了能够在脚本中调用download
方法,您必须加载download.js
文件,以便修改MODULENAME_view.xml以添加新行
<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data>
<template id="assets_backend_MODULENAME" name="MODULENAME assets" inherit_id="web.assets_backend">
<xpath expr="." position="inside">
...
<!-- The new line-->
<script type="text/javascript" src="/MODULENAME/static/src/js/download.js"></script>
...
</xpath>
</template>
</data>
</openerp>
答案 1 :(得分:-1)
我不认为使用当前报道可能。 2年前我使用jasper报告做了同样的事情。并使用子报告功能。