html模板导出pdf文件

package com.crc.commonreport.util;

import java.awt.Insets;
import java.io.ByteArrayOutputStream;
import java.io.StringReader;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.zefer.pd4ml.PD4Constants;
import org.zefer.pd4ml.PD4ML;

import com.crc.accidentreport.util.WordUtil;

public class HtmlToPdf {
	private static final Log log = LogFactory.getLog(WordUtil.class);
	
	public static byte[] generatePDF(StringReader strReader) {
		try {
			ByteArrayOutputStream os = new ByteArrayOutputStream();
			PD4ML pd4ml = new PD4ML();
			pd4ml.setPageInsets(new Insets(5, 5, 5, 5));
			pd4ml.setHtmlWidth(900);
			pd4ml.setPageSize(PD4Constants.A4); // A4竖向显示
												// pd4ml.changePageOrientation(PD4Constants.A4)横向显示
			pd4ml.useTTF("java:fonts", true);
			pd4ml.setDefaultTTFs("SimHei", "YouYuan", "SimSun"); // fonts.jar的配置文件中的值,用于中文乱码
			pd4ml.enableDebugInfo();
			pd4ml.render(strReader, os); // 如果用上面的fos,就是写出到指定的文件
			return os.toByteArray();
		} catch (Exception e) {
			e.printStackTrace();
			log.error("HtmlToPdf(方法:generatePDF,异常错误信息代码:" + e.getClass().getName() + "###"+ e.getMessage());
		} 
		
		return null;

	}

	public static void main(String[] args) throws Exception {
		StringBuffer sb = new StringBuffer();
		sb.append("");
		sb.append("");
		sb.append("");
		sb.append("");
		sb.append("");
		sb.append("法律报告导出模板");
		sb.append("");
		sb.append("");
		sb.append("");
		sb.append("");
		sb.append("
"); sb.append("
"); sb.append("

华润集团2017年11月报告

"); sb.append("
"); sb.append("
"); sb.append("
"); sb.append("
"); sb.append("
"); sb.append("一、投资并购"); sb.append("
"); sb.append("
"); sb.append("1.当月首次参与的投资并购项目的数目:"); sb.append("
"); sb.append("
"); sb.append("


"); sb.append(""); sb.append(""); sb.append(""); sb.append(""); sb.append(""); sb.append(""); sb.append(""); sb.append(""); sb.append(""); sb.append(""); sb.append(" "); sb.append(" "); sb.append(""); sb.append(""); sb.append(""); sb.append(""); sb.append(""); sb.append(""); sb.append(""); sb.append(""); sb.append(""); sb.append(""); sb.append(""); sb.append(""); sb.append(""); sb.append(""); sb.append(""); sb.append(""); sb.append(""); sb.append(""); sb.append(""); sb.append("
55555
77777










"); sb.append("


"); sb.append("
"); sb.append("
"); sb.append("二、合同管理"); sb.append("
"); sb.append("
"); sb.append("1.合同管理制度性文件的制定、修订情况:"); sb.append("
"); sb.append("
"); sb.append("
"); sb.append("三、一级报告项"); sb.append("
"); sb.append("
"); sb.append("1.二级报告项01:"); sb.append("
"); sb.append("
"); sb.append("
"); sb.append("2.二级报告项02:"); sb.append("
"); sb.append("
"); sb.append("
"); sb.append("
"); sb.append("
"); sb.append("
"); sb.append(""); sb.append(""); StringReader strReader = new StringReader(sb.toString()); FileOperationUtil.writeFileFromByte(generatePDF(strReader), "D:\\test.pdf"); System.out.println(2); } }

需导入包:pd4ml_demo.jar ss_css2.jar fonts.jar

下载:http://download.csdn.net/download/wy240036141/10016357

你可能感兴趣的:(html模板导出pdf文件)