【问题标题】:Path issue with wkhtmltopdf.exe to convert HTML file to PDFwkhtmltopdf.exe 将 HTML 文件转换为 PDF 的路径问题
【发布时间】:2012-07-05 17:41:37
【问题描述】:

我正在使用wkhtmltopdf 将 HTML 文件转换为链接按钮上的 PDF 文档 http://code.google.com/p/wkhtmltopdf/

当用户单击链接按钮时,它会运行以下代码,如下所示的代码在传递文件路径中作为参数 ProcessStartInfo。此代码仅在以下场景中工作正常

考虑到网站托管在域http://www.xyz.net/

  1. 当我提到路径为http://demo.XYZ.net/ 它工作正常
  2. 当我提到路径为 http://www.XYZ.net/ 它不起作用
  3. 在本地主机的情况下,如果路径是http://localhost:51005/XYZ/http://web:8080/,它可以正常工作

为了使其正常工作,我们需要为网站提供完全信任级别&我不确定为什么代码无法运行如果我创建 put PrintArticle.aspx 则给它相同的域路径如果我创建一个子域然后它会正常工作。我不确定这是安全问题还是什么

代码如下

protected void lnkbtnDownload_Click(object sender, EventArgs e)
{
    //ConvertURLToPDF();
    try
    {
        string url = "PrintArticle.aspx?articleID=" + Request["articleID"] + "&download=yes&Language=" + Request["Language"];

        //string args = string.Format("\"{0}\" - ", "http://demo.XYZ.net/" + url); //Works
        //string args = string.Format("\"{0}\" - ", "http://www.xyz.net/" + url); Doesnt work
        //string args = string.Format("\"{0}\" - ", url);

        string args = string.Format("\"{0}\" - ", "http://localhost:51005/XYZ/" + url); //Works

        var startInfo = new ProcessStartInfo(Server.MapPath("bin\\wkhtmltopdf.exe"), args)
        {
            UseShellExecute = false,
            CreateNoWindow = true,
            RedirectStandardOutput = true
        };
        var proc = new Process { StartInfo = startInfo };
        proc.Start();

        string output = proc.StandardOutput.ReadToEnd();
        byte[] buffer = proc.StandardOutput.CurrentEncoding.GetBytes(output);
        proc.WaitForExit();
        proc.Close();
        Response.ContentType = "application/pdf";
        Response.AddHeader("Content-Disposition", "attachment;filename=download.pdf");
        Response.BinaryWrite(buffer);
        Response.End();
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

如果文件在同一个域中的错误消息

“/”应用程序中的服务器错误。找不到资源。

描述:HTTP 404。您正在寻找的资源(或其之一 依赖项)可能已被删除,名称已更改,或者是 暂时不可用。请查看以下 URL 并确保 它拼写正确。 请求的 URL:/PrintArticle.aspx

版本信息:Microsoft .NET Framework 版本:4.0.30319; ASP.NET 版本:4.0.30319.272

【问题讨论】:

    标签: asp.net c#-4.0 pdf-generation webforms wkhtmltopdf


    【解决方案1】:

    我使用以下语句解决了这个问题

    var url = Request.Url.GetLeftPart(UriPartial.Authority) + "/PrintArticle.aspx?articleID=" + Request["articleID"] + "&download=yes&Language=" + Request["Language"];
    

    现在它工作正常我不确定当我指定文件路径时它不起作用。

    【讨论】:

      【解决方案2】:

      输出变量包含空字符串 我的代码如下: 尝试 { 字符串 url=Request.Url.GetLeftPart(UriPartial.Authority) +"/PrintQuickPrescription.aspx?DoctorId=" + DoctorID + "&DispnID=" + DispnID + "&ApptID=" + ApptID + "&PatientID=" + PatientID; System.Diagnostics.Process 进程 = new System.Diagnostics.Process();

              string args = string.Format("\"{0}\" - ", "http://localhost:50013/DPMNewWeb/"+url);
              //string  args="http://localhost:50013/DPMNewWeb/PrintQuickPrescription.aspx";
      
              var startInfo = new ProcessStartInfo(Server.MapPath("~\\Bin\\wkhtmltopdf.exe"), args)
              {
                  UseShellExecute = false,
                  CreateNoWindow = true,
                  RedirectStandardOutput = true
              };
              var proc = new Process { StartInfo = startInfo };
              proc.Start();
      
              string output = proc.StandardOutput.ReadToEnd();
      
              byte[] buffer = proc.StandardOutput.CurrentEncoding.GetBytes(output);
              proc.WaitForExit();
              proc.Close();
              Response.ContentType = "application/pdf";
              Response.BinaryWrite(buffer);
              Response.End();
      
      
      
              //byte[] fileContent = GeneratePDFFile();
              //GeneratePDFFile();
              //if (fileContent != null)
              //{
              //    Response.Clear();
              //    Response.ContentType = "application/pdf";
              //    Response.AddHeader("content-length", fileContent.Length.ToString());
              //    Response.BinaryWrite(fileContent);
              //    Response.End();
              //}
          }
          catch
          {
          }
      

      【讨论】:

        猜你喜欢
        • 2023-04-04
        • 2021-02-15
        • 2018-06-03
        • 2013-07-10
        • 1970-01-01
        • 2016-12-30
        • 2013-08-01
        相关资源
        最近更新 更多