【问题标题】:iText7 for .NET does not include images in the conversion from HTML to PDF.NET 的 iText7 在从 HTML 到 PDF 的转换中不包括图像
【发布时间】:2020-10-02 19:38:23
【问题描述】:

我有这个 HTML:

<table style="width:100%" class="table table-bordered"><tbody><tr><td><p><img src="/User/Images/logo-48_20200612183312_78003.jpg" style="width: 64px;"></p></td><td><p>Development, Systems &amp; Technologies</p><p>Santiago</p></td></tr></tbody></table>

该 HTML 仅包含生成的 PDF 文件中的文本,绕过图像。

这是我用来转换该段落的代码:

    private IList<IElement> ConvertHtmlToElements(string html)
    {
        ConverterProperties properties = new ConverterProperties();
        properties.SetBaseUri(HttpContext.Current.Server.MapPath("/"));

        return HtmlConverter.ConvertToElements(html, properties);
    }

我正在使用 iText7 7.1.11 和 iText7.pdfhtml 3.0.0。

我还应该告诉您,使用 iText7 7.1.2 和 iText7.pdfhtml 2.0.2 使用相同的图像,同样的代码可以完美运行。

这些版本之间发生了什么变化?

当然图片可以使用https://server/User/Images/logo-48_20200612183312_78003.jpg访问

詹姆

【问题讨论】:

  • 嗨 Jaime,您在日志中有任何警告/错误吗?您可以使用github.com/net-commons/common-logging 的说明配置日志记录
  • @AlexeySubach 我发现了问题所在。当使用以反斜杠结尾的物理路径并且 IMG SRC 属性以 / 开头时调用 properties.SetBaseUri,图像不会在 PDF 中呈现。为了解决这个问题,我对 HTML 使用了这个调用:html = html.Replace("img src=\"/", "img src=\"");.... 我真的不知道为什么如果我只删除物理路径末尾的最后一个反斜杠,它也不起作用。我不得不将 / 删除到 IMG SRC 路径的开头。也许是因为这是 Window 并且物理路径使用反斜杠,而 IMG SRC 使用斜杠。

标签: itext7 pdfhtml


【解决方案1】:

最后,事实证明,在 iText7 的最新版本(iText7 7.1.11 和 iText7.pdfhtml 3.0.0)中,IMG 标记的 SRC 属性不应以斜杠开头,这一点很重要。

【讨论】:

    猜你喜欢
    • 2018-04-07
    • 1970-01-01
    • 1970-01-01
    • 2021-03-23
    • 2018-08-13
    • 1970-01-01
    • 2019-01-01
    • 2015-04-19
    相关资源
    最近更新 更多