[使用docx4j转换docx-> pdf时如何更改字体编码?

问题描述 投票:2回答:3

[当我将docx文档转换为pdf时,我的国家字符转换为“#”标记。有什么方法可以设置pdf文档的字体编码?

过去我使用xdocreport,它可以处理此问题,但是在图像,页眉和页脚方面存在问题。

Docx4j可以做到这一点,但是字体却不能。转换后,字体具有ANSI编码,而我想要Windows-1250。有设置选项吗?

java pdf docx docx4j xdocreport
3个回答
3
投票

我的问题是-在Linux服务器上缺少正确的True Type字体。而是插入默认字体(没有我的代码页)。

我解决了通过安装默认的Windows Ms字体的问题ttf-mscorefonts-installer

关于debian:

apt-get install ttf-mscorefonts-installer

2
投票

我遇到了同样的问题,发现您自己提到了字体问题。系统上的字体需要支持您的编码。

例如:对于使用“ Arial”字体的文档,德语变音符号显示为“?”。

我找到了另一种解决方案,以覆盖如下所示的PDF字体编码:

    //
    // read template
    //
    File docxFile = new File(System.getProperty("user.dir") + "/" + "Test.docx");
    InputStream in = new FileInputStream(docxFile);

    // 
    // prepare document context
    //
    IXDocReport report = XDocReportRegistry.getRegistry().loadReport(in, TemplateEngineKind.Velocity);
    IContext context = report.createContext();
    context.put("name", "Michael Küfner");

    // 
    // generate PDF output
    //
    Options options = Options.getTo(ConverterTypeTo.PDF).via(ConverterTypeVia.XWPF);
    PdfOptions pdfOptions = PdfOptions.create();
    pdfOptions.fontEncoding("iso-8859-15");
    options.subOptions(pdfOptions);     


    OutputStream out = new FileOutputStream(new File(docxFile.getPath() + ".pdf"));
    report.convert(context, options, out);

尝试根据需要设置pdfOptions.fontEndcoding中的属性(在我的情况下为“ iso-8859-15”。

将其设置为“ UTF-8”(默认情况下,使用特殊字符会导致相同的问题。

我发现的另一件事:

使用“ Calibri”字体(这是Word 2007/2010的默认字体,即使使用UTF-8编码,也不会出现此问题。也许iText中用于生成PDF的嵌入式Type-1 Arial字体不支持UTF-8编码。


0
投票

如果您使用docker,这会有所帮助https://github.com/captnswing/msttcorefonts

© www.soinside.com 2019 - 2024. All rights reserved.