這裡demo用的maven來管理項目,pom.xml如下:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.zy</groupId>
<artifactId>ftl2pdf</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<servlet>3.1.0</servlet>
<freemarker>2.3.22</freemarker>
<flying-saucer>9.1.12</flying-saucer>
</properties>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>${servlet}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>${freemarker}</version>
</dependency>
<dependency>
<groupId>org.xhtmlrenderer</groupId>
<artifactId>flying-saucer-pdf</artifactId>
<version>${flying-saucer}</version>
</dependency>
<!-- log4j必需 -->
<dependency>
<groupId>log</groupId>
<artifactId>log4j12</artifactId>
<version>1.6.6</version>
</dependency>
<dependency>
<groupId>log</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
<dependency>
<groupId>log</groupId>
<artifactId>log4j-api</artifactId>
<version>1.6.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.46</version>
</dependency>
</dependencies>
</project>
然後
package com.zy.common;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.StringWriter;
import java.util.Locale;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xhtmlrenderer.pdf.ITextRenderer;
import com.lowagie.text.DocumentException;
import com.lowagie.text.pdf.BaseFont;
import freemarker.core.ParseException;
import freemarker.template.Configuration;
import freemarker.template.MalformedTemplateNameException;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.TemplateNotFoundException;
/**
* <p>Title: PdfUtils.java</p>
* <p>Description: PDF生成工具類</p>
* <p>Company: </p>
* @author ZY
* <p> Just go on !!!</p>
* @date 2018年3月1日 下午8:44:18
* @version v1.0
*/
@SuppressWarnings("all")
public class PDFUtil {
private static final Logger logger = LoggerFactory.getLogger(PDFUtil.class);
/**
* <p>Description: 生成PDF到檔案</p>
* <p>Company: </p>
* @author ZY
* @param ftlPath 模闆檔案路徑(不含檔案名)
* @param ftlName 模闆檔案(不含路徑)
* @param imageDiskPath 圖檔的磁盤路徑
* @param data 資料 (填到模闆上的資料)
* @param outputFile 目标檔案(全路徑名稱)
* @throws Exception
*/
public static void generateToFile(String ftlPath, String ftlName, String imageDiskPath, Object data,String outputFile) throws Exception {
OutputStream out = null;
ITextRenderer render = null;
try {
String html = getPdfContent(ftlPath, ftlName, data); //組裝模闆和資料生成html串
out = new FileOutputStream(outputFile);
render = getRender();
render.setDocumentFromString(html); //此處抛異常
if (imageDiskPath != null && !imageDiskPath.equals("")) {
// html中如果有圖檔,圖檔的路徑則使用這裡設定的路徑的相對路徑,這個是作為根路徑
render.getSharedContext().setBaseURL("file:/" + imageDiskPath);
}
render.layout();
render.createPDF(out);
render.finishPDF();
render = null;
} catch (Exception e) {
logger.error("Exception:",e);
throw e;
}finally{
if (out != null) {
try {
out.close();
} catch (IOException e) {
logger.error("Exception:",e);
throw e;
}
}
}
}
/**
* 生成PDF到輸出流中(ServletOutputStream用于下載下傳PDF)
*
* @param ftlPath
* ftl模闆檔案的路徑(不含檔案名)
* @param ftlName
* ftl模闆檔案的名稱(不含路徑)
* @param imageDiskPath
* 如果PDF中要求圖檔,那麼需要傳入圖檔所在位置的磁盤路徑
* @param data
* 輸入到FTL中的資料
* @param response
* HttpServletResponse
* @return
* @throws TemplateNotFoundException
* @throws MalformedTemplateNameException
* @throws ParseException
* @throws IOException
* @throws TemplateException
* @throws DocumentException
*/
public static OutputStream generateToServletOutputStream(String ftlPath, String ftlName, String imageDiskPath,
Object data, HttpServletResponse response) throws Exception {
String html = getPdfContent(ftlPath, ftlName, data);
OutputStream out = null;
ITextRenderer render = null;
out = response.getOutputStream();
render = getRender();
render.setDocumentFromString(html);
if (imageDiskPath != null && !imageDiskPath.equals("")) {
// html中如果有圖檔,圖檔的路徑則使用這裡設定的路徑的相對路徑,這個是作為根路徑
render.getSharedContext().setBaseURL("file:/" + imageDiskPath);
}
render.layout();
render.createPDF(out);
render.finishPDF();
render = null;
return out;
}
public static ITextRenderer getRender() throws DocumentException, IOException {
ITextRenderer render = new ITextRenderer();
String path = getPath();
// 添加字型,以支援中文
render.getFontResolver().addFont(path + "fonts/ARIALUNI.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
render.getFontResolver().addFont(path + "fonts/SIMSUN.TTC", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
return render;
}
// 擷取要寫入PDF的内容
public static String getPdfContent(String ftlPath, String ftlName, Object o) throws Exception {
return useTemplate(ftlPath, ftlName, o);
}
// 使用freemarker得到html内容
public static String useTemplate(String ftlPath, String ftlName, Object o) throws Exception {
String html = null;
Template tpl = getFreemarkerConfig(ftlPath).getTemplate(ftlName);
tpl.setEncoding("UTF-8");
StringWriter writer = new StringWriter();
tpl.process(o, writer);
writer.flush();
html = writer.toString();
return html;
}
/**
* 擷取Freemarker配置
*
* @param templatePath
* @return
* @throws IOException
*/
private static Configuration getFreemarkerConfig(String templatePath) throws IOException {
Configuration config = new Configuration();
config.setDirectoryForTemplateLoading(new File(templatePath));
config.setEncoding(Locale.CHINA, "utf-8");
return config;
}
/**
* 擷取類項目根路徑
*
* @return
*/
public static String getPath() {
//return PDFUtil.class.getResource("").getPath().substring(1); //傳回類路徑(目前類所在的路徑)
return PDFUtil.class.getResource("/").getPath().substring(1); //傳回項目根路徑(編譯之後的根路徑)
}
}
字型、圖檔、模闆放在resource目錄下:
下面貼一個示例模闆:
<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<meta http-equiv="Content-Style-Type" content="text/css"/>
<title>文檔标題</title>
<style type="text/css">
body{
margin: 0 45px;
font-family: Arial Unicode MS;
font-size: 12px;
color: #000;
}
.document_body h1 ,
.document_body h2 ,
.document_body h3 ,
.document_body h4 ,
.document_body h5 ,
.document_body h6 ,
.document_body p{
width:100%;
height: 100%;
color: #000;
margin: 0;
font-weight: normal;
text-indent: 0;
}
.document_body .logo_position{
width: 20%;
position: absolute;
top: 20px;
left: 0;
}
.document_body .logo_position >img{width: 100%;}
.document_body{
width: 100%; /*可以修改此處的寬度百分比來适應頁面大小*/
padding-top: 40px;
margin: 0 auto;
position: relative;
}
.document_body p{
text-align: justify;
text-justify: inter-word;
}
.document_head{margin-bottom: 10px;}
.document_head .heading{text-align: right;}
.document_head .heading h1{
font-size: 20px;
line-height: 36px;
text-align: right;
font-weight: bold;
}
.document_head .heading h6{
text-align: right;
font-weight: bold;
font-size: 12px;
}
table{
width: 100%;
text-align: left;
margin-bottom: 10px;
}
table .table_title{
background: #999;
color: #fff;
font-size: 14px;
text-align: center;
font-weight: bold;
}
table .strong_area{
background: #FFEC00;
}
span.line_input{
display: inline-block;
border-bottom: 1px solid #000;
padding: 0 6px;
margin-left: 2px;
}
.document_body .document_main .blank{font-family: "Times New Roman"}
.sign_area{
margin-top: 10px;
}
.sign_area > div{
float: left;
}
.sign_area > div >b{
display: block;
height: 30px;
font-weight: normal;
}
strong{
font-weight: bold;
color: red;
}
.explain_text p{line-height: 1.5}
.explain_text h5{font-weight: bold;}
.clear{clear: both;}
.mr10{margin-right: 10px;}
.link a{
font-weight: bold;
color: #000;
font-size: 13px;
}
.document_body .side_tips{
position: absolute;
top:400px;
right: -18px;
width: 10px;
}
</style>
</head>
<body>
<!--文檔start-->
<div class="document_body">
<div class="document_head">
<div class="logo_position"><img src="logo_269_48.png" alt="logo"/></div>
<div class="heading">
<h1>文檔标題</h1>
<h6>提示:警惕洗錢風險,保護您的權益</h6>
</div>
</div>
<div class="document_main">
<table cellspacing="0" cellpadding="4">
<tr>
<td colspan="3" class="table_title">賬 戶 信 息 1</td>
</tr>
<tr class="strong_area">
<td align="center">存款人名稱</td>
<td colspan="2">${data.remark1}</td>
</tr>
<tr>
<td align="center">辦公位址</td>
<td colspan="2">
<#if data.remark2 == "01">
<input type="checkbox" checked="true"/><label class="mr10">同注冊位址</label>或 <input type="checkbox"/><label>其它:</label><span class="line_input"> </span>省/區/直轄市<span class="line_input"> </span>市 <span class="line_input"> </span> 區/縣 <span class="line_input"> </span>
<#else>
<input type="checkbox"/><label class="mr10">同注冊位址</label>或 <input type="checkbox" checked="true"/><label>其它:</label><span class="line_input">${data.remark3}</span>省/區/直轄市<span class="line_input">${data.remark4}</span>市 <span class="line_input">${data.remark5}</span> 區/縣 <span class="line_input">${data.remark6}</span>
</#if>
</td>
</tr>
<tr>
<td align="center">郵寄位址</td>
<td colspan="2">
<#if data.remark7 == "01">
<input type="checkbox" checked="true"/><label class="mr10">同注冊位址</label>或 1.<input type="checkbox"/><label class="mr10">同辦公位址</label> 2.<input type="checkbox"/><label>其它:</label><span class="line_input"> </span>省/區/直轄市<span class="line_input"> </span>市 <span class="line_input"> </span> 區/縣 <span class="line_input"> </span>
<#elseif data.remark7 == "02">
<input type="checkbox"/><label class="mr10">同注冊位址</label>或 1.<input type="checkbox" checked="true"/><label class="mr10">同辦公位址</label> 2.<input type="checkbox"/><label>其它:</label><span class="line_input"> </span>省/區/直轄市<span class="line_input"> </span>市 <span class="line_input"> </span> 區/縣 <span class="line_input"> </span>
<#else>
<input type="checkbox"/><label class="mr10">同注冊位址</label>或 1.<input type="checkbox"/><label class="mr10">同辦公位址</label> 2.<input type="checkbox" checked="true"/><label>其它:</label><span class="line_input">${data.remark8}</span>省/區/直轄市<span class="line_input">${data.remark9}</span>市 <span class="line_input">${data.remark10}</span> 區/縣 <span class="line_input">${data.remark11}</span>
</#if>
</td>
</tr>
<tr>
<td rowspan="3" align="center">聯系資訊</td>
<td>
<#if data.remark12 == "01">
<input type="checkbox" checked="true"/><label class="mr10">法定代表人</label>或<br/><input type="checkbox"/><label>機關負責人</label>
<#else>
<input type="checkbox"/><label class="mr10">法定代表人</label>或<br/><input type="checkbox" checked="true"/><label>機關負責人</label>
</#if>
</td>
<td>辦公電話:區号<span class="line_input">${data.remark13}</span>直線<span class="line_input">${data.remark14}</span>分機<span class="line_input">${data.remark15}</span><br/>行動電話:<span class="line_input">${data.remark16}</span></td>
</tr>
<tr>
<td class="strong_area" align="center">财務負責人</td>
<td>姓名:<span class="line_input mr10">${data.remark17}</span>辦公電話:區 号<span class="line_input">${data.remark18}</span>直線<span class="line_input">${data.remark19}</span>分機<span class="line_input">${data.remark20}</span><br/>行動電話:<span class="line_input mr10">${data.remark21}</span>電子郵箱:<span class="line_input">${data.remark22}</span></td>
</tr>
<tr class="strong_area">
<td align="center">财務經辦人員</td>
<td>姓名:<span class="line_input mr10">${data.remark23}</span>辦公電話:區 号<span class="line_input">${data.remark24}</span>直線<span class="line_input">${data.remark25}</span>分機<span class="line_input">${data.remark26}</span><br/>行動電話:<span class="line_input">${data.remark27}</span></td>
</tr>
<tr>
<td rowspan="2" align="center">賬戶種類</td>
<td align="center">人民币</td>
<td>
<#if data.remark28 == "01">
1.<input type="checkbox" checked="true"/><label class="mr10">定期賬戶</label>2.<input type="checkbox"/><label class="mr10">基本戶</label>3.<input type="checkbox"/><label>一般戶且用于:</label>
<input type="checkbox"/><label class="mr10">結算</label><input type="checkbox"/><label class="mr10">貸款</label>或 <input type="checkbox"/><label>其它:</label> <span class="line_input"> </span><br/>
4.<input type="checkbox"/><label class="mr10">臨時戶</label>5.<input type="checkbox"/><label>專使用者且賬戶資金性質為:</label><span class="line_input mr10"> </span>6.<input type="checkbox"/><label>NRA賬戶且國家代碼為:</label><span class="line_input mr10"> </span>
<#elseif data.remark28 == "02">
1.<input type="checkbox"/><label class="mr10">定期賬戶</label>2.<input type="checkbox" checked="true"/><label class="mr10">基本戶</label>3.<input type="checkbox"/><label>一般戶且用于:</label>
<input type="checkbox"/><label class="mr10">結算</label><input type="checkbox"/><label class="mr10">貸款</label>或 <input type="checkbox"/><label>其它:</label> <span class="line_input"> </span><br/>
4.<input type="checkbox"/><label class="mr10">臨時戶</label>5.<input type="checkbox"/><label>專使用者且賬戶資金性質為:</label><span class="line_input mr10"> </span>6.<input type="checkbox"/><label>NRA賬戶且國家代碼為:</label><span class="line_input mr10"> </span>
<#elseif data.remark28 == "03">
1.<input type="checkbox"/><label class="mr10">定期賬戶</label>2.<input type="checkbox"/><label class="mr10">基本戶</label>3.<input type="checkbox" checked="true"/><label>一般戶且用于:</label>
<#if data.remark29 == "01">
<input type="checkbox" checked="true"/><label class="mr10">結算</label><input type="checkbox"/><label class="mr10">貸款</label>或 <input type="checkbox"/><label>其它:</label> <span class="line_input"> </span><br/>
<#elseif data.remark29 == "02">
<input type="checkbox"/><label class="mr10">結算</label><input type="checkbox" checked="true"/><label class="mr10">貸款</label>或 <input type="checkbox"/><label>其它:</label> <span class="line_input"> </span><br/>
<#else>
<input type="checkbox"/><label class="mr10">結算</label><input type="checkbox"/><label class="mr10">貸款</label>或 <input type="checkbox" checked="true"/><label>其它:</label> <span class="line_input">${data.remark30}</span><br/>
</#if>
4.<input type="checkbox"/><label class="mr10">臨時戶</label>5.<input type="checkbox"/><label>專使用者且賬戶資金性質為:</label><span class="line_input mr10"> </span>6.<input type="checkbox"/><label>NRA賬戶且國家代碼為:</label><span class="line_input mr10"> </span>
<#elseif data.remark28 == "04">
1.<input type="checkbox"/><label class="mr10">定期賬戶</label>2.<input type="checkbox"/><label class="mr10">基本戶</label>3.<input type="checkbox"/><label>一般戶且用于:</label>
<input type="checkbox"/><label class="mr10">結算</label><input type="checkbox"/><label class="mr10">貸款</label>或 <input type="checkbox"/><label>其它:</label> <span class="line_input"> </span><br/>
4.<input type="checkbox" checked="true"/><label class="mr10">臨時戶</label>5.<input type="checkbox"/><label>專使用者且賬戶資金性質為:</label><span class="line_input mr10"> </span>6.<input type="checkbox"/><label>NRA賬戶且國家代碼為:</label><span class="line_input mr10"> </span>
<#elseif data.remark28 == "05">
1.<input type="checkbox"/><label class="mr10">定期賬戶</label>2.<input type="checkbox"/><label class="mr10">基本戶</label>3.<input type="checkbox"/><label>一般戶且用于:</label>
<input type="checkbox"/><label class="mr10">結算</label><input type="checkbox"/><label class="mr10">貸款</label>或 <input type="checkbox"/><label>其它:</label> <span class="line_input"> </span><br/>
4.<input type="checkbox"/><label class="mr10">臨時戶</label>5.<input type="checkbox" checked="true"/><label>專使用者且賬戶資金性質為:</label><span class="line_input mr10">${data.remark31}</span>6.<input type="checkbox"/><label>NRA賬戶且國家代碼為:</label><span class="line_input mr10"> </span>
<#else>
1.<input type="checkbox"/><label class="mr10">定期賬戶</label>2.<input type="checkbox"/><label class="mr10">基本戶</label>3.<input type="checkbox"/><label>一般戶且用于:</label>
<input type="checkbox"/><label class="mr10">結算</label><input type="checkbox"/><label class="mr10">貸款</label>或 <input type="checkbox"/><label>其它:</label> <span class="line_input"> </span><br/>
4.<input type="checkbox"/><label class="mr10">臨時戶</label>5.<input type="checkbox"/><label>專使用者且賬戶資金性質為:</label><span class="line_input mr10"> </span>6.<input type="checkbox" checked="true"/><label>NRA賬戶且國家代碼為:</label><span class="line_input mr10">${data.remark32}</span>
</#if>
</td>
</tr>
<tr>
<td align="center">外 币</td>
<td>
币種:
<#if data.remark33 == "01">
1.<input type="checkbox" checked="true"/><label class="mr10">美元</label>2.<input type="checkbox"/><label class="mr10">港币</label>3.<input type="checkbox"/><label class="mr10">歐元</label>4.<input type="checkbox"/><label class="mr10">日元</label>5.<input type="checkbox"/><label >其它:</label><span class="line_input"> </span><br/>
<#elseif data.remark33 == "02">
1.<input type="checkbox"/><label class="mr10">美元</label>2.<input type="checkbox" checked="true"/><label class="mr10">港币</label>3.<input type="checkbox"/><label class="mr10">歐元</label>4.<input type="checkbox"/><label class="mr10">日元</label>5.<input type="checkbox"/><label >其它:</label><span class="line_input"> </span><br/>
<#elseif data.remark33 == "03">
1.<input type="checkbox"/><label class="mr10">美元</label>2.<input type="checkbox"/><label class="mr10">港币</label>3.<input type="checkbox" checked="true"/><label class="mr10">歐元</label>4.<input type="checkbox"/><label class="mr10">日元</label>5.<input type="checkbox"/><label >其它:</label><span class="line_input"> </span><br/>
<#elseif data.remark33 == "04">
1.<input type="checkbox"/><label class="mr10">美元</label>2.<input type="checkbox"/><label class="mr10">港币</label>3.<input type="checkbox"/><label class="mr10">歐元</label>4.<input type="checkbox" checked="true"/><label class="mr10">日元</label>5.<input type="checkbox"/><label >其它:</label><span class="line_input"> </span><br/>
<#else>
1.<input type="checkbox"/><label class="mr10">美元</label>2.<input type="checkbox"/><label class="mr10">港币</label>3.<input type="checkbox"/><label class="mr10">歐元</label>4.<input type="checkbox"/><label class="mr10">日元</label>5.<input type="checkbox" checked="true"/><label >其它:</label><span class="line_input">${data.remark34}</span><br/>
</#if>
種類:
<#if data.remark35 == "01">
1.<input type="checkbox" checked="true"/><label class="mr10">經常項目外彙賬戶</label>2.<input type="checkbox"/><label class="mr10">外彙資本金賬戶</label>3.<input type="checkbox"/><label class="mr10">外債賬戶</label>4.<input type="checkbox"/><label >其它:</label><span class="line_input"> </span>
<#elseif data.remark35 == "02">
1.<input type="checkbox"/><label class="mr10">經常項目外彙賬戶</label>2.<input type="checkbox" checked="true"/><label class="mr10">外彙資本金賬戶</label>3.<input type="checkbox"/><label class="mr10">外債賬戶</label>4.<input type="checkbox"/><label >其它:</label><span class="line_input"> </span>
<#elseif data.remark35 == "03">
1.<input type="checkbox"/><label class="mr10">經常項目外彙賬戶</label>2.<input type="checkbox"/><label class="mr10">外彙資本金賬戶</label>3.<input type="checkbox" checked="true"/><label class="mr10">外債賬戶</label>4.<input type="checkbox"/><label >其它:</label><span class="line_input"> </span>
<#else>
1.<input type="checkbox"/><label class="mr10">經常項目外彙賬戶</label>2.<input type="checkbox"/><label class="mr10">外彙資本金賬戶</label>3.<input type="checkbox"/><label class="mr10">外債賬戶</label>4.<input type="checkbox" checked="true"/><label >其它:</label><span class="line_input">${data.remark36}</span>
</#if>
</td>
</tr>
</table>
<table cellspacing="0" cellpadding="4">
<tr>
<td colspan="3" class="table_title">賬 戶 信 息 2</td>
</tr>
<tr>
<td align="center">控股股東或<br/>實際控制人</td>
<td colspan="2">名稱:<span class="line_input mr10">${data.remark37}</span>
證件種類:
<#if data.remark38 == "01">
1.<input type="checkbox" checked="true"/><label class="mr10">身份證</label>2.<input type="checkbox"/><label class="mr10">護照</label>3.<input type="checkbox"/><label>其它:</label> <span class="line_input"> </span><br/>
<#elseif data.remark38 == "02">
1.<input type="checkbox"/><label class="mr10">身份證</label>2.<input type="checkbox" checked="true"/><label class="mr10">護照</label>3.<input type="checkbox"/><label>其它:</label> <span class="line_input"> </span><br/>
<#else>
1.<input type="checkbox"/><label class="mr10">身份證</label>2.<input type="checkbox"/><label class="mr10">護照</label>3.<input type="checkbox" checked="true"/><label>其它:</label> <span class="line_input">${data.remark39}</span><br/>
</#if>
證件号碼:<span class="line_input mr10">${data.remark40}</span>
證件到期日:
<#if data.remark41 == "01">
1.<input type="checkbox" checked="true"/><label class="mr10">長期有效</label>2.<input type="checkbox"/><label >到期日</label><span class="line_input"> </span>年<span class="line_input"> </span>月<span class="line_input"> </span>日</td>
<#else>
1.<input type="checkbox"/><label class="mr10">長期有效</label>2.<input type="checkbox" checked="true"/><label >到期日</label><span class="line_input">${data.remark42}</span>年<span class="line_input">${data.remark43}</span>月<span class="line_input">${data.remark44}</span>日</td>
</#if>
</tr>
<tr class="strong_area">
<td rowspan="2" align="center"><p style="color: red;text-align: center;">稅收居民身份<br/>資訊</p></td>
<td><p style="color: red">機構類别:
<#if data.remark45 == "01">
1.<input type="checkbox" checked="true"/><label class="mr10">消極非金融機構</label>2.<input type="checkbox"/>其他非金融機構</p></td>
<#else>
1.<input type="checkbox"/><label class="mr10">消極非金融機構</label>2.<input type="checkbox" checked="true"/>其他非金融機構</p></td>
</#if>
<td><p style="color: red">稅收居民身份:
<#if data.remark46 == "01">
1.<input type="checkbox" checked="true"/><label class="mr10">非居民或多重稅收身份</label>2.<input type="checkbox"/>僅中國稅收居民</p></td>
<#else>
1.<input type="checkbox"/><label class="mr10">非居民或多重稅收身份</label>2.<input type="checkbox" checked="true"/>僅中國稅收居民</p></td>
</#if>
</tr>
<tr class="strong_area">
<td colspan="2"><p style="color: red">注:屬于“消極非金融機構”或“非居民或多重稅收身份”的,需進一步提供稅收居民聲明檔案及相關補充資料。</p></td>
</tr>
<tr>
<td align="center">上級法人或主管機關</td>
<td colspan="2">名稱:<span class="line_input">${data.remark47}</span><br/>注:申請機關如有上級法人或主管機關,需提供其證照資料及法定代表人/機關負責人身份證件資料。</td>
</tr>
<tr class="strong_area">
<td align="center">網上/手機銀行/橙e網服務</td>
<td colspan="2">
<#if data.remark48 == "01">
<input type="checkbox" checked="true"/><label class="mr10">開通或加挂網上/手機銀行/橙e網,且網上銀行服務權限:</label>
<#else>
<input type="checkbox"/><label class="mr10">開通或加挂網上/手機銀行/橙e網,且網上銀行服務權限:</label>
</#if>
<#if data.remark49 == "01">
<input type="checkbox" checked="true"/><label class="mr10">轉賬與查詢(企業管理者可在網銀端自設錄入及複核使用者)</label>
<#else>
<input type="checkbox"/><label class="mr10">轉賬與查詢(企業管理者可在網銀端自設錄入及複核使用者)</label>
</#if>
或
<#if data.remark50 == "01">
<input type="checkbox" checked="true"/><label class="mr10">僅查詢;</label>
<#else>
<input type="checkbox"/><label class="mr10">僅查詢;</label>
</#if>
<br/><label class="mr10">接收網上/手機銀行激活碼的行動電話:</label>
<#if data.remark51 == "01">
<input type="checkbox" checked="true"/><label class="mr10">同法定代表人(機關負責人)電話</label><input type="checkbox"/><label>同财務負責人電話</label>
<#else>
<input type="checkbox"/><label class="mr10">同法定代表人(機關負責人)電話</label><input type="checkbox" checked="true"/><label>同财務負責人電話</label>
</#if>
<br/><b>USBKEY購買數量:<span class="line_input">${data.remark52}</span>個</b>
</td>
</tr>
<tr>
<td align="center">其它增值服務<br/>(可多選)</td>
<td colspan="2"><b class="mr10">1.預設開通電話銀行</b>
<#if data.remark53 == "01">
2.<input type="checkbox" checked="true"/><label>開通金衛士服務且簽約行動電話為:
<#if data.remark54??>
①</label><span class="line_input">${data.remark54}</span><br/>
<#if data.remark55??>
②<span class="line_input">${data.remark54}</span>
<#if data.remark56??>
③<span class="line_input">${data.remark56}</span>;
<#else>
③<span class="line_input"> </span>;
</#if>
<#else>
②<span class="line_input"> </span>③<span class="line_input"> </span>;
</#if>
<#else>
①</label><span class="line_input"> </span><br/>②<span class="line_input"> </span>③<span class="line_input"> </span>;
</#if>
<#else>
</#if>
繳費方式為:
<#if data.remark57 == "01">
<input type="checkbox" checked="true"/><label class="mr10">按月</label><input type="checkbox"/><label class="mr10">按條</label><input type="checkbox"/>按年<br/>
<#elseif data.remark57 == "02">
<input type="checkbox"/><label class="mr10">按月</label><input type="checkbox" checked="true"/><label class="mr10">按條</label><input type="checkbox"/>按年<br/>
<#else>
<input type="checkbox"/><label class="mr10">按月</label><input type="checkbox"/><label class="mr10">按條</label><input type="checkbox" checked="true"/>按年<br/>
</#if>
<#if data.remark58 == "01">
3.<input type="checkbox" checked="true"/><label class="mr10">開通通兌</label>
<#else>
3.<input type="checkbox"/><label class="mr10">開通通兌</label>
</#if>
<#if data.remark59 == "01">
4.<input type="checkbox" checked="true"/><label>開通支付密碼</label>
<#if data.remark60 == "01">
(支付密碼器:<input type="checkbox" checked="true"/><label class="mr10">新開</label><input type="checkbox"/><label class="mr10">自備)</label>
<#else>
(支付密碼器:<input type="checkbox"/><label class="mr10">新開</label><input type="checkbox" checked="true"/><label class="mr10">自備)</label>
</#if>
<#else>
4.<input type="checkbox"/><label>開通支付密碼</label>(支付密碼器:<input type="checkbox"/><label class="mr10">新開</label><input type="checkbox"/><label class="mr10">自備)</label>
</#if>
<#if data.remark61 == "01">
5.<input type="checkbox" checked="true"/><label>電子商業彙票簽約</label>
<#else>
5.<input type="checkbox"/><label>電子商業彙票簽約</label>
</#if>
</td>
</tr>
</table>
<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
<table cellspacing="0" cellpadding="4">
<col width="34%"/>
<col width="33%"/>
<col width="33%"/>
<tr>
<td colspan="3" class="table_title">意 見</td>
</tr>
<tr class="link">
<td align="center"><a href="#" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" >xxxx稽核意見</a></td>
<td align="center"><a href="#" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" >xxxx承諾及簽章</a></td>
<td align="center"><a href="#" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" >xxxx稽核意見</a></td>
</tr>
<tr>
<td valign="top" style="position: relative" class="strong_area">
<p style="text-indent: 24px">本機關因結算需要,申請在貴行開立賬戶,保證開戶資料的真實、完整、合規。本機關已閱讀、了解《平安銀行機關賬戶管理協定》條款(後附),并同意按協定約定使用賬戶和辦理業務。</p>
<p style="padding: 60px 0 80px">法定代表人<strong style="font-weight: normal">或其授權人</strong>簽章:<span class="line_input">${data.remark62}</span></p>
<div style="position: absolute;width: 100%;bottom: 4px;right: 4px">
<p style="padding: 20px 0 20px 0;">申請機關(公章)</p>
<p style="text-align: right;padding-bottom:15px;"><span class="line_input">${data.remark63}</span>年<span class="line_input">${data.remark64}</span>月<span class="line_input">${data.remark65}</span>日</p>
</div>
</td>
<td valign="top" style="position: relative">
<p style="margin: 20px 0;"> 經辦:<span class="line_input">${data.remark66}</span></p>
<p style="position: relative"> 主管:<span class="line_input">${data.remark67}</span></p>
<div style="position: relative;width: 100%;bottom: 4px;right: 4px">
<p style="padding: 115px 0 0 0;margin: 20px 0;">開戶銀行(簽章)</p>
<p style="text-align: right;padding-bottom:15px;"><span class="line_input">${data.remark68}</span>年<span class="line_input">${data.remark69}</span>月<span class="line_input">${data.remark70}</span>日</p>
</div>
</td>
<td valign="top" style="position: relative">
<p style="text-align: center">(非核準類賬戶除外)</p>
<p style="margin: 20px 0;"> 經辦人(簽章):<span class="line_input">${data.remark71}</span></p>
<div style="position: relative;width: 100%;bottom: 4px;right: 4px">
<br/><br/>
<p style="padding: 105px 0 0 0;">中國人民銀行(簽章)</p>
<br/>
<p style="text-align: right;padding-bottom:15px;"><span class="line_input">${data.remark72}</span>年<span class="line_input">${data.remark73}</span>月<span class="line_input">${data.remark74}</span>日</p>
</div>
</td>
</tr>
</table>
<div class="sign_area" style="width: 100%;">
<div style="width: 45%;text-align: right">
<b>客戶經理UM号:<span class="line_input">${data.remark75}</span></b>
</div>
<div style="text-align; right;margin-left: 60px">
<b>客戶經理姓名:<span class="line_input">${data.remark76}</span></b>
</div>
<p class="clear"></p>
</div>
</div>
<div class="side_tips">
<p>第</p>
<p>一</p>
<p>聯</p>
<p>:</p>
<p>開</p>
<p>戶</p>
<p>銀</p>
<p>行</p>
<p>留</p>
<p>存</p>
<p style="margin-top: 80px;">第</p>
<p>二</p>
<p>聯</p>
<p>:</p>
<p>申</p>
<p>請</p>
<p>單</p>
<p>位</p>
<p>留</p>
<p>存</p>
</div>
</div>
<!--文檔end-->
</body>
</html>
然後就是上測試代碼:
package com.zy.test;
import java.util.HashMap;
import java.util.Map;
import org.junit.Test;
import com.zy.common.templatebo.PDFTemplateBo;
import com.zy.common.utils.PDFUtil;
public class TestPDFUtil {
@Test
public void testGeneratePDF(){
String templateName = "template";//需要選用的模闆名稱
String targetPathRoot = "D:/temp/";//生成pdf的目标位置
Map<String, Object> map = generateUnitOpenAccountApplicationData();//構模組化闆所需資料
try {
String path = PDFUtil.getPath();
//System.out.println(path); //項目根路徑
PDFUtil.generateToFile(path,"templates/" + templateName + ".ftl", path + "/images/", map, targetPathRoot + templateName + ".pdf");
System.out.println("生成PDF成功!");
} catch (Exception e) {
e.printStackTrace();
System.out.println("生成PDF失敗!");
}
}
public Map<String, Object> generateUnitOpenAccountApplicationData() {
Map<String, Object>map = new HashMap<String, Object>();
PDFTemplateBo bo = new PDFTemplateBo();
bo.setRemark1("張三");//存款人名稱
bo.setRemark2("02");//辦公位址:01同注冊位址,02其它 當此項值為02時要設定remark3、remark4、remark5、remark6的值
bo.setRemark3("xxx");//具體辦公位址:省
bo.setRemark4("xxx");//具體辦公位址:市
bo.setRemark5("xxx");//具體辦公位址:區/縣
bo.setRemark6("xxx");//具體辦公位址:
bo.setRemark7("03");//郵寄位址:01同注冊位址,02同辦公位址,03其他 當此項值為03時要設定remark8、remark9、remark10、remark11的值
bo.setRemark8("xxx");//具體郵寄位址:省
bo.setRemark9("xxx");//具體郵寄位址:市
bo.setRemark10("xxx");//具體郵寄位址:區/縣
bo.setRemark11("xxx");//具體郵寄位址:
bo.setRemark12("01");//聯系資訊:01法定代表人,02機關負責人
bo.setRemark13("027");//辦公電話:區号
bo.setRemark14("88936");//辦公電話:直線
bo.setRemark15("985635");//辦公電話:分機
bo.setRemark16("13295684565");//辦公電話:行動電話
bo.setRemark17("賴總");//财務負責人:姓名
bo.setRemark18("010");//辦公電話:區 号
bo.setRemark19("88968");//辦公電話:直線
bo.setRemark20("9435649");//辦公電話:分機
bo.setRemark21("18868365686");//辦公電話:行動電話
bo.setRemark22("[email protected]");//電子郵箱
bo.setRemark23("羅總");//财務經辦人員:姓名
bo.setRemark24("020");//辦公電話:區 号
bo.setRemark25("89468");//辦公電話:直線
bo.setRemark26("9845146");//辦公電話:分機
bo.setRemark27("16686866868");//辦公電話:行動電話
bo.setRemark28("05");//賬戶種類:人民币:01定期賬戶,02基本戶,03一般戶,04臨時戶,05專使用者,06NRA賬戶 當此項值為03時要設定remark29的值 當此項為05時要設定remark31的值 當此項值為06時要設定remark32的值
//vo.setRemark29("03");//一般戶且用于:01結算,02貸款,03其它 當此項值為03時要設定remark30的值
//vo.setRemark30("預算");//具體的其他賬戶類型
bo.setRemark31("國有");//remark28值為05時要設定此項 專使用者且賬戶資金性質為
//vo.setRemark32("64987");//remark28值為06時要設定此項 NRA賬戶且國家代碼為
bo.setRemark33("05");//賬戶種類:外 币:币種:01美元,02港币,03歐元,04日元,05其他 當此項的值為05時要設定remark32的值
bo.setRemark34("英鎊");//具體的币種
bo.setRemark35("04");//賬戶種類:外 币:種類:01經常項目外彙賬戶,02外彙資本金賬戶,03外債賬戶,04其他 當此項值為04時要設定remark34的值
bo.setRemark36("具體的賬戶種類");//具體的種類
bo.setRemark37("老祝");//控股股東或實際控制人
bo.setRemark38("03");// 證件種類:01身份證,02護照,03其他 當此項值為03時要設定remark37的值
bo.setRemark39("好男人證");//具體的證件種類名稱
bo.setRemark40("86686688");//證件号碼
bo.setRemark41("02");//證件到期日類型:01長期有效,02具體到期日 當此項值為02時要設定remark40、remark41、remark42的值
bo.setRemark42("2080");//證件到期日 年
bo.setRemark43("08"); //證件到期日 月
bo.setRemark44("18"); //證件到期日 日
bo.setRemark45("02");//機構類别:01消極非金融機構,02其他非金融機構
bo.setRemark46("01");//稅收居民身份:01非居民或多重稅收身份,02僅中國稅收居民
bo.setRemark47("xxx");//上級法人或主管機關名稱
bo.setRemark48("01");//是否勾選 開通或加挂網上/手機銀行/橙e網,且網上銀行服務權限:01勾選,02不勾選
bo.setRemark49("02");//是否勾選 轉賬與查詢(企業管理者可在網銀端自設錄入及複核使用者):01勾選,02不勾選
bo.setRemark50("01");//是否勾選 僅查詢:01勾選,02不勾選
bo.setRemark51("02");//接收網上/手機銀行激活碼的行動電話:01同法定代表人(機關負責人)電話,02同财務負責人電話
bo.setRemark52("20");//USBKEY購買數量
bo.setRemark53("01");//其它增值服務:開通金衛士服務且簽約行動電話:01勾選,02不勾選 當此項值為01時要設定remark52的值
bo.setRemark54("13398595998");//電話号碼1
//vo.setRemark55("");//電話号碼2
//vo.setRemark56("");//電話号碼3
bo.setRemark57("02");//繳費方式:01按月,02按條,03按年
bo.setRemark58("01");//是否勾選 開通通兌:01勾選,02不勾選
bo.setRemark59("01");//是否勾選 開通支付密碼:01勾選,02不勾選 當此項的值為01時要設定remark58的值
bo.setRemark60("01");//支付密碼器類型:01新開,02自備
bo.setRemark61("01");//是否勾選 電子商業彙票簽約 :01勾選,02不勾選
bo.setRemark62("法定代表人");//法定代表人或其授權人簽章
bo.setRemark63("2018");//簽章日期 年
bo.setRemark64("03"); //簽章日期 月
bo.setRemark65("10"); //簽章日期 日
bo.setRemark66("經辦簽的字");//經辦簽字
bo.setRemark67("主管簽的字");//主管簽字
bo.setRemark68("2018");//簽字日期 年
bo.setRemark69("03"); //簽字日期 月
bo.setRemark70("10"); //簽字日期 日
bo.setRemark71("經辦人的簽章");//經辦人(簽章)
bo.setRemark72("2018");//簽章日期 年
bo.setRemark73("03");//簽章日期 月
bo.setRemark74("10");//簽章日期 日
bo.setRemark75("88888888");//客戶經理UM号
bo.setRemark76("老劉");//客戶經理姓名
map.put("data", bo);
return map;
}
}
截取部分生成的pdf: