html 转pdf,西里尔字符显示不正确

html to pdf convert, cyrillic characters not displayed properly

我对 pdf 字体有疑问。我使用了一种从 html 生成 pdf 的方法,该方法在我的 windows OS 本地机器上运行良好,但现在在 linux 上显示带有问号的西里尔文文本。我在那里检查了字体,但结果发现有必需的字体。现在我切换到另一种方法,如下所示。

    Document document = new Document(PageSize.A4);
    String myFontsDir = "C:\";
    String filePath = AppProperties.downloadLocation + "Order_" + orderID + ".pdf";
    try {
        OutputStream file = new FileOutputStream(new File(filePath));
        PdfWriter writer = PdfWriter.getInstance(document, file);
        int iResult = FontFactory.registerDirectory(myFontsDir);
        if (iResult == 0) {
            System.out.println("TestPDF(): Could not register font directory " + myFontsDir);
        } else {
            System.out.println("TestPDF(): Registered font directory " + myFontsDir);
        }

        document.open();
        String htmlContent = "<html><head>"
                + "<meta http-equiv=\"content-type\" content=\"application/xhtml+xml; charset=UTF-8\"/>"
                + "</head>"
                + "<body>"
                + "<h4 style=\"font-family: arialuni, arial; font-size:16px; font-weight: normal; \" >"
                + "Здраво Kristijan!"
                + "</h4></body></html>";
        InputStream inf = new ByteArrayInputStream(htmlContent.getBytes("UTF-8"));

        XMLWorkerFontProvider fontImp = new XMLWorkerFontProvider(myFontsDir);
        FontFactory.setFontImp(fontImp);
        XMLWorkerHelper.getInstance().parseXHtml(writer, document, inf, null, null, fontImp);

        document.close();
        System.out.println("Done.");
    } catch (Exception e) {
        e.printStackTrace();
    }

通过这种安静的代码,我能够从拉丁文本生成正确的 pdf,但西里尔字母显示时带有奇怪的字符。这发生在 Windows,我还没有在 Linux 上测试过。对编码或字体有什么建议吗?

提前致谢

首先:很难相信您的字体目录是C:\。您假设您有一个路径为 C:\arialuni.ttf 的文件,而我假设 MS Arial Unicode 的路径是 C:\windows\fonts\arialuni.ttf.

其次:我不认为 arialuni 是正确的名字。我很确定它是 arial unicode ms。您可以通过 运行 此代码进行检查:

XMLWorkerFontProvider fontProvider = new XMLWorkerFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS);
fontProvider.register("c:/windows/fonts/arialuni.ttf");
for (String s : fontProvider.getRegisteredFamilies()) {
    System.out.println(s);
}

输出应该是:

courier
arial unicode ms
zapfdingbats
symbol
helvetica
times
times-roman

这些是您可以使用的值; arialuni 不是其中之一。

另外:你是不是在错误的地方定义了字符集?

我稍微调整了你的源代码,因为我将 HTML 存储在 HTML 文件中 cyrillic.html:

<html>
<head>
<meta http-equiv="content-type" content="application/xhtml+xml; charset=UTF-8"/>
</head>
<body>
<h4 style="font-family: Arial Unicode MS, FreeSans; font-size:16px; font-weight: normal; " >Здраво Kristijan!</h4>
</body>
</html>

请注意,我将 arialuni 替换为 Arial Unicode MS,并且我使用 FreeSans 作为替代字体。在我的代码中,我使用 FreeSans.ttf 而不是 arialttf.

ParseHtml11:

public static final String DEST = "results/xmlworker/cyrillic.pdf";
public static final String HTML = "resources/xml/cyrillic.html";
public static final String FONT = "resources/fonts/FreeSans.ttf";

public void createPdf(String file) throws IOException, DocumentException {
    // step 1
    Document document = new Document();
    // step 2
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
    // step 3
    document.open();
    // step 4
    XMLWorkerFontProvider fontImp = new XMLWorkerFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS);
    fontImp.register(FONT);
    FontFactory.setFontImp(fontImp);
    XMLWorkerHelper.getInstance().parseXHtml(writer, document,
            new FileInputStream(HTML), null, Charset.forName("UTF-8"), fontImp);
    // step 5
    document.close();
}

如您所见,我在解析 HTML 时使用了 Charset。结果如下所示:

如果您坚持使用 Arial Unicode,只需替换此行:

public static final String FONT = "resources/fonts/FreeSans.ttf";

有了这个:

public static final String FONT = "c:/windows/fonts/arialuni.ttf";

我已经在 Windows 机器上测试过了,它也能正常工作: