我有一个包含中文字符的html文件。我想将HTML文件转换为PDF文件。一切都转好,但是汉字显示出问题。 代码如下
HTMl文件 -
<html>
<head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
chinese---快得利-协议重组贷款
</body>
</html>
Java文件-----
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();
}
}
输出 - &GT; 中国----
答案 0 :(得分:0)
该字体是否支持中文字符?您可以尝试使用iTextAsian库。另请参阅this thread。
答案 1 :(得分:0)
bitstream veranda sans确实:
http://weblog.delacour.net/archives/2005/04/bitstream_vera_not_for_me_thanks.php