C# wkhtmltopdf输出到生成0字节文件的html响应

C# wkhtmltopdf输出到生成0字节文件的html响应,c#,asp.net,wkhtmltopdf,C#,Asp.net,Wkhtmltopdf,我在服务器上使用wkhtmltopdf.exe将一些基于.aspx页面的报告转换为pdf,然后下载到客户端计算机。经过广泛的研究,我发现了一个例子,似乎得到了我想要完成的。我试图使它适应我自己的使用,但我无法使它工作。来自页面的响应是一个.pdf文件,但长度为0字节。我一直在研究将呈现的aspx页面转换为.pdf的解决方案,这已经有将近一天半的时间了,但运气不好——这个解决方案是我遇到的最接近的解决方案,我想我只是缺少了一些简单的东西,它会起作用 请参阅下面的代码-我将感谢您提供的任何指导,使这

我在服务器上使用wkhtmltopdf.exe将一些基于.aspx页面的报告转换为pdf,然后下载到客户端计算机。经过广泛的研究,我发现了一个例子,似乎得到了我想要完成的。我试图使它适应我自己的使用,但我无法使它工作。来自页面的响应是一个.pdf文件,但长度为0字节。我一直在研究将呈现的aspx页面转换为.pdf的解决方案,这已经有将近一天半的时间了,但运气不好——这个解决方案是我遇到的最接近的解决方案,我想我只是缺少了一些简单的东西,它会起作用

请参阅下面的代码-我将感谢您提供的任何指导,使这项工作

public partial class PDFOut : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string args = string.Format("\"{0}\" - ", Request.Form["url"]);//'http://www.google.com' is what I'm passing for testing
        var startInfo = new ProcessStartInfo(Server.MapPath("\\tools\\wkhtmltopdf.exe"), args)
        {
            UseShellExecute = false,
            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=test.pdf");
        Response.BinaryWrite(buffer);
        Response.End();
    }
}

你喜欢这个工作吗?将PDF写入临时目录,然后读取PDF并最终删除临时文件

protected void Page_Load(object sender, EventArgs e)
    {
        string outputFile = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName(), ".pdf");
        string args = string.Format("\"{0}\" \"{1}\"", Request.Form["url"], outputFile );
        var startInfo = new ProcessStartInfo(Server.MapPath("\\tools\\wkhtmltopdf.exe"), args)
        {
            UseShellExecute = false,
            RedirectStandardOutput = true
        };
        var proc = new Process { StartInfo = startInfo };
        proc.Start();

        proc.WaitForExit();
        proc.Close();

        var buffer= File.ReadAllBytes(outputFile);
        File.Delete(outputFile);

        Response.ContentType = "application/pdf";
        Response.AddHeader("content-disposition", "attachment; filename=test.pdf");
        Response.BinaryWrite(buffer);
        Response.End();
    }

我从主提要中阅读了标题中的
wkhtmltopdf
,我确信这是垃圾邮件<代码>wkhtmltopdf?我们真的没有名字了…:我一直让它将PDF文件写入临时目录。我不知道它会将pdf文件写入标准输出。您是否尝试过从实际的命令行运行命令?我不相信wkhtmltopdf有任何将结果文件输出到标准输出的示例。我可能错了。我不得不对临时文件的放置位置做一些调整,但除此之外,它就像一个符咒!非常感谢。