时间:2021-07-01 10:21:17 帮助过:46人阅读
FreeMarkerUtil |
package cn.danny;import java.io.IOException;import java.io.StringWriter;import java.io.Writer;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Locale;import java.util.Map;import freemarker.template.Configuration;import freemarker.template.DefaultObjectWrapper;import freemarker.template.Template;import freemarker.template.TemplateException;public class FreeMarkerUtil { public String createDefineTargetTemplate() throws Exception { Configuration conf = new Configuration(); try { conf.setClassForTemplateLoading(this.getClass(), "templates"); } catch (Exception e) { throw new Exception("加载模板异常!" + e.getMessage()); } conf.setObjectWrapper(new DefaultObjectWrapper()); conf.setLocale(Locale.CHINA); conf.setDefaultEncoding("utf-8"); conf.setClassicCompatible(true);// 处理空值为空字符串 String targetStr = null; Map root = new HashMap(); List |
Test |
package cn.danny;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileOutputStream;import java.io.OutputStream;import org.xhtmlrenderer.pdf.ITextFontResolver;import org.xhtmlrenderer.pdf.ITextRenderer;import com.lowagie.text.pdf.BaseFont;public class Test { public static void main(String[] args) throws Exception { String outputFile = "D:\\workspaces\\cnbs\\pdfDemo\\firstdoc.pdf"; OutputStream os = new FileOutputStream(outputFile); String htmlStr = new FreeMarkerUtil().createDefineTargetTemplate(); createPdfFileFromHtmlContent(htmlStr, os); os.close(); System.out.println("Done"); } public static ByteArrayOutputStream createPdfFromHtml(String htmlTemplateUrl) throws Exception { ByteArrayOutputStream bos = new ByteArrayOutputStream(); ITextRenderer renderer = new ITextRenderer(); ITextFontResolver fontResolver = renderer.getFontResolver(); fontResolver.addFont("C:/Windows/Fonts/arialuni.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); File file = new File(htmlTemplateUrl); String url = file.toURI().toURL().toString(); renderer.setDocument(url); renderer.layout(); renderer.createPDF(bos); bos.close(); return bos; } public static ByteArrayOutputStream createPdfFromHtmlContent(String htmlContent) throws Exception { ByteArrayOutputStream bos = new ByteArrayOutputStream(); ITextRenderer renderer = new ITextRenderer(); ITextFontResolver fontResolver = renderer.getFontResolver(); fontResolver.addFont("C:/Windows/Fonts/arialuni.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); renderer.setDocumentFromString(htmlContent); renderer.layout(); renderer.createPDF(bos); bos.close(); return bos; } public static void createPdfFileFromHtmlContent(String htmlContent, OutputStream os) throws Exception { ITextRenderer renderer = new ITextRenderer(); ITextFontResolver fontResolver = renderer.getFontResolver(); fontResolver.addFont("C:/Windows/Fonts/simsun.ttc", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); /*File file = new File("D:\\workspaces\\cnbs\\pdfDemo\\src\\cn\\danny\\templates\\first.html"); renderer.setDocument(file);*/ renderer.setDocumentFromString(htmlContent); renderer.layout(); renderer.createPDF(os); os.close(); } } |
test.ftl |
|
依赖包 |
代码结构 |