根据HTML生成PDF_html/css_WEB-ITnose
程序员文章站
2022-06-01 09:54:20
...
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 |
|
依赖包 |
代码结构 |
推荐阅读
-
页面自动生成工具设计_html/css_WEB-ITnose
-
超好玩!10款神奇的字符图案 & 词汇云生成工具_html/css_WEB-ITnose
-
同一个表单,根据单击的按钮跳转到不同的页面_html/css_WEB-ITnose
-
网站网页生成.shtml访问无法显示_html/css_WEB-ITnose
-
#sphinx#生成html文档_html/css_WEB-ITnose
-
简洁的在线按钮css生成器_html/css_WEB-ITnose
-
JavaScript根据json生成html表格的示例代码
-
JavaScript根据json生成html表格的示例代码
-
jinja2模版生成页面时会产生大量空行和空格,如何移除?_html/css_WEB-ITnose
-
V4.0到来了,css雪碧图生成工具4.0更新啦_html/css_WEB-ITnose