itext用html方式导出pdf
来源:互联网 发布:港股模拟软件 编辑:程序博客网 时间:2024/05/19 03:17
依赖:
<dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> </dependency> <dependency> <groupId>org.xhtmlrenderer</groupId> <artifactId>flying-saucer-pdf</artifactId> <version>9.0.9</version> </dependency> <dependency> <groupId>com.itextpdf</groupId> <artifactId>itextpdf</artifactId> <version>5.5.9</version> </dependency>
代码:
public class TestIText { public static void main(String[] args) throws IOException, TemplateException, DocumentException { String htmlStr = getHtmlContent("bbc.ftl", new Object()); try (OutputStream os = new FileOutputStream(new File(UUID.randomUUID().toString()));) { createPDF("/static/simhei.ttf", htmlStr, os); } System.out.println(htmlStr.toString()); } private static void createPDF(String fontPath, String htmlStr, OutputStream os) throws IOException, DocumentException { ITextRenderer renderer = new ITextRenderer(); ITextFontResolver iTextFontResolver = renderer.getFontResolver(); iTextFontResolver.addFont( fontPath, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED ); renderer.setDocumentFromString(htmlStr); renderer.layout(); renderer.createPDF(os); } private static String getHtmlContent(String name, Object o) throws IOException, TemplateException { Template template = getTemplate(name); StringWriter sw = new StringWriter(); template.process(o, sw); return sw.toString(); } private static Template getTemplate(String name) throws IOException { Configuration config = new Configuration(); config.setClassForTemplateLoading(TestIText.class, "/static"); return config.getTemplate(name); }}别忘了自己弄个ttf字体和ftl模板。
阅读全文
0 0
- itext用html方式导出pdf
- Itext 导出PDF(中文解决,HTML转PDF)
- 用itext导出pdf文件,包含中文
- itext导出pdf
- itext导出pdf
- iText导出word、pdf
- iText 导出pdf
- itext导出pdf
- itext实现pdf导出
- iText实现pdf导出
- itext导出pdf
- 导出pdf之--itext
- Itext 导出pdf文件
- itext将html字符串以pdf格式导出
- struts2 + freemark + itext 导出pdf(基于模板方式)
- itext html to pdf
- itext html 生成pdf
- iText html转pdf
- jdk8 Metaspace 调优
- JAVA虚拟机内存结构精简要义(干货)
- 关于Servlet找不到路径的问题(Servlet HTTP Status 404 )
- 2017中国青岛国际食品博览会 青岛国际餐饮食材暨火锅食材展览会会刊(参展商名录)
- 兼容拖动
- itext用html方式导出pdf
- ElasticSearch vs Solr多维度分析对比
- Android中扫描多媒体文件操作详解
- 最全iOS数据存储方法介绍:FMDB,SQLite3 ,Core Data,Plist,Preference偏好设置,NSKeyedArchiver归档
- < 笔记 > JavaScript
- JavaScript中错误正确处理方式,你用对了吗?
- ssh加密的两种方式
- 交互式 Bash Shell 获取进程 pid
- OS学习笔记六:文件系统