2013-04-23 131 views
4

您好使用Wkhtmltopdf使用c#,Asp.net从Html页面创建PDF。 PDF创建顺利,但是当我将图像添加到HTML时,它不会显示在PDF上。使用WKHTMLTOPDF以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" /> 

+0

生成的HTML文件显示为我所需要的。但图像不以PDF格式显示。请帮忙。 – 2013-04-23 13:23:40

回答

2

尝试添加完整路径,图片src。这通常是由于“../TempLogo/chafa.jpg”实际上并不是wkhtmltopdf工作目录的正确相对URL。因此,而不是“../TempLogo/chafa.jpg”尝试“C:/yourpath/TempLogo/chafa.jpg”或“file:/// C:/yourpath/TempLogo/chafa.jpg”,而不是“C:/yourpath/TempLogo/chafa.jpg”看看是否有帮助。如果是这样,你的相对路径是问题。

+0

我会尝试并让你知道。 – 2013-04-24 01:33:19

+0

我已经尝试过但它没有帮助。请帮我解决这个问题。 – 2013-04-24 04:38:57

+0

我使用过“file:/// E:/AMS/AMS/AMS.WEB/TempLogo/chafa.jpg”&“src =”http:// localhost:5822/AMS.WEB/TempLogo/chafa.jpg“我也尝试将版本从0.11更改为0.9,但它也不成功 – 2013-04-24 04:58:45

1

使用pdfkit与红宝石,我发现,如果我发送带有STDIN图像的HTML文件到wkhtmltopdf图像不呈现。 (即wkhtmltopdf - toto.pdf:没有图像)

如果我手工通过一个html文件(wkhtmltopdf toto.html toto.html)执行EXACT相同的命令,这个工程。

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

+0

嘿,你能显示toto.html吗?我真的很想复制这个,因为除了路径问题之外,我从来没有遇到任何问题。 – Nenotlep 2013-09-30 07:01:50

+0

我认为这可能是一个路径问题,现在我重新思考它 - 你的HTML是否有相对路径?因为在非stdin转换过程中可能会从输入文件读取工作目录。 – Nenotlep 2014-12-08 10:20:53

0

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

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

0

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

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

我有这个问题,对我来说“修复”是去除高度属性。

而不是

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

尝试

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

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