【问题标题】:Image display in HTML but not in PDF using WKHTMLTOPDF使用 WKHTMLTOPDF 以 HTML 格式而不是 PDF 格式显示图像
【发布时间】:2013-04-23 13:22:22
【问题描述】:

您好,我正在使用 Wkhtmltopdf 使用 c#、Asp.net 从 Html 页面创建 PDF。 PDF 可以顺利创建,但是当我将图像添加到 HTML 时,它不会显示在 PDF 上。

     public static string HtmlToPdf(string pdfOutputLocation, string outputFilenamePrefix, string[] urls,
        string[] options = null,
        // string pdfHtmlToPdfExePath = "C:\\Program Files (x86)\\wkhtmltopdf\\wkhtmltopdf.exe")

       string pdfHtmlToPdfExePath = "C:\\Program Files\\wkhtmltopdf\\wkhtmltopdf.exe")
    {
        string urlsSeparatedBySpaces = string.Empty;
        try
        {
            //Determine inputs
            if ((urls == null) || (urls.Length == 0))
                throw new Exception("No input URLs provided for HtmlToPdf");
            else
                urlsSeparatedBySpaces = String.Join(" ", urls); //Concatenate URLs

            string outputFolder = pdfOutputLocation;
            string outputFilename = outputFilenamePrefix + "_" + DateTime.Now.ToString("yyyy-MM-dd-hh-mm-ss-fff") + ".PDF"; // assemble destination PDF file name

            var p = new System.Diagnostics.Process()
            {
                StartInfo =
                {
                    FileName = pdfHtmlToPdfExePath,
                    Arguments = ((options == null) ? "" : String.Join(" ", options)) + " " + urlsSeparatedBySpaces + " " + outputFilename,
                    UseShellExecute = false, // needs to be false in order to redirect output
                    RedirectStandardOutput = true,
                    RedirectStandardError = true,
                    RedirectStandardInput = true, // redirect all 3, as it should be all 3 or none
                    WorkingDirectory = HttpContext.Current.Server.MapPath(outputFolder),
                    CreateNoWindow = true
                }
            };

            p.Start();

            // read the output here...
            var output = p.StandardOutput.ReadToEnd();
            var errorOutput = p.StandardError.ReadToEnd();

            // ...then wait n milliseconds for exit (as after exit, it can't read the output)
            p.WaitForExit(60000);

            // read the exit code, close process
            int returnCode = p.ExitCode;
            p.Close();

            // if 0 or 2, it worked so return path of pdf
            if ((returnCode == 0) || (returnCode == 2))
                return outputFolder + outputFilename;
            else
                throw new Exception(errorOutput);
        }
        catch (Exception exc)
        {
            throw new Exception("Problem generating PDF from HTML, URLs: " + urlsSeparatedBySpaces + ", outputFilename: " + outputFilenamePrefix, exc);
        }
    }

这是我显示图像的 HTML 代码区域..

<div id="Image" class="right" style="background:#333;height:15%;width:25%;">

  <img id="LogoImage" runat="server" width="100%" height="100%" src="../TempLogo/chafa.jpg" />

【问题讨论】:

  • 根据需要显示生成的 HTML 文件。但图像未以 PDF 显示。请帮忙。

标签: c# asp.net c#-4.0 wkhtmltopdf


【解决方案1】:

尝试为图像 src 添加完整路径。这通常是因为“../TempLogo/chafa.jpg”实际上不是 wkhtmltopdf 工作目录的正确相对 URL。

因此,请尝试“C:/yourpath/TempLogo/chafa.jpg”或“file:///C:/yourpath/TempLogo/chafa.jpg”而不是“../TempLogo/chafa.jpg”并查看如果这有帮助。如果是这样,那么您的相对路径就是问题所在。

【讨论】:

  • 我会尝试并告诉你。
  • 我试过了,但没有帮助。请帮我解决这个问题。
  • 我也使用过 "file:///E:/​​AMS/AMS/AMS.WEB/TempLogo/chafa.jpg" & "src="localhost:5822/AMS.WEB/TempLogo/chafa.jpg" 但它不起作用。我也尝试将版本从 0.11 更改为 0.9,但也没有成功
  • 该死。我会试着想点别的。
  • file:///E:/AMS/AMS/AMS.WEB/TempLogo/chafa.jpg 为我工作。使用absolutUri,HTML 仅呈现absolutUri
【解决方案2】:

我遇到了这个问题,对我来说,“修复”是删除 height 属性。

代替

&lt;img id="Logo" runat="server" width="100%" height="100%" src="../TempLogo/chafa.jpg" /&gt;

试试

&lt;img id="Logo" runat="server" width="100%" src="../TempLogo/chafa.jpg" /&gt;

我没有对此行为的解释,它可能是一个错误。

【讨论】:

  • 这也为我修复了它。更新到 0.12.5 似乎已经修复了这个错误,让我可以毫无问题地同时使用宽度和高度
  • 这为我“解决”了这个问题。我正在运行 0.12.5 但仍然不能同时使用高度和宽度
【解决方案3】:

将 pdfkit 与 ruby​​ 一起使用,我发现如果我将带有 STDIN 上的图像的 html 文件发送到 wkhtmltopdf,则图像不会呈现。 (即 wkhtmltopdf - toto.pdf :没有图像)

如果我通过手动传递一个 html 文件 (wkhtmltopdf toto.html toto.html) 执行完全相同的命令,这将有效。

不知道为什么,但是,我只是写了一个小包装器来这样称呼它,就是这样。

【讨论】:

  • 嘿,你能显示 toto.html 吗?我真的很想复制这个,因为除了路径问题之外,我从来没有遇到过问题。
  • 我认为这也可能是一个路径问题,现在我重新考虑它 - 你的 HTML 是否有相对路径?因为在非标准输入转换期间可能会从输入文件中读取工作目录。
【解决方案4】:

我使用视图来存储我想要的每个 pdf 模板的 HTML 标记,我将模型传递给视图。使用 Server.MapPath() 可以获得图像的完整路径。

<img src="@Server.MapPath("~/path/to/your/image.jpg")" />

【讨论】:

    【解决方案5】:

    我最近看到一个 jpeg 文件转换失败 - 问题是图像是灰度图像。

    请注意,所有灰度图像不一定都是灰度图像 - 如果是这种情况,请使用类似 irfanview 的程序进行检查。

    【讨论】:

      猜你喜欢
      • 2011-09-23
      • 2014-04-07
      • 2017-09-12
      • 1970-01-01
      • 2021-06-24
      • 1970-01-01
      • 2013-02-15
      • 2011-11-09
      • 2015-09-02
      相关资源
      最近更新 更多