0

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

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

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

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

为了让它正常工作,我们需要给网站完全信任级别&我不确定为什么代码不运行我给它相同的域路径如果我创建 putPrintArticle.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

4

2 回答 2

0

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

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

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

于 2012-07-05T10:40:00.657 回答
0

输出变量包含空字符串 我的代码如下: try { string url=Request.Url.GetLeftPart(UriPartial.Authority) +"/PrintQuickPrescription.aspx?DoctorId=" + DoctorID + "&DispnID=" + DispnID + "&ApptID=" + ApptID + "&PatientID=" + PatientID; System.Diagnostics.Process 进程 = 新 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
    {
    }
于 2014-09-20T12:47:58.347 回答