历史版本12 :批量打印 返回文档
编辑时间:
内容长度:图片数:目录数:
修改原因:
1. 问题描述编辑
若您有很多张模板需要打印,而如果一个一个进行打印的话会比较麻烦,希望批量打印出来。
例如有一张全国销售人员的业绩报表,我传入不同销售人员参数后,批量打印出这些报表。
2. 批量打印传入的url格式编辑
批量打印多张报表url格式如:http://localhost:8075/WebReport/ReportServer?reportlets=[{reportlet:'reportname1.cpt',paraname:'paravalue'},{reportlet:'reportname2.cpt',paraname:'paravalue'}]
调用内置的打印方法直接使用完整的url进行批量打印:
var printurl="http://localhost:8075/WebReport/ReportServer?reportlets=[{reportlet:'reportname1.cpt',paraname:'paravalue'},{reportlet:'reportname2.cpt',paraname:'paravalue'}]";
FR.doURLPDFPrint(printurl,true); //get方式传参
如批量打印的模板过多的话,url就很长,而get方式对长度有限制,url过长时会导致打印失败。推荐批量打印的时候用post方式,reportlets参数打包在数据包中传输,不在url中显示,从而缩短url长度,另外安全性较好,如下:
var printurl="http://localhost:8075/WebReport/ReportServer";
var reportlets = FR.cjkEncode("[{reportlet: '/doc/Primary/Parameter/Parameter_1.cpt', 地区 : '华东'}, {reportlet: '/doc/Primary/Parameter/Parameter_1.cpt', 地区 : '华北'}]");
var config = {
url : printurl,
isPopUp : false,
data : {
reportlets: reportlets
}
};
FR.doURLPDFPrint(config);
注:调用打印方法中的第二个参数为true表示弹出对话框,为false表示不弹出对话框即静默打印。具体可参考不预览模板直接打印文档。
3. 示例编辑
如需要打印出某个模板所有参数情况对应的结果,如下图,选择希望打印的参数值,点击doPrint按钮批量打印出对应的结果。
3.1 实现思路
首先通过JS获取复选框的值然后拼凑出正确的url,最后调用打印方法如PDF打印,通过post方法传参(FR.doURLPDFPrint(printurl,true,{data: {reportlets : paravalue}});)或get方法传参(FR.doURLPDFPrint(printurl,true);)进行批量打印。
3.2 post传参PDF打印完整代码
<html>
<head>
<title>FineReport Demo</title>
<script type="text/javascript" src="/WebReport/ReportServer?op=emb&resource=finereport.js"></script>
<link rel="stylesheet" type="text/css" href="/WebReport/ReportServer?op=emb&resource=finereport.css"/>
<script type="text/javascript">
function doPrint(){ //通过sessionid打印
var printurl="http://localhost:8075/WebReport/ReportServer";
var p=[];
//获取当前页面选中的参数值,并将值放入数组中
$(":checkbox").each(function(){
if($(this).attr("checked")=="checked")
p.push("{reportlet: '/doc/Primary/Parameter/Parameter_1.cpt', 地区 : " + $(this).val() + "}");
})
if(p.length>0){
//将参数值组成的数组转化为字符串
var rp=p.join(",");
//使用FineReport自带的方法cjkEncode进行转码
var reportlets=FR.cjkEncode("["+rp+"]");
var config = {
url : printurl,
isPopUp : false,
data : {
reportlets: reportlets
}
};
FR.doURLPDFPrint(config); }
else
alert("请选择需要打印的参数");
}
</script>
</head>
<body>
<form name="report" width="200" height="200">
<input id="config1" type="checkbox" value="华东" />华东<br>
<input id="config2" type="checkbox" value="华北" />华北<br>
<input type="button" onClick="doPrint();" value="doPrint" ><br>
</form>
<body>
</html>
3.3 效果查看
打开内置服务器,在浏览器中输入http://localhost:8075/WebReport/page_demo/cusprint_batch.html,选中多个复选框,点击doprint按钮,既可以实现批量打印了。