html到pdf转换,西里尔字符没有正确显示

时间:2015-06-17 10:42:36

标签: pdf encoding fonts itext

我的pdf字体有问题。我已经使用了一种从html生成pdf的方法,这种方法在我的本地机器上工作正常,这是Windows操作系统,但现在在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上测试它。有关编码或字体的建议吗?

提前致谢

1 个答案:

答案 0 :(得分:4)

首先:很难相信你的字体目录是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。结果如下:

enter image description here

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

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

有了这个:

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

我在Windows机器上对此进行了测试,它也有效:

enter image description here