Home > Web Front-end > HTML Tutorial > 根据HTML生成PDF_html/css_WEB-ITnose

根据HTML生成PDF_html/css_WEB-ITnose

WBOY
Release: 2016-06-21 08:52:32
Original
1311 people have browsed it


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);	}}
Copy after login
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();	}	}
Copy after login
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>
Copy after login
依赖包

代码结构


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template