JasperReport浏览器输出PDF为空白页的解决方案

当我们在用IReport制作好PDF模板,并生成jrxml,jaser文件时,将jaser文件放在web项目目录下,这时在Contoller中调用JasperRunManager.runReportToPdf()函数,即可输出PDF文件
@Controller
@RequestMapping(value = "/test")
public class testController extends BaseController {
Log log = LogFactory.getLog(testController.class);
@RequestMapping(value = "/pdfs")
@SystemNormalLog(description = "测试pdf")
public void printComToPdf(HttpServletRequest request,HttpServletResponse response) throws Exception {
log.info("====Start");
PageData pd = new PageData(request);
String filePath = PathUtil.getClasspath()
+ "jasperFiles/test.jasper";
/** 获取报表模板 */
File reportFile = new File(filePath);
/** 组织数据源JRBeanCollectionDataSource */
Map param = new HashMap();
param.put("Name", "jack");
param.put("Age","19");
try {
/** 填充报表 */
byte[] bytes = [color=red]JasperRunManager.runReportToPdf(reportFile.getPath(), param);[/color]
/** 生成PDF文件 */
response.setContentType("application/pdf");
ServletOutputStream ouputStream = response.getOutputStream();
ouputStream.write(bytes, 0, bytes.length);
log.info("====Byte-length:"+bytes.length);
log.info("====Name&Age:"+param.get("Name")+","+param.get("Age"));
ouputStream.flush();
ouputStream.close();
log.info("====End");
} catch (Exception e) {
e.printStackTrace();
}
}

}

在这个过程中可能遇到IReport预览PDF为空白的问题,解决办法,:
1.工具-classpath,添加iTextAsia.jar
2.设置PDF文本格式,如下图

[img]http://dl2.iteye.com/upload/attachment/0117/7148/5eb30701-0243-3c75-8652-ee3acf9c9ba2.png[/img]
,意思为设置字体为宋体,同时将所需字体编译到jasper文件中
3.预览,即可看到所制作的PDF模板
预览可能正常,但在web浏览器浏览为空白,解决办法:
1.设置模板属性when no date:all selection,no details,如下图:
[img]http://dl2.iteye.com/upload/attachment/0117/7154/7af589c8-fc9d-374c-bfb7-c9180f0920df.png[/img]
意思为页面中没有details数据时,显示所有。

你可能感兴趣的:(iText)