最近项目中要把相关数据导出为PDF文件,总结下自己写这个功能遇到的种种问题
先给出个大概的框架
package com.xsm;
import java.util.HashMap;
import java.util.Map;
import org.junit.Test;
import com.sectechio.fintech.common.utils.PDFUtil;
public class PDFtest {
@Test
public void createPDF(){
String path = "D:/test";
String name = "test.html";
String content = "'\r\n<html>\r\n\r\n<head>\r\n<meta http-equiv=Content-Type content=\"text/html; charset=gb2312\"></meta>\r\n<meta name=Generator content=\"Microsoft Word 15 (filtered)\"></meta>\r\n\r\n\r\n</head>\r\n\r\n<body style=\'text-justify-trim:punctuation\'>\r\n\r\n${
bankName}年利率是:${
interest}%\r\n\r\n</body>\r\n\r\n</html>\r\n'";
Map<String, Object> paraMap = new HashMap<String, Object>();
paraMap.put("bankName", "小人国银行");
paraMap.put("interest", 36);
PDFUtil pdfUtil = new PDFUtil(path,name);
pdfUtil.createdHtmlTemplate(content);
try {
String uploadfile = pdfUtil.fillTemplate(paraMap);
System.out.println(uploadfile);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}