1.要生成pdf首先需要通过iReport生成pdf模板,详情自行百度。
2.生成pdf模板之后,将.jrxml和.jasper文件放在java项目的某个文件夹中。.jrxml文件是可编辑的文件,.jasper文件不可编辑。
3.最重要的部分来了,见下面:
-------------------------------------------------------------------------------------------------------------- @RequestMapping("print")
@ResponseBody
public void printFxt(Long tm_uuid, HttpServletRequest request, HttpServletResponse response) throws IOException{
//必须是map集合
Map<String, Object> parameters = this.carSeqMapper.selectFxt(tm_uuid);
ServletOutputStream outPutStream = response.getOutputStream();
//注意.jasper的是相对路径,不是绝对路径
InputStream inputStream = request.getServletContext().getResourceAsStream("/template/cljcfxt.jasper");
parameters.put("pic1", "check.png");
parameters.put("pic2", "check.png");
parameters.put("pic3", "check.png");
parameters.put("pic4", "check.png");
parameters.put("contextPath", request.getServletContext().getRealPath("/"));
//按条件选择复选框
String operType = (String) parameters.get("oper_type");
if("1".equals(operType)){
parameters.put("pic1", "checked.png");
}
else if("2".equals(operType)){
parameters.put("pic2", "checked.png");
}
JRDataSource jrDataSource = new JREmptyDataSource();
try {
//生成网页pdf
JasperRunManager.runReportToPdfStream(inputStream, outPutStream, parameters, jrDataSource);
response.setContentType("application/pdf");
} catch (Exception e) {
e.printStackTrace();
response.setContentType("text/plain; charset=utf-8");
response.setCharacterEncoding("utf-8");
outPutStream.write("生成pdf文件时,服务器发生异常".getBytes("utf-8"));
}
finally{
if(outPutStream != null){
outPutStream.flush();
outPutStream.close();
}
}
}
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
4.jrxml文件里面的变量属性要跟map集合里面的key值相对应,要修改jrxml直接在ireport里面修改,编译之后再放到java项目里面去覆盖之前的文件。
5.pom.xml文件中引入以下内容:
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------<dependency>
<groupId>feiliks.hn</groupId>
<artifactId>jasperreports</artifactId>
<version>6.4.1</version>
</dependency>
<dependency>
<groupId>com.lowagie</groupId>
<artifactId>itext</artifactId>
<version>2.1.7</version>
<scope>compile</scope>
<exclusions>
<exclusion>
<groupId>org.bouncycastle</groupId>
<artifactId>bcmail-jdk14</artifactId>
</exclusion>
<exclusion>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk14</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>feiliks.hn</groupId>
<artifactId>feiliks-font</artifactId>
<version>1.0.1</version>
</dependency>
<dependency>
<groupId>commons-digester</groupId>
<artifactId>commons-digester</artifactId>
<version>2.1</version>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>2.4.5</version>
</dependency>
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
6.页面跳转要跳到一个新的页面,即<a>标签里面设置target="_blank"。
7.页面pdf自带打印功能,OK了,一个动态生成PDF的功能就实现了。