一 簡介
因公司業務需求需要做一個通過模版導出PDF的功能,模版是word文檔,模版中包含圖片,文字,水印,分頁。遇到這種沒做過的功能,沒什麼可說的,直接問度娘。結果度娘給不了我想要的,只好去github上下了一些相關的項目,通過篩選和實驗終於找到了不錯的代碼,但是在使用過程中也是踩了無數坑,隨記錄下來。
二 思路
通過模版導出PDF的方法有很多,但是思路都大致相同,總結起來就是 1 渲染模版 2 導出模版。以爲之前使用過freemarker,所以我這裏渲染模版的技術用的是freemarker,然後在使用itextpdf將渲染好的模版導出。
三 踩過的坑
1 Win下中文不顯示 2 圖片不顯示 3 Linux下中文不顯示 4 圖片樣式跑偏 5 分頁加水印
四 上代碼
4.1 準備工作
4.11首先準備好一個word模版,如下圖所示
4.1.2 將word模版通過wps或其他工具另存爲 轉爲HTML模版.
轉爲html後 ${}會被 分開,通過在線格式化工具排版HTML,將${}之間多餘的代碼刪掉,重新恢復到${},批量操作即可.
4.1.3 新建一個ftl文件,將HTML代碼拷貝進去 如下圖所示
4.1.4 maven座標
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.23</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.4.2</version>
</dependency>
<dependency>
<groupId>com.itextpdf.tool</groupId>
<artifactId>xmlworker</artifactId>
<version>5.4.1</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
<dependency>
<groupId>org.xhtmlrenderer</groupId>
<artifactId>flying-saucer-pdf</artifactId>
<version>9.0.3</version>
</dependency>
4.2 寫代碼 新建一個PDF工具類
4.2.1 第一步 渲染模版 代碼如下
//獲取模板並填充數據
public static String getContent(String fileName,Object data) throws Exception{
// 創建一個Configuration對象
Configuration configuration = new Configuration(Configuration.getVersion());
Configuration config = new Configuration(Configuration.getVersion());//FreeMarker配置
// 告訴config對象模板文件存放的路徑。
configuration.setDirectoryForTemplateLoading(new File(PdfUtil.class.getClassLoader().getResource("templates/").getPath()));
// 設置config的默認字符集。一般是utf-8
configuration.setDefaultEncoding("utf-8");
//從config對象中獲得模板對象。需要制定一個模板文件的名字。
Template template = configuration.getTemplate(fileName+".ftl");
StringWriter writer = new StringWriter();
//模版和數據匹配
template.process(data, writer);
writer.flush();
String html = writer.toString();
return getImgs(html);
}
4.2.2 第二步 替換模版中的圖片路徑 代碼如下
//替換html中的圖片路徑
private static String getImgs(String content) {
String img = "";
Pattern p_image;
Matcher m_image;
String str = "";
String[] images = null;
String regEx_img = "(<img.*src\\s*=\\s*(.*?)[^>]*?>)";
p_image = Pattern.compile(regEx_img, Pattern.CASE_INSENSITIVE);
m_image = p_image.matcher(content);
if(m_image!=null){
while (m_image.find()) {
img = m_image.group();
Matcher m = Pattern.compile("src\\s*=\\s*\"?(.*?)(\"|>|\\s+)")
.matcher(img);
if(m!=null){
while (m.find()) {
String tempSelected = m.group(1);
str = tempSelected;
String classpath= PathUtil.getRootPath();
String ulr=classpath.substring(5,classpath.length());
content=content.replace(str,ulr+"templates/"+str);
}
}
}
}
return content;
}
工具類 PathUtil
/**
* @author:change
* @description:獲取絕對路徑
* @date:2018/6/28 0028
*/
public class PathUtil {
static String path1;
static String download;
public static String getRootPath() {
String line = File.separator;
String path = Thread.currentThread().getContextClassLoader().getResource("").toString();
//windows下
if ("\\".equals(line)) {
/* path = path.replace("/", "\\"); // 將/換成\\*/
path1 = path;
}
//linux下
if ("/".equals(line)) {
path = path.replace("\\", "/");
path1 = path;
}
return path1;
}
public static void main(String[] args) {
System.out.println(getRootPath());
System.out.println(PdfUtil.class.getClassLoader().getResource("").getPath());
}
}
4.2.3 設置pdf字符集 代碼如下
/**
* 設置字符集
*/
public static class MyFontsProvider extends XMLWorkerFontProvider {
public MyFontsProvider(){
super(null, null);
}
@Override
public Font getFont(final String fontname, String encoding, float size, final int style) {
String fntnames = fontname;
Font FontChinese = null;
if (fntnames == null) {
fntnames = "宋體";
}
if (size == 0) {
size = 4;
}
try{
BaseFont bfChinese = BaseFont.createFont("STSong-Light",
"UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
FontChinese = new Font(bfChinese, 12, Font.NORMAL);
}catch (Exception e){
e.printStackTrace();
}
if(FontChinese==null){
FontChinese = super.getFont(fntnames, encoding, size, style);
}
return FontChinese;
}
}
4.2.4 生產PDF並下載 代碼如下
/**
* 生成PDF到輸出流中(ServletOutputStream用於下載PDF)
* @param templateName ftl模板名稱
* @param data 輸入到FTL中的數據
* @param response HttpServletResponse
* @return
*/
public static OutputStream exportToResponse(String templateName, Object data,
HttpServletResponse response)throws Exception{
TEMPLATENAMES=templateName;
String html= getContent(templateName,data);
response.reset();
response.setContentType("application/pdf;charset=utf-8");
response.setHeader("Content-Disposition", "attachment;filename="+ new String((templateName + ".pdf").getBytes(), "iso-8859-1"));
try{
OutputStream out = null;
ITextRenderer render = null;
out = response.getOutputStream();
//設置文檔大小
Document document = new Document(PageSize.A4);
PdfWriter writer = PdfWriter.getInstance(document, out);
writer.setStrictImageSequence(true);
//輸出爲PDF文件
convertToPDF(writer,document,html);
return out;
}catch (Exception ex){
System.out.println(ex);
throw new SimpleException("PDF export to response fail");
}
}
/**
* @description PDF文件生成
*/
private static void convertToPDF(PdfWriter writer,Document document,String htmlString)throws Exception{
document.open();
MyFontsProvider fontProvider = new MyFontsProvider();
fontProvider.addFontSubstitute("lowagie", "garamond");
fontProvider.setUseUnicode(true);
CssAppliers cssAppliers = new CssAppliersImpl(fontProvider);
HtmlPipelineContext htmlContext = new HtmlPipelineContext(cssAppliers);
htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());
XMLWorkerHelper.getInstance().getDefaultCssResolver(true);
if(TEMPLATENAMES.equals("合同")||TEMPLATENAMES.equals("合同模版"))
writer.setPageEvent(new BackGroundImage());
if(TEMPLATENAMES.equals("設計合同"))
writer.setPageEvent(new SheJiGroundImage()); //這裏初始化分頁監聽類
try {
XMLWorkerHelper.getInstance().parseXHtml(writer,document,
new ByteArrayInputStream(htmlString.getBytes("UTF-8")),
XMLWorkerHelper.class.getResourceAsStream("/default.css"),
Charset.forName("UTF-8"),fontProvider);
} catch (IOException e) {
e.printStackTrace();
throw new SimpleException("PDF文件生成異常");
}finally {
document.close();
}
}
4.2.5 設置水印和分業 新建一個類繼承pdfPageEventHelper ,該類用來監聽pdf分頁 代碼如下
package com.rxjy.commons.pdf;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.PdfPageEventHelper;
import com.itextpdf.text.pdf.PdfWriter;
import com.rxjy.constructionContract.myutils.PathUtil;
import java.io.IOException;
/**
* @author:change
* @description:監聽分頁
* @date:2018/7/2 0002
*/
public class BackGroundImage extends PdfPageEventHelper {
String classpath= PathUtil.getRootPath();
String ulr=classpath.substring(5,classpath.length());
@Override
public void onEndPage(PdfWriter writer, Document document) {
try {
if(document.getPageNumber()==1){ //第一頁
Image image = Image.getInstance(ulr+"templates/合同.files/合同39.png"); //甲方
Image image1 = Image.getInstance(ulr+"templates/合同.files/合同242.png"); //乙方
/* 設置圖片的大小 */
image.scaleAbsolute(128, 128);
image1.scaleAbsolute(128, 128);
/* 設置圖片的位置 */
image.setAbsolutePosition(70,600);
image1.setAbsolutePosition(300,600);
document.add(image);
document.add(image1);
//二維碼
Image code = Image.getInstance(ulr+"templates/合同.files/合同二維碼.png");
code.scaleAbsolute(50,50);
code.setAbsolutePosition(500,750);
document.add(code);
}
if(document.getPageNumber()==4){ //第四頁
Image image = Image.getInstance(ulr+"templates/合同.files/合同39.png"); //甲方
Image image1 = Image.getInstance(ulr+"templates/合同.files/合同242.png"); //乙方
/* 設置圖片的大小 */
image.scaleAbsolute(128, 128);
image1.scaleAbsolute(128, 128);
/* 設置圖片的位置 */
image.setAbsolutePosition(70,350);
image1.setAbsolutePosition(300,350);
document.add(image);
document.add(image1);
}
//分頁水印
Image yejiao = Image.getInstance(ulr+"templates/合同.files/合同"+document.getPageNumber()+".png");
yejiao.scaleAbsolute(144,44);
yejiao.setAbsolutePosition(450,30);
document.add(yejiao);
} catch (BadElementException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
}
}
}
4.2.6 調用PDF工具類 代碼如下
@ApiOperation("導出合同")
@GetMapping("/importPdfByTemplates")
void importPdfByTemplates(HttpServletResponse response,String rwdid,String templateName)throws Exception{
Map<String,Object> map=heTongService.htInfoMap(rwdid); //該數據的key對應模版中的${}中的字段
PdfUtil.exportToResponse(templateName,map,response);
}
五 測試
導出的PDF若遇到樣式上的細微問題。可以在ftl中使用html代碼調整 諮詢相關問題請加羣.