Path issue with wkhtmltopdf.exe to convert HTML file to PDF

不打扰是莪最后的温柔 提交于 2019-12-11 17:46:17

问题


I am using wkhtmltopdf to convert HTML file into PDF document on a link Button http://code.google.com/p/wkhtmltopdf/

When User Click on a link Button it runs the following code as shown below code in pass file path as an argument ProcessStartInfo. THis code works fine in Following Scenarios only

Taking Into consideration that website is hosted on Domain http://www.xyz.net/

  1. When i mention path as http://demo.XYZ.net/ It works fine
  2. When i mention path as http://www.XYZ.net/ It doesn't work
  3. In-case of local-host it works fine if path is http://localhost:51005/XYZ/or http://web:8080/

For this to work properly we need to give the website full trust level & i am not sure why code doesn't run i give it the same domain path if i create put PrintArticle.aspx if i create a sub domain then it will work fine. I am nost sure if this is a security problem or what

Code Below

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;
    }
}

Error Message in case file is on same domain

Server Error in '/' Application. The resource cannot be found.

Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly. Requested URL: /PrintArticle.aspx

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.272


回答1:


I resolved this issue by using the following statement

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

Now it is working fine i am not sure what it doesn't work when i specify the file path.




回答2:


output variable contains empty string my code as follows: try { string url=Request.Url.GetLeftPart(UriPartial.Authority) +"/PrintQuickPrescription.aspx?DoctorId=" + DoctorID + "&DispnID=" + DispnID + "&ApptID=" + ApptID + "&PatientID=" + PatientID; System.Diagnostics.Process 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
    {
    }


来源:https://stackoverflow.com/questions/11342292/path-issue-with-wkhtmltopdf-exe-to-convert-html-file-to-pdf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!