java ftl转pdf_FreeMarker(二):利用FreeMarker转pdf

1. 设计需求

订票成功后,生成一个pdf 文件,用户打印入场票,门票大致如下所示:

78b3f8fdb5bad830eeaaae8f062b5bb8.png

2. 设计思想

1). 使用FreeMarker 转换 ftl(就是FreeMarker 魔板文件),生成HTML

2). 使用jtidy库将 HTML转换成 对格式要求严格的 XHTML

3 ) . 使用Itext 库将XHTML转换成PDF

3. Demo 创建

简介:本项目是在Maven 的环境中搭建的,其pom.xml 主要导入的依赖如下:

org.freemarker

freemarker

2.3.28

com.itextpdf.tool

xmlworker

5.5.6

net.sf.jtidy

jtidy

r938

3.1 FreeMarker 魔板转换成pdf 源码如下:

public class FreeMarkToHtml {

/**

* 通过路径 和文件名称后去魔板

*

* @param templateFilePath

* @param templateFileName

* @return

*/

public static Template getTemplate(String templateFilePath, String templateFileName) {

try {

Configuration cfg = new Configuration();

cfg.setClassicCompatible(true);

TemplateLoader templateLoader = new FileTemplateLoader(new File(

templateFilePath));

cfg.setTemplateLoader(templateLoader);

return cfg.getTemplate(templateFileName);

} catch (IOException e) {

e.printStackTrace();

}

return null;

}

/**

* @param templateFilePath

* @param templateFileName 加载的模板文件名

* @param replaceData

* @param outFile 生成指定文件

* @return 成功,返回文件名;失败,返回null。

*/

public static String freemarkToHtml(String templateFilePath, String templateFileName, Map replaceData,

String outFile) {

String path = null;

Writer out = null;

try {

out = new BufferedWriter(new OutputStreamWriter(

new FileOutputStream(outFile), "UTF-8"));

Template temp = getTemplate(templateFilePath, templateFileName);

temp.setEncoding("UTF-8");

temp.process(replaceData, out);

path = outFile;

} catch (IOException e) {

e.printStackTrace();

path = null;

} catch (TemplateException e) {

e.printStackTrace();

path = null;

} finally {

try {

if (out != null)

out.close();

} catch (IOException e) {

e.printStackTrace();

}

}

return path;

}

}

3.1 使用jtidy库将 HTML转换成 对格式要求严格的 XHTML源码

/**

* 将html 转换成为严格的XHTML

*/

public class Html2Xhtml {

/**

* 转化类

*

* @param html html文件输入路径(带文件名称)

* @param xhtml xhtml文件输入路径(带文件名称)

* @return

*/

public static String html2Xhtml(String html, String xhtml) {

String path = null;

try {

FileInputStream fin = new FileInputStream(html);

ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();

int data = -1;

while ((data = fin.read()) != -1) {

byteArrayOut.write(data);

}

fin.close();

byte[] htmlFileData = byteArrayOut.toByteArray();

byteArrayOut.close();

ByteArrayInputStream tidyInput = new ByteArrayInputStream(

htmlFileData);

ByteArrayOutputStream tidyOut = new ByteArrayOutputStream();

Tidy tidy = new Tidy();

tidy.setInputEncoding("UTF-8");

tidy.setOutputEncoding("UTF-8");

tidy.setShowWarnings(false);

tidy.setIndentContent(true);

tidy.setSmartIndent(true);

tidy.setIndentAttributes(false);

tidy.setMakeClean(true);

tidy.setQuiet(true);

tidy.setWord2000(true);

tidy.setXHTML(true);

tidy.setErrout(new PrintWriter(System.out));

tidy.parse(tidyInput, tidyOut);

tidyInput.close();

tidyOut.writeTo(new FileOutputStream(xhtml));

tidyOut.flush();

tidyOut.close();

path = xhtml;

} catch (FileNotFoundException e) {

e.printStackTrace();

path = null;

} catch (IOException e) {

e.printStackTrace();

path = null;

}

return path;

}

}

3.3 使用Itext 库将XHTML转换成PDF

/**

* @Description: xhtml --> pdf

* @Author: liupenghao

* @Date: 2018/4/17 ❤❤ 11:16

*/

public class XHtml2Pdf {

/**

* 转化方法

*

* @param html html文件输入路径(带文件名称)

* @param pdf pdf文件输出路径(带文件名称)

*/

public static void XHtml2Pdf(String html, String pdf)

throws FileNotFoundException, IOException, DocumentException,

CssResolverException {

int i = html.lastIndexOf(".html");

String xhtml = null;

xhtml = html.substring(0, i) + ".xhtml";

xhtml = Html2Xhtml.html2Xhtml(html, xhtml);

if (xhtml != null) {

Document document = new Document();

PdfWriter writer = PdfWriter.getInstance(document,

new FileOutputStream(pdf));

document.open();

FontProvider fontProvider = new FontProvider();

fontProvider.addFontSubstitute("lowagie", "garamond");

fontProvider.setUseUnicode(true);

// 使用我们的字体提供器,并将其设置为unicode字体样式

CssAppliers cssAppliers = new CssAppliersImpl(fontProvider);

HtmlPipelineContext htmlContext = new HtmlPipelineContext(

cssAppliers);

htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());

CSSResolver cssResolver = XMLWorkerHelper.getInstance()

.getDefaultCssResolver(true);

Pipeline> pipeline = new CssResolverPipeline(cssResolver,

new HtmlPipeline(htmlContext, new PdfWriterPipeline(

document, writer)));

XMLWorker worker = new XMLWorker(pipeline, true);

XMLParser p = new XMLParser(worker);

File input = new File(xhtml);

p.parse(new InputStreamReader(new FileInputStream(input), "UTF-8"));

document.close();

}

}

}

3.4 启动类编写

/**

* 描述:

*

* @author liupenghao

* @create 2018-04-17 9:58

**/

public class ApplicationStart {

public static void main(String[] args) {

URL url = Thread.currentThread().getContextClassLoader().getResource("");

Map replaceData = initData();

FreeMarkToHtml.freemarkToHtml(url.getPath(), "template.ftl", replaceData, url.getPath() + "result.html");

try {

XHtml2Pdf.XHtml2Pdf(url.getPath() + "result.html", url.getPath() + "result.pdf");

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} catch (DocumentException e) {

e.printStackTrace();

} catch (CssResolverException e) {

e.printStackTrace();

}

}

public static HashMap initData() {

HashMap needReplaceMapData = new HashMap();

/******************************************单个属性************************************/

needReplaceMapData.put("name", "刘亦菲");

/******************************************list 属性添加***********************************/

List students = new ArrayList();

for (int i = 0; i < 2; i++) {

Student student = new Student();

student.setId(i);

student.setName(" 葫芦娃" + i + "号");

students.add(student);

}

needReplaceMapData.put("students", students);

/******************************************日期 ***********************************/

needReplaceMapData.put("date", new Date());

needReplaceMapData.put("showDate", true);

needReplaceMapData.put("imgUrl", "http://n.sinaimg.cn/translate/344/w700h1244/20180410/B_F_-fyzeyqa1740187.jpg");

needReplaceMapData.put("description", "描述 一只猫");

return needReplaceMapData;

}

}

3.5 中文乱码的解决

/**

* @Description: 字体提供类, 更改 默认的字体

* @Author: liupenghao

* @Date: 2018/4/17 ❤❤ 11:19

*/

public class FontProvider extends XMLWorkerFontProvider {

public FontProvider() {

super(null, null);

}

@Override

public Font getFont(final String fontname, String encoding, float size,

final int style) {

String fntname = fontname;

if (fntname == null) {

fntname = "宋体";

}

return super.getFont(fntname, encoding, size, style);

}

}

4. 项目源码:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值