converting html file containing Chinese character to pdf file using iText. Chinese character not converting properly

无人久伴 提交于 2020-01-17 02:39:45

问题


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 file--

<html>
<head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
chinese---快得利-协议重组贷款
</body>
</html>

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----


回答1:


bitstream veranda sans does:

http://weblog.delacour.net/archives/2005/04/bitstream_vera_not_for_me_thanks.php




回答2:


Does that font support Chinese characters? You could try the iTextAsian library. Also see this thread.



来源:https://stackoverflow.com/questions/8462797/converting-html-file-containing-chinese-character-to-pdf-file-using-itext-chine

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!