本文介绍了使用iText将包含汉字的html文件转换为pdf文件.汉字转换不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个包含汉字的html文件.我想将HTML文件转换为PDF文件.一切都转换良好,但汉字显示出问题.代码如下
I have an html file which containing Chinese character. I want to convert HTML file to PDF file. Everything is converting well but Chinese character it showing problem.code are following
HTMl文件-
<html>
<head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
chinese---快得利-协议重组贷款
</body>
</html>
Java文件-----
Java file-----
package bancmate.reports.otherreports.engreport;
import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.Watermark;
import com.lowagie.text.html.simpleparser.HTMLWorker;
import com.lowagie.text.html.simpleparser.StyleSheet;
import com.lowagie.text.pdf.PdfWriter;
//import com.lowagie.text.pdf.codec.Base64;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;
public class html2pdf {
public static void main(String[] args) throws Exception {
Document pdfDocument = new Document();
Reader htmlreader = new BufferedReader(new InputStreamReader(
new FileInputStream("D:\\Support\\LatestSupport\\CUSTOEMR.html"),"UTF-8"));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PdfWriter.getInstance(pdfDocument, baos);
pdfDocument.clearTextWrap();
pdfDocument.open();
StyleSheet styles = new StyleSheet();
styles.loadTagStyle("body", "font", "Bitstream Vera Sans");
ArrayList arrayElementList = HTMLWorker.parseToList(htmlreader, styles);
for (int i = 0; i < arrayElementList.size(); ++i) {
Element e = (Element) arrayElementList.get(i);
pdfDocument.add(e);
}
pdfDocument.close();
byte[] bs = baos.toByteArray();
// String pdfBase64 = Base64.encodeBytes(bs); //output
File pdfFile = new File("D:\\Support\\LatestSupport\\pdfExample.pdf");
FileOutputStream out = new FileOutputStream(pdfFile.toString());
out.write(bs);
out.close();
}
}
输出->中文----
output->chinese----
推荐答案
您可以尝试 iTextAsian 库.另请参见此线程.
You could try the iTextAsian library. Also see this thread.
这篇关于使用iText将包含汉字的html文件转换为pdf文件.汉字转换不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!