【问题标题】:Flying Saucer ignores embedded font when generating PDF飞碟在生成 PDF 时忽略嵌入的字体
【发布时间】:2018-03-22 16:21:07
【问题描述】:

我尝试使用 Flying Saucer 为以下 HTML 生成 PDF:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <style type="text/css">
  @font-face {
  font-family: 'Roboto';
  font-style: normal;
  font-weight: 300;
  src:url(data:application/font-woff;charset=utf-8;base64,d09GRgABAAAAAGSwABMAAAAAtfwAAQAAAAA...zubbzBiN9B2+6bK8AAAABV9JwXgAA) format('woff');
  -fs-pdf-font-embed: embed;
  -fs-pdf-font-encoding: Identity-H;
}

    body {
      font-family: Roboto;
      font-size: 26px;
      font-weight: 300;
      letter-spacing: -.03em;
    }
  </style>
</head>
<body>
<p>The quick brown fox jumps over the lazy dog</p>
</body>
</html>

base64 字体取自https://gist.github.com/abelaska/9c9eda70d31315f27a564be2ee490cf4

        ITextRenderer renderer = new ITextRenderer();
        renderer.setDocumentFromString(data);
        renderer.layout();
        renderer.createPDF(os);

当我从 Adob​​e Reader 检查属性中使用的字体时,列出的是 Times New Roman 而不是上面的字体。

如果我在 css 中使用字体的路径,PDF 会正确显示字体。

src:url(/usr/local/Roboto.woff)

有人可以告诉我我缺少什么,或者这是飞碟限制吗?

【问题讨论】:

    标签: pdf itext flying-saucer


    【解决方案1】:

    Flying saucer 在后台使用 iText(但不是最新版本)。 在 iText7 中解决了很多字体和 HTML 相关问题,这实际上是我们最初迁移到 iText7 的很大一部分原因。

    试试 pdfHTML。它是一个 iText7 附加组件,可让您将 HTML5 (+CSS3) 转换为 PDF。它是 AGPL 许可和开源的。 (或者更确切地说,我们目前正在开源它)。

    https://itextpdf.com/itext7/pdfHTML

    这是一些示例代码:

    // load license
    LicenseKey.loadLicenseFile("path_to_license_key.xml");
    
    // conversion
    HtmlConverter.convertToPdf(
        "<b>This text should be written in bold.</b>", 
        new PdfWriter(new File("C://users/user5733033/output.pdf"))
    );
    

    【讨论】:

    • 我期待 Flying Saucer 解决方案,因为我们在项目中一直使用它。
    • 我当然不是说这是解决您问题的方法。我只是在解决根本问题。
    猜你喜欢
    • 2014-01-03
    • 2016-06-26
    • 2013-10-08
    • 2017-11-25
    • 1970-01-01
    • 2014-04-04
    • 2011-11-23
    • 2021-03-25
    • 1970-01-01
    相关资源
    最近更新 更多