java生成pdf
介绍
本篇博客主要是为了介绍如何使用:flying-saucer+itext+freemark实现导出复杂点的pdf文件。
思路
- 先把pdf的内容以html形式准备好
- 使用freemarker将html中的动态内容替换掉
- 使用flying-saucer生成pdf文件
下载jar包
<dependency>
<groupId>org.xhtmlrenderer</groupId>
<artifactId>flying-saucer-pdf</artifactId>
<version>9.1.5</version>
</dependency>
下载中文字体
搜索:simsun.ttc
宋体(对应css中的 属性 font-family: SimSun; /宋体/)
准备好pdf的模板
把这个模板放到
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>准考证</title>
<style>
@page { size: 13.8in 10.4in; }
</style>
<!--@page { size: 13.8in 10.4in; } 这个可以调整pdf页面的大小很好用-->
</head>
<body>
<div style="width: 1200px; margin:0 auto; background: #fff; position: relative">
<img src="/isuyang/images/steam/students.jpg" width="1200px;" alt=""/>
<span style="color:#333; font-size:12.0pt; font-family: ; position: absolute; top: 63px; left:154px;">${certificateNumber}</span>
<span style="color:#333; font-size:20.0pt; font-family: SimSun; font-weight: bold; position: absolute; top: 320px; left:236px;width: 160px; text-align: center">${realName}</span>
<span style="color:#333; font-size:20.0pt; font-family: SimSun; font-weight: bold; position: absolute; top: 426px; left:236px;width: 140px; text-align: center">${division}</span>
<span style="color:#333; font-size:20.0pt; font-family: SimSun; font-weight: bold; position: absolute; top: 426px; left:428px;width: 140px; text-align: center">${grade}</span>
<span style="color:#333; font-size:20.0pt; font-family: SimSun; font-weight: bold; position: absolute; top: 426px; left:612px;width: 140px; text-align: center">${prize}</span>
</div>
</body>
</html>
配置Freemaker
添加引用
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.20</version>
</dependency>
在src/main/resource目录下创建ftl.properties文件
这个文件不一定非得放在这里,只是maven项目约定资源文件的目录是在这。
ftl的文件内容:
classic_compatible=true
##如果变量为null,转化为空字符串,比如做比较的时候按照空字符做比较
whitespace_stripping=true
##去掉多余的空格,非常有用
##模版更新事件,设置为1秒,正式环境设置为3600秒
#template_update_delay=3600
template_update_delay=1
##模版更新时间,这里配置是1秒更新一次,正式环境,模版不会改变,可以将这个值设很大,提高效率
##locale=zh_CN
##中国
default_encoding=utf-8
##编码utf8
url_escaping_charset=utf-8
##url编码utf8
date_format=yyyy-MM-dd
##显示日期格式
time_format=HH:mm:ss
##显示时间格式
datetime_format=yyyy-MM-dd HH:mm:ss
##显示日期格式
number_format=0.######
##数字显示格式
boolean_format=是,否
## boolean显示格式
tag_syntax=auto_detect
##设置标签类型 两种:[] 和 <> 。[] 这种标记解析要快些
在ftl.xml文件里配置freemarker.template.Configuration
记得在spring-context.xml文件里引用这个文件,
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd"
default-lazy-init="true">
<description>ftl Configuration</description>
<!-- 加载配置属性文件 -->
<context:property-placeholder ignore-unresolvable="true" location="classpath:config/config.properties" />
<bean id="configuration" class="freemarker.template.Configuration"/>
</beans>
在启动的时候加载配置
/**
* 启动
* <p>
* 应用的启动入口
*
* @author ZhuangJunxiang(529272571@qq.com)
* @Date 2017年4月10日
*/
@Component
public class Setup implements ServletContextAware {
@Override
public void setServletContext(final ServletContext context) {
WebApplicationContext act = ContextLoader.getCurrentWebApplicationContext();
Configuration ftlConf = (Configuration) act.getBean("configuration");
FtlUtil.initConfiguration(ftlConf, context, "/", "config/ftl.properties");
}
}
编写Controller
/**
* 下载初赛的pdf证书
*
*/
@Void //这个是自定义注解,可以删除
@RequestMapping
@ResponseBody
public void downloadChuSaiPrizePdf() {
stemViewService.downloadPdf();
}
编写steamViewService
public void downloadPdf() {
String pdfPath = generatorPdf(getSteamSignup(), "student.ftl");
boolean exist = FileUtil.isExist(pdfPath);
if (!exist) {
throw ExceptionUtil.bEx("PDF不存在");
}
try {
DownloadUtil.download(MvcUtil.getResponse(), pdfPath);
} catch (Exception e) {
e.printStackTrace();
} finally {
FileUtil.deleteFile(pdfPath);
}
}
/**
* 生成pdf
*
* @param parameter
* @return TODO(这里描述每个参数,如果有返回值描述返回值,如果有异常描述异常)
*/
public String generatorPdf(Map<String, Object> parameter, String ftlPath) {
//Pdf的存放路径:"G:\\pdf\\" 这里的路径我是通过配置获取的
String pdfPath = pdfConfig.getValue("pdfPath") + parameter.get("certificateNumber") + ".pdf";
//字体的存放路径 这里也是通过配置获取的:可以写:G:\\pdf\\simsun.ttc
String font1 = pdfConfig.getValue("fontPath");
OutputStream os = null;
try {
os = new FileOutputStream(pdfPath);
ITextRenderer renderer = new ITextRenderer();
ResourceLoaderUserAgent callback = new ResourceLoaderUserAgent(renderer.getOutputDevice());
renderer.getSharedContext().setUserAgentCallback(callback);
callback.setSharedContext(renderer.getSharedContext());
//添加中文字体
renderer.getFontResolver().addFont(font1, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
//这里的ftl路径存放在WEB-INF/steam/下
String repAfterHtml = FtlUtil.build(configuration, "steam/" + ftlPath, parameter);
renderer.setDocumentFromString(repAfterHtml);
// 解决图片的相对路径问题,这里也可以写:http://localhost:8080/的形式
renderer.getSharedContext().setBaseURL("http://static.isuyang.cn/");
renderer.layout();
renderer.createPDF(os);
os.close();
os = null;
} catch (Exception ex) {
ex.printStackTrace();
} finally {
if (os != null) {
try {
os.close();
} catch (IOException e) {
// ignore
}
}
}
return pdfPath;
}
/**
* 资源加载代理
*
*/
private static class ResourceLoaderUserAgent extends ITextUserAgent {
public ResourceLoaderUserAgent(ITextOutputDevice outputDevice) {
super(outputDevice);
}
protected InputStream resolveAndOpenStream(String uri) {
InputStream is = super.resolveAndOpenStream(uri);
System.out.println("加载资源文件: " + uri);
return is;
}
}
DownloadUtil.java
package com.we.core.web.util;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import javax.servlet.http.HttpServletResponse;
import com.we.core.common.util.ExceptionUtil;
import com.we.core.common.util.FileUtil;
/**
* 下载方法
* @author lq
* @Date 2014-7-15
*/
public class DownloadUtil {
public static HttpServletResponse download(final HttpServletResponse response, final String path) throws Exception {
return download(response, path, null);
}
public static HttpServletResponse download(final HttpServletResponse response, final String path,
final String fileName) throws Exception {
File file = new File(path);
String fn = fileName;
if (com.we.core.common.util.Util.isEmpty(fileName)) {
fn = file.getName();
} else {
if (fileName.indexOf(".") == -1) {
fn = fileName + "." + FileUtil.getSuffix(file.getName());
}
}
InputStream fis = new BufferedInputStream(new FileInputStream(path));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
response.reset();
response.addHeader("Content-Disposition", "attachment;filename="
+ new String(fn.getBytes("gb2312"), "ISO8859-1"));
response.addHeader("Content-Length", "" + file.length());
OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
toClient.write(buffer);
toClient.flush();
toClient.close();
return response;
}
public static HttpServletResponse viewWord(final HttpServletResponse response, final String path) throws Exception {
try {
File file = new File(path);
String filename = file.getName();
InputStream fis = new BufferedInputStream(new FileInputStream(path));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
response.reset();
response.addHeader("Content-Disposition", "inline;filename="
+ new String(filename.getBytes("gb2312"), "ISO8859-1"));
OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/pdf");
toClient.write(buffer);
toClient.flush();
toClient.close();
} catch (Exception e) {
throw ExceptionUtil.bEx("文件损坏,预览失败");
}
return response;
}
public HttpServletResponse viewTxt(final HttpServletResponse response, final String path) throws Exception {
try {
BufferedReader bis = null;
File file = new File(path);
InputStream in = new FileInputStream(file);
bis = new BufferedReader(new InputStreamReader(in));
StringBuffer buf = new StringBuffer();
String temp;
while ((temp = bis.readLine()) != null) {
buf.append(temp);
response.getWriter().write(temp);
if (buf.length() >= 1000) {
break;
}
}
bis.close();
} catch (Exception e) {
throw ExceptionUtil.bEx("文件损坏,预览失败");
}
return response;
}
}
效果
建议
最好参考官方文档,因为好多写的都是不全,还有就是在搜索百度的时候一定要搜索博客靠前的文章,可以使用百度的搜索工具过滤一下。
https://developers.itextpdf.com/examples/xml-worker-itext5/html-images
java生成pdf的更多相关文章
- Java 生成pdf表格文档
最近在工作做一个泰国的项目,应供应商要求,需要将每天的交易生成pdf格式的报表上传到供应商的服务器,特此记录实现方法.废话不多说,直接上代码: THSarabunNew.ttf该文件是泰国字体自行网上 ...
- JAVA生成PDF文件
生成PDF文件是主要应用的是ITEXT插件 import java.awt.Color; import java.io.File; import java.io.FileOutputStream; i ...
- JAVA 生成PDF报表()
许多应用程序都要求动态生成 PDF 文档.这些应用程序涵盖从生成客户对帐单并通过电子邮件交付的银行到购买特定的图书章节并以 PDF 格式接收这些图书章节的读者.这个列表不胜枚举.在本文中,我们将使用 ...
- Java生成PDF报表
一.前言 前几天,做ASN条码收货模块,需要实现打印下载收货报表,经一番查找,选定iText--用于生成PDF文档的一个Java类库.废话不多说,进入正题. 二.iText简介 iText是著名的开放 ...
- java生成PDF文件(itext)
itextpdf-5.4.3.jar下载地址: http://www.kuaipan.cn/file/id_58980483773788178.htm 导入itextpdf-5.4.3.jar ToP ...
- 电子凭证 : Java 生成 Pdf
来源:蛙牛, my.oschina.net/lujianing/blog/894365 如有好文章投稿,请点击 → 这里了解详情 1.背景 在某些业务场景中,需要提供相关的电子凭证,比如网银/支付宝中 ...
- java生成PDF,各种格式、样式、水印都有
代码中有两处需要图片,请自行替换. 一个是水印.一个是手指. 需要的JAR包链接:http://download.csdn.net/detail/justinytsoft/9688893 下面是预览: ...
- java生成PDF,并下载到本地
1.首先要写一个PDF工具类,以及相关工具 2.PDF所需jar包 iText是一种生成PDF报表的Java组件 freemarker是基于模板来生成文本输出 <dependency> & ...
- Java生成PDF文件(转)
原文地址:https://www.cnblogs.com/shuilangyizu/p/5760928.html 一.前言 前几天,做ASN条码收货模块,需要实现打印下载收货报表,经一番查找,选定iT ...
随机推荐
- 解决windows7无法连接CentOS7系统中oracle问题:ORA-12514 TNS 监听程序当前无法识别
linux开启后终端按下面输入(容易忘记,记录下): [oracle@localhost ~]$ lsnrctl stop #先关闭监听服务 [oracle@localh ...
- Tomcat中的Filter
Filter 节选部分源码.源码版本 Tomcat8.5 说明 filter 是 Servlet 规范 filter 是在 ,执行 Servlet.service方法之前执行 Filter相关接口 p ...
- Genymotion集成到Eclipse
在Eclipse中使用Genymotion Google的ADT中自带的模拟器速度太慢,可以使用Genymotion代替.关于Genymotion的安装方法,可以直接访问官网,需要注册账号,因为创建模 ...
- 如何导出SHP文件中的点坐标?(ArcGIS10)
行政区域坐标,网上流传较广的版本是包括海域的,假如你仅仅想要把陆地边界绘出,那么怎么办呢? 现在讲一下用arcgis 10从shp线.面文件中获取对应区域的坐标呢?(点图层忽略第一步) 首先用在arc ...
- Linux 防止rm -rf 误删Shell脚本
#!/bin/bash #:set ff=unix #:set nobomb #-*- coding:utf-8 -*- ####################################### ...
- 精准测试白皮书v3.0-2019最新版
现代社会是建立在各种以计算机为基石的软件技术基础之上的.随着日新月异的需求变化,软件系统越来越复杂.很多人觉得软件开发才是重要环节,但实际上,无法对大型软件进行有效的质量把控,就无法真正构建与维护大型 ...
- Oracle AWR与警报系统一
管理自动工作负荷知识库 Oracle收集大量有关性能和活动的统计信息.这些信息在内存中积累,并定期写入数据库:写入到构成自动工作负荷知识库(Automatic Workload Repository, ...
- Nginx与Tomcat实现请求动态数据与请求静态资源的分离
上篇博客说明了Nginx在应用架构中的作用,以及负载均衡的思路.这篇实践一下其中的访问静态资源与访问动态资源的操作. 一.认识访问静态资源与访问动态资源的区别 静态资源:指存储在硬盘内的数据,固定的数 ...
- MySQL----MySQL数据库入门----第三章 添加、更新与删除数据
3.1 添加数据 ①为所有字段添加数据 方法1:字段包含全部定义的字段 insert into 表名(字段1,字段2...字段n) values(值1,值2,......,值n); 方法2:值必须与字 ...
- Python学习:15.Python面向对象(二、继承的各种情况)
一.什么是继承 继承是一种创建类的方法,在python中,一个类可以继承来自一个或多个父.原始类称为基类或超类. #创建父类 class Parent1: pass class Parent2: pa ...