【发布时间】:2012-07-05 17:41:37
【问题描述】:
我正在使用wkhtmltopdf 将 HTML 文件转换为链接按钮上的 PDF 文档
http://code.google.com/p/wkhtmltopdf/
当用户单击链接按钮时,它会运行以下代码,如下所示的代码在传递文件路径中作为参数 ProcessStartInfo。此代码仅在以下场景中工作正常
考虑到网站托管在域http://www.xyz.net/
- 当我提到路径为
http://demo.XYZ.net/它工作正常 - 当我提到路径为
http://www.XYZ.net/它不起作用 - 在本地主机的情况下,如果路径是
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