micmiu – 软件开发+生活点滴
iText XML Worker修改源码完美支持HTML中文字符转PDF
作者: Michael 日期: 2012 年 11 月 26 日发表评论 (54)查看评论
目录:
- 介绍
- 中文字符转换
- 转换效果
[一]、介绍
XML Worker是辅助iText用于把XML、XHTML转换PDF的一个开源的软件工具库,详细介绍可以参见下面的链接:
- 官网地址:http://sourceforge.net/projects/xmlworker/
- 在线转换的Demo:http://demo.itextsupport.com/xmlworker/
- 在线文档:http://demo.itextsupport.com/xmlworker/itextdoc/index.html
- 在线文档:http://demo.itextsupport.com/xmlworker/itextdoc/flatsite.html
[二]、中文字符问题
如果HTML文件中包含中文字符,在转换成PDF是会无法显示,简单看了下它的源码,发现XML Worker无法设置默认字体,只好修改它的部分源码实现中文字符的转换。
附件提供修改后的lib包下载:xmlworker-1.2.1-micmiu.jar.zip (源码:https://github.com/micmiu/micmiu-xmlworker)
修改:com.itextpdf.tool.xml.css.apply.ChunkCssApplier.java 中的 public Chunk apply(final Chunk c, final Tag t) 这个方法,增加如下逻辑判断:
1 2 3 4 5 6 7 8 | Font f = applyFontStyles(t); // for chinese charater display @www.micmiu.com if (null != HTMLUtils.bfCN && HTMLUtils.isChinese(c.getContent())) { f = new Font(HTMLUtils.bfCN, f.getSize(), f.getStyle(), f.getColor()); } float size = f.getSize(); ...... |
在:com.itextpdf.tool.xml.html.HTMLUtils.java 中 增加下面用于中文字符判断的方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | public static BaseFont bfCN = null; static { try { bfCN = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); } catch (Exception e) { } }
// add by Michael more see:http://www.micmiu.com private static final boolean isChinese(char c) { Character.UnicodeBlock ub = Character.UnicodeBlock.of(c); if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) { return true; } return false; } // add by Michael more see:http://www.micmiu.com public static final boolean isChinese(String strName) { char[] ch = strName.toCharArray(); for (int i = 0; i < ch.length; i++) { char c = ch[i]; if (isChinese(c)) { return true; } } return false; } |
[三]、转换效果
演示一个简单的HTML文件:demo-cn.html
1 2 3 4 5 6 7 8 9 10 11 12 13 | <!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" dir="ltr" lang="zh-CN"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>iText xmlworker</title> </head> <body> <div> <p>[ Hello iText English ]</p> <p>[ 你好 iText 这是中文 ]</p> </div> </body> </html> |
转PDF的程序: Demo4HTMLCn.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | package com.micmiu.pdf.itext;
import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStreamReader;
import com.itextpdf.text.Document; import com.itextpdf.text.pdf.PdfWriter; import com.itextpdf.tool.xml.XMLWorkerHelper;
/** * 测试xml worker 页面包含中文字符的转换 * * @author <a href="http://www.micmiu.com">Michael Sun</a> */ public class Demo4HTMLCn {
/** * @param args */ public static void main(String[] args) throws Exception { String pdfFile = "d:/test/itext/demo-htmlcn.pdf"; String htmlFile = "d:/test/itext/demo-cn.html";
Document document = new Document(); PdfWriter pdfwriter = PdfWriter.getInstance(document, new FileOutputStream(pdfFile)); pdfwriter.setViewerPreferences(PdfWriter.HideToolbar); document.open();
// html文件 InputStreamReader isr = new InputStreamReader(new FileInputStream( htmlFile), "UTF-8"); XMLWorkerHelper.getInstance().parseXHtml(pdfwriter, document, isr); document.close();
}
} |
HTML页面、修改源码前 、修改源码后的效果对比图如下:
从上图可知,修改源码后,基本实现了对HTML的中文字符的支持。
本文介绍到此结束@Michael Sun.
原创文章,转载请注明: 转载自micmiu – 软件开发+生活点滴[ http://www.micmiu.com/ ]
本文链接地址: http://www.micmiu.com/opensource/expdoc/itext-xml-worker-cn/
MARK: 通过此博客将 xmlworker-5.5.6改造与 itextpdf-5.5.6完美配合,解决了以下异常
java.lang.NoSuchMethodError: com.itextpdf.text.pdf.PdfPTable.addCell(Lcom/itextpdf/text/pdf/PdfPCell;)V