根据HTML生成PDF_html/css_WEB-ITnose

php中文网
发布: 2016-06-21 08:52:32
原创
1421人浏览过


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<ClauseTemplateBean> clauses = new ArrayList<ClauseTemplateBean>();		ClauseTemplateBean bean1 = new ClauseTemplateBean();		bean1.setClauseCode("D31001");		ClauseTemplateBean bean2 = new ClauseTemplateBean();		bean2.setClauseCode("D31002");		clauses.add(bean1);		clauses.add(bean2);		root.put("clauses", clauses);		try {			targetStr = generateHtmlFromFtl(root, "test.ftl", conf);		} catch (Exception e) {			throw e;		}		return targetStr;	}	public static String generateHtmlFromFtl(Object root, String tplName,			Configuration conf) throws IOException, TemplateException {		Template temp = conf.getTemplate(tplName);		Writer out = new StringWriter();		temp.process(root, out);		return out.toString();	}	public static void main(String args[]) throws Exception {		String html = new FreeMarkerUtil().createDefineTargetTemplate();		System.out.println(html);	}}
登录后复制
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
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">  <head>        <title>模板</title>        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />        <style type="text/css"  mce_bogus="1">        body {        	font-family : 'SimSun'        }        </style>    </head>    <body>        <#list clauses as clause>        	<#if clause.clauseCode = "D31001">        	<h2><span style="font-weight: bolder;">主标题</span></h2>        	<h2><b>This is my Demo</b></h2>        	<h2>This is my Demo</h2>        	<p>        		<span style="font-weight: bolder;">段标题</span>        		其他内容        	</p>        	</#if>        	<#if clause.clauseCode = "D31002">        	<h2>段标题</h2>        	<p>        		其他内容啦啦啦        	</p>        	</#if>        </#list>    </body></html>
登录后复制
依赖包

代码结构

uBrand Logo生成器
uBrand Logo生成器

uBrand Logo生成器是一款强大的AI智能LOGO设计工具。

uBrand Logo生成器 57
查看详情 uBrand Logo生成器

立即学习前端免费学习笔记(深入)”;


WPS零基础入门到精通全套教程!
WPS零基础入门到精通全套教程!

全网最新最细最实用WPS零基础入门到精通全套教程!带你真正掌握WPS办公! 内含Excel基础操作、函数设计、数据透视表等

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号