wkhtmltopdf.exe System.Security.SecurityException on cloud web server. How can i override server security policy

StackOverflow https://stackoverflow.com/questions/9478141

  •  13-11-2019
  •  | 
  •  

Question

I want to have a feature for my website which can print the page contents as PDF. I tried a few option for this but the best match was wkhtmltopdf as it also handle multilingual characters.

I got it working on my local server but when I uploaded it on a cloud web server for hosting now it is giving me the following error

Security Exception
Description: The application attempted to perform an operation not allowed by the security policy.  To grant this application the required permission please contact your system administrator or change the application's trust level in the configuration file.

Exception Details: System.Security.SecurityException: Request failed.

I changed the security policy to in web.config

<securityPolicy>
  <trustLevel name="Full" policyFile="internal"/>
</securityPolicy>

But it is still not working

Test URL is http://www.noor.com.asp1-20.dfw1-2.websitetestlink.com/ArticleDetails.aspx?Language=en-US&PageID=19&ArticleID=4

You can click the Download link on the back this download link convert the particular section of web page to PDF. It works fine on local server but it is not working for security reason on the web server. I had looked for days to get the PDF working and now it is not working on the web server. I tried other option but they iText for some reason it was not converting arabic it was printing junk characters only for arabic version of page.

Please advise what I should change to get it right.

I have developed this website using asp.net 4.0 using c#

Code I use to generate PDF

string url = "PrintArticle.aspx?articleID=" + Request["articleID"] + "&download=yes&Language=" + Request["Language"];

// string args = string.Format("\"{0}\" - ", url);
string args = string.Format("\"{0}\" - ", "http://www.domain.com.asp1-20.dfw1-2.websitetestlink.com/" + url);
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();
Was it helpful?

Solution

WORKING CODE: I resolved the issue by calling the PrintArticle.aspx from MainPage.aspx, When user clinks on the lnkbtnDownload link Button it will call the page PrinteArticle.aspx (Print Article Page is a simple page which show article title, date and Article Description) and pass the HTML to the code below which will then print it as a PDF file

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

        string args = string.Format("\"{0}\" - ", "http://xyz.net/" + url);
        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();
    }

I am passing other parameters as query string like articleID, Download, Language etc..

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top