介绍

本篇博客主要是为了介绍如何使用: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的更多相关文章

  1. Java 生成pdf表格文档

    最近在工作做一个泰国的项目,应供应商要求,需要将每天的交易生成pdf格式的报表上传到供应商的服务器,特此记录实现方法.废话不多说,直接上代码: THSarabunNew.ttf该文件是泰国字体自行网上 ...

  2. JAVA生成PDF文件

    生成PDF文件是主要应用的是ITEXT插件 import java.awt.Color; import java.io.File; import java.io.FileOutputStream; i ...

  3. JAVA 生成PDF报表()

    许多应用程序都要求动态生成 PDF 文档.这些应用程序涵盖从生成客户对帐单并通过电子邮件交付的银行到购买特定的图书章节并以 PDF 格式接收这些图书章节的读者.这个列表不胜枚举.在本文中,我们将使用 ...

  4. Java生成PDF报表

    一.前言 前几天,做ASN条码收货模块,需要实现打印下载收货报表,经一番查找,选定iText--用于生成PDF文档的一个Java类库.废话不多说,进入正题. 二.iText简介 iText是著名的开放 ...

  5. java生成PDF文件(itext)

    itextpdf-5.4.3.jar下载地址: http://www.kuaipan.cn/file/id_58980483773788178.htm 导入itextpdf-5.4.3.jar ToP ...

  6. 电子凭证 : Java 生成 Pdf

    来源:蛙牛, my.oschina.net/lujianing/blog/894365 如有好文章投稿,请点击 → 这里了解详情 1.背景 在某些业务场景中,需要提供相关的电子凭证,比如网银/支付宝中 ...

  7. java生成PDF,各种格式、样式、水印都有

    代码中有两处需要图片,请自行替换. 一个是水印.一个是手指. 需要的JAR包链接:http://download.csdn.net/detail/justinytsoft/9688893 下面是预览: ...

  8. java生成PDF,并下载到本地

    1.首先要写一个PDF工具类,以及相关工具 2.PDF所需jar包 iText是一种生成PDF报表的Java组件 freemarker是基于模板来生成文本输出 <dependency> & ...

  9. Java生成PDF文件(转)

    原文地址:https://www.cnblogs.com/shuilangyizu/p/5760928.html 一.前言 前几天,做ASN条码收货模块,需要实现打印下载收货报表,经一番查找,选定iT ...

随机推荐

  1. [Python 多线程] Barrier (十一)

    Barrier 栅栏,也叫屏障.可以想象成路障.道闸. Python 3.2引入的新功能. 构造方法: threading.Barrier(parties, action=None, timeout= ...

  2. Dynamic Rankings—带单点修改的主席树

    这道题应该是很不错的板子了\(\mathcal{\color{cyan}{Link}}\) \(\mathcal{\color{red}{Description}}\) 给定一个序列,有两种操作.一是 ...

  3. 网络测量中基于Sketch方法的简单介绍

    Sketch介绍 为什么要用Sketch 网络流主要根据五元组.主机地址.包的大小来分类.在网络中存在各种各样的包,如果按照上述分类方法,对每一种包都分配一个计数器来储存,虽然测量准确,那么存放计数器 ...

  4. javaSE web开发 登录思路代码

    准备工作: 1.在WebContent  WEB-INF   lib下添加c3p0-0.9.5.2.jar     mchange-common-java-0.2.11.jar   ojdbc6.ja ...

  5. Linux(CentOS7)设置自动备份数据库到阿里云OSS

    环境:阿里云服务器CentOS7.4 + MySQL5.6 基本思路: 1.编写shell脚本,备份数据库到指定目录下 2.编写Python脚本,把文件上传到OSS 3.把shell脚本和Python ...

  6. JQuery 同时获取多个标签的指定内容并储存为数组

    在网页开发中经常会碰到获取同种类型的 标签 的值得问题,比如下面的两种情况. 当需要批量获取同种标签的指定值时,新人就会碰上一点小麻烦. 比如 id=problem1的demo var list1=$ ...

  7. Windows Oracle连接ORA-12541:TNS:无监听程序

    上面两篇是安装和连接,这里写一下碰到的问题,安装后使用SQL Developer连接出现ORA-12541:TNS:无监听程序.网上搜了一下是安装的Oracle中自带的JDK是32位,而程序中使用的S ...

  8. 使用img标签能使用background-size:conver一样的效果

    使用css img { object-fit: cover; object-position:left center; } 就可以达到 和 background-size:cover; 一样的效果 共 ...

  9. PHP中的递增/递减运算符

    看这段代码 <?php $a=10; $b=++$a; //此语句等同于 ; $a=$a+1 ; $b=$a echo $a."<br>"; echo $b; ? ...

  10. net core mysql 组件记录

    nuget 下 Pomelo.EntityFrameworkCore.MySql (据说是微软官方推荐) MySql.Data.EntityFrameworkCore (甲骨文出品) 使用方式等同于  ...