将HTML转换为PDF时出现字符问题

时间:2019-04-15 06:11:30

标签: java html pdf

我无法从html的PDF中获取特殊字符。我尝试使用UTF-8阅读,Windows-1257,ISO-8859-13等。但是没有任何效果,相反,我只能得到空格。

所以问题是如何解决?

JAVA

    String d1 = "<html><head></head><body>...ą...č...ę...ė...į...š...ų...ū...ž...Ą...Č...Ę...Ė...Į...Š...Ų...Ū...Ž...</body></html>";


    OutputStream myFile = new FileOutputStream(new File("C:\\My\\pdf1.pdf"));
    Document document = new Document();

    document.addCreationDate();
    document.setPageSize(PageSize.A4);
    document.setMargins(36, 36, 36, 36);
    document.setMarginMirroring(true);


    PdfWriter writer = PdfWriter.getInstance(document, myFile);
    document.open();

    XMLWorkerHelper worker = XMLWorkerHelper.getInstance();

    InputStream is;
    //is = new ByteArrayInputStream(d1.getBytes(StandardCharsets.UTF_8));
    is = new ByteArrayInputStream(d1.getBytes("UTF-8"));

    String FONT = "C:\\My\\FreeSans.ttf";
    XMLWorkerFontProvider fontImp = new XMLWorkerFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS);
    fontImp.register(FONT);

    worker.parseXHtml(writer, document, is, Charset.forName("UTF-8"), fontImp);

    document.close();
    myFile.close();

1 个答案:

答案 0 :(得分:0)

Topaco对,在我添加字符串“ body style ...”之后,一切正常!