pdf - html 到 pdf 转换,西里尔字符无法正确显示

标签 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 上测试过。对于编码或字体有什么建议吗?

提前致谢

最佳答案

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

关于pdf - html 到 pdf 转换,西里尔字符无法正确显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30889217/

相关文章:

java - 当我以 pdf 形式向占位符插入值时,如何设置 iText 的编码?

python - 如何使用公钥加密?

c# - StreamReader 问题 - 未知文件编码(西方 ISO 88591)

android - 在 Android 中解码乌尔都语代码

css - 为什么我的字体文本看起来如此粗体?

javascript - 使用 jsPDF 从动态内容生成 PDF 时遇到问题

java - 文本居中,不考虑 x 坐标

html - 适合 A4 尺寸的 html 表格

java - Jasper 报 Font not found 异常

用于打开包含文本、图像和格式的 PDF 的 Java 库?