jarsperreport6.3输出带条码的html。

来源:互联网 发布:openstack 源码 编辑:程序博客网 时间:2024/05/19 18:12

由于jarsperreport生成的pdf格式的报表用针式打印机打印出来非常模糊,所以就换成html格式打印,原由的xml模板不改变的情况下实现输出条形码。


jarspertreport输出html的时候会调用barbecue的api生成图片,HtmlResourceHandler.handleResource 用来保存barbecue生成的图片

HtmlResourceHandler.getResourcePath用来返回前段访问时的图片URL地址



public class ReportUtils {




    private static final Logger logger = LoggerFactory.getLogger(ReportUtils.class.getName());
    
//打印请求
private HttpServletRequest  request;
//打印响应
private HttpServletResponse response;
//打印模板的数据来源-JSON
private JSONObjectprintData;
//打印模板的数据来源-JavaBean
private List printBean;
//打印模板的文件名
private String              jasperFileName;
 

public ReportUtils(HttpServletRequest  request,
HttpServletResponse response,
List    printBean,
String              jasperFileName){
this.request=request;
this.response=response;
this.printBean=printBean;
this.jasperFileName=jasperFileName;
}
public ReportUtils( 
HttpServletRequest  request,
HttpServletResponse response,
JSONObject printData,
String              jasperFileName){
this.request=request;
this.response=response;
this.printData=printData;
this.jasperFileName=jasperFileName;
}



public HttpServletRequest getRequest() {
return request;
}
public void setRequest(HttpServletRequest request) {
this.request = request;
}
public HttpServletResponse getResponse() {
return response;
}
public void setResponse(HttpServletResponse response) {
this.response = response;
}
public JSONObject getPrintData() {
return printData;
}
public void setPrintData(JSONObject printData) {
this.printData = printData;
}
public String getJasperFileName() {
return jasperFileName;
}
public void setJasperFileName(String jasperFileName) {
this.jasperFileName = jasperFileName;
}

public void printHtml() throws Exception {

ServletContext context = request.getSession().getServletContext();
response.setContentType("text/html;charset=UTF-8");
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();


try
{
final String webAppRealPath=context.getRealPath("/files")+"/";

String subReportPath=context.getRealPath("/WEB-INF/classes/print")+"/";

//String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/public-access/barcode/getImageByBillNo?";  
final String imageReqPath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/files/";

File reportFile = new File(context.getRealPath("/WEB-INF/classes/print/"+jasperFileName+".jasper"));
logger.debug("reportFile path......"+reportFile.getAbsolutePath());

if (!reportFile.exists())
throw new Exception("未找到文件名为:"+jasperFileName+"的打印模板");

JasperReport jasperReport = (JasperReport)JRLoader.loadObjectFromFile(reportFile.getPath());

Map<String, Object> parameters = new HashMap<String, Object>();
parameters.put("SUBREPORT_DIR", subReportPath);
//parameters.put("BARCODE_GEN_URL",basePath);//图片保存目录

List<JasperPrint> jasperPrintList = new ArrayList<JasperPrint>();
for(Object bean:printBean){
JRDataSource jrDataSource = new JREmptyDataSource();
if(bean!=null){
List<Object> dataSource=new ArrayList<Object>();
dataSource.add(bean);
jrDataSource = new JRBeanCollectionDataSource(dataSource);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport,
parameters, jrDataSource);
jasperPrintList.add(jasperPrint);
}

}

HtmlExporter exporter = new HtmlExporter();


exporter.setExporterInput(SimpleExporterInput.getInstance(jasperPrintList));

SimpleHtmlExporterOutput output=new SimpleHtmlExporterOutput(out);

output.setImageHandler(new HtmlResourceHandler() {

@Override
public void handleResource(String id, byte[] data) {
//保存在服务器的位置
try {
logger.debug("xxh the gen barcode image"+imageReqPath+id);
FileOutputStream fos=new FileOutputStream(new File(webAppRealPath,id));
fos.write(data);
} catch (Exception e) {
logger.error("error_gen_image_jarsper",e);

}

@Override
public String getResourcePath(String id) {
//前端请求时返回的URL
logger.debug("xxh the gen barcode image"+imageReqPath+id);
return imageReqPath+id;
}
});

exporter.setExporterOutput(output);

exporter.exportReport();
logger.debug("report html generate over....");
}catch(Exception e){
logger.error("print report html error:",e);
throw e;
}

}

};



注意:window.print() 一定要延时处理,setTimeout(function (){ window.print() }) ;

由于打印的页面含有条码图片,生成预览的页面的时候,预览效果已经出来了,但是请求的图片还没返回时,就会导致生成的Html页面存在条码图片,

但是预览时就看不见图片了。

阅读全文
0 0