I don’t know why the Server.Execute is throwing an exception, but here is an alternative to using that call.
You are using Server.Execute to get the web page to convert to HTML. Instead of that approach have Winnovative make that call. The trick is to allow that page request to access the current user’s session. You specify the URL to call and then supply the user credentials in the cookie collection of the PDFConverter.
See this page and choose the approach based on your authentication technique.
Winnovative Authentication Handling
Edit:
@LordHonydew, We have gone in a big circle in the comments and ended up at the beginning. Let me try this again.
First, in the Server.Execute exception is there an inner exception? There could be more information that helps explain what is going wrong. Even after fixing that issue there are other items that must be fixed.
Second, when using Winnovative to generate a PDF from a web page that is secured you must provide credentials to the PDFConverter so that it can access the page and its resources. There are a couple of ways winnovative can get the HTML: one is by giving the PDFConverter the URL to call and another is to use the Server.Execute to get the HTML directly and then provide the PDFConverter with the HTML, which is how you are doing it. Either way the PDFConverter still needs to talk to the server to get the additional page resources. Things like images and CSS files are not in the HTML, they are referenced by the HTML. The converter will make calls to the server to get these items. Since your application is secured you must provide the converter with credentials to access the server. We will do this by using the same authentication cookie that is used for each page request. There are other ways such as supplying user name and password. That link above explains the various ways.
This code gets the auth cookie from the current request and gives it to the converter:
pdfConverter.HttpRequestCookies.Add(FormsAuthentication.FormsCookieName,
Request.Cookies[FormsAuthentication.FormsCookieName].Value);
Finally, the converter.SavePdfFromUrlToFile is not the right method to use. That would only save the receipt to the local server’s drive. You need to stream it back to the user.
Try the following. Set a breakpoint in the catch block so you can see if there is an inner exception.
protected void Save_BtnClick(object sender, EventArgs e)
{
// Get the web page HTML as a string
string htmlCodeToConvert = null;
using (StringWriter sw = new StringWriter())
{
try
{
System.Web.HttpContext.Current.Server.Execute("Receipt.aspx", sw);
htmlCodeToConvert = sw.ToString();
}
catch (Exception ex)
{
// set breakpoint below and on an exception see if there is an inner exception.
throw;
}
}
PdfConverter converter = new PdfConverter();
// Supply auth cookie to converter
converter.HttpRequestCookies.Add(System.Web.Security.FormsAuthentication.FormsCookieName,
Request.Cookies[System.Web.Security.FormsAuthentication.FormsCookieName].Value);
// baseurl is used by converter when it gets CSS and image files
string baseUrl = Request.Url.Scheme + "://" + Request.Url.Authority +
Request.ApplicationPath.TrimEnd('/') + "/";
// create the PDF and get as bytes
byte[] pdfBytes = converter.GetPdfBytesFromHtmlString(htmlCodeToConvert, baseUrl);
// Stream bytes to user
Response.Clear();
Response.AppendHeader("Content-Disposition", "attachment;filename=Receipt.pdf");
Response.ContentType = "application/pdf";
Response.OutputStream.Write(pdfBytes, 0, pdfBytes.Length);
HttpContext.Current.ApplicationInstance.CompleteRequest();
}