converting html file containing Chinese character

2019-08-29 15:48发布

问题:

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.



标签: java itext