Wkhtmltopdf.exe System.Security.SecurityException on the cloud web server. How to override server security policy

I want to have a function for my site that can print the contents of a page in PDF format. I tried several options for this, but wkhtmltopdf was the best match, as it also handles multilingual characters.

I worked on my local server, but when I uploaded it to the cloud web server for hosting, it gives 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 trust level in the configuration file. Exception Details: System.Security.SecurityException: Request failed. 

I changed the security policy to web.config

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

But it still does not work

Test URL 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 of this download link to convert a specific section of the web page to PDF. It works fine on the local server, but it does not work for security reasons on the web server. I searched for a few days to get the PDF file and now it does not work on the web server. I tried another option, but iText for some reason did not convert the Arabic language, it printed unnecessary characters only for the Arabic version of the page.

Please let me know what I need to change to understand this.

I designed this site using asp.net 4.0 using C #

The code I use to create the 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(); 
+4
source share
1 answer

WORK CODE: I solved the problem by calling PrintArticle.aspx from MainPage.aspx. When the user clicks on the link button lnkbtnDownload , it is called on the PrinteArticle.aspx page (the "Print article" page is a simple page that shows the title of the article, date and description of the article) and pass the HTML code below, which will then print it as a file Pdf

  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 pass in other parameters as a query string, such as articleID, Download, Language, etc.

+3
source

Source: https://habr.com/ru/post/1398765/


All Articles