itext2.0.8把html转换为pdf及解决中文不显示办法

对html文件格式要求比较严格,请遵循以下格式:

<!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 mce_bogus="1" type="text/css">
     body {font-family: SimSun; background:none;margin-left: auto;margin-right: auto;}
</style>
  </head>
  <body>
  </body>
</html>

注:

为了解决中文问题,请在body中加入样式 font-family: SimSun; 同时,对应JAVA代码中文处理类如下:

/**
 * 
 * @ClassName: Html2Pdf
 * @Description: Html文件转换为(这里用一句话描述这个类的作用)
 * @author lxz
 * @date 2016-4-18 下午4:08:41
 * 
 */
public class Html2PdfUtil {

	public synchronized static boolean html2Pdf(String inputFile, String outputFile) {

		OutputStream os = null;
		File htmlFile = new File(inputFile);
		try {
			os = new FileOutputStream(outputFile);

			ITextRenderer renderer = new ITextRenderer();
			String url = null;
			try {
				url = htmlFile.toURI().toURL().toString();
			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			renderer.setDocument(url);
			// 解决中文支持问题
			ITextFontResolver fontResolver = renderer.getFontResolver();
			try {
				fontResolver.addFont("c://windows//fonts//simsun.ttc,1",BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
			} catch (DocumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			// 解决图片的相对路径问题
			renderer.getSharedContext().setBaseURL("file:/" + basePath + "/print");
			renderer.layout();
			try {
				renderer.createPDF(os);
			} catch (DocumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return false;
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				os.flush();
				os.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
                //删除原html文件
		htmlFile.delete();
		return true;
	}
}

其中 " c://windows//fonts//simsun.ttc,1使用Windows系统字体,和html页面中font-family: SimSun; 对应。

1、解决中文问题 2、附字体 3、动态html拼接pdf public static void htmlCodeComeString(String linkcss,String htmlCode, String outputFile,String title) throws Exception { OutputStream os = new FileOutputStream(outputFile); ITextRenderer renderer = new ITextRenderer(); renderer.setDocumentFromString(getConversionHtmlCode(linkcss,htmlCode,title)); ITextFontResolver fontResolver = renderer.getFontResolver(); URL fontPath = ItextUtil.class.getResource("simsun.ttc"); fontResolver.addFont(fontPath.toString(), BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); // 解决图片的相对路径问题 // renderer.getSharedContext().setBaseURL("file:/F:/teste/html/"); renderer.layout(); renderer.createPDF(os); System.out.println("======转换成功!"); os.close(); os.flush(); } public static void main(String[] args) { ItextUtil itextUtil = new ItextUtil(); String html = ""; html += ""; html += "企业信息"; html += " "; html += " "; html += " 登记日期"; html += " 2006-04-28"; html += " "; html += " "; html += " 纳税人编号"; html += " HSJIHKS002"; html += " "; html += " "; html += " 有效标志"; html += " Y"; html += " "; html += " "; html += " 社会信用代码"; html += " 916101317H"; html += " "; html += " "; html += " 评估机关代码"; html += " 盛世"; html += " "; html += " "; html += " 工商注销日期"; html += " 2006-04-28"; html += " "; html += " "; html += ""; String outputFile = "D:\\pdf\\aa.pdf"; try { itextUtil.htmlCodeComeString("",html,outputFile,""); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("生成结束!!!"); }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值