当前位置:Gxlcms > html代码 > 根据HTML生成PDF_html/css_WEB-ITnose

根据HTML生成PDF_html/css_WEB-ITnose

时间: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 clauses = new ArrayList();		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
          模板                                <#list clauses as clause>        	<#if clause.clauseCode = "D31001">        	

主标题

This is my Demo

This is my Demo

段标题 其他内容

<#if clause.clauseCode = "D31002">

段标题

其他内容啦啦啦

依赖包

代码结构

人气教程排行