Hi.
I have some code that dynamically generates a PDF and spits this content
directly to the web browser. I use HTMLDoc to create the Pdf's from html. So
the user can click on a button "Print PDF" and the current page magically
becomes a PDF file. This worked great until we moved the site to https. Now,
when the button is clicked, I get a warning that
This page contains both secure and nonsecure items.
Do you want to display the nonsecure items?
If I choose Yes, the PDF is displayed. No, and it isn't, as expected from
the dialog. However, The PDF is created within the context of the
application, and written to a Pdfs directory within the application path. I
do not want the users to receive this message
What might I be doing wrong?
Here is my code.
// When the user clicks the button, we are redirected to the page that
contains the PDF creation functions
// Pass the URL we are from so we not which page to create a PDF from
private void btnPrint_Click(object sender, System.EventArgs e)
{
string Url = "Response/ViewResponse.aspx&" +
Request.QueryString.ToString();
Response.Redirect("~/Pdfs/Pdf.aspx?CallingURL=" + Url);
}
// Call the creation and display functions
private void Page_Load(object sender, System.EventArgs e)
{
string callingURL = Request.Params["CallingURL"];
if(callingURL != null && callingURL != "")
{
string filename = PDFGenerator.CreatePDF(callingURL);
PDFGenerator.DisplayPDF(filename);
}
}
public class PDFGenerator
{
// Thanks to
// http://www.codeproject.com/aspnet/HTML2PDF.asp
// For the excellent example on how to use HTMLDOC in an ASP.NET
environment
public static string CreatePDF(string URL)
{
// file and path variables
string tempFile = "";
string filename = "";
string filepath = "";
try
{
// Create the temporary file names we need
tempFile = Path.GetTempFileName();
filename = Path.GetFileNameWithoutExtension(tempFile);
filepath = HttpContext.Current.Server.MapPath("~/Pdfs") + "\\";
// delete the file in the temp directory
File.Delete(tempFile);
// Execute the current page and get the output in a string
StringWriter pageResponse = new StringWriter();
// NOTE: It appears that Server.Execute executes the page but in the
context
// of the page that Server.Execute was called in. As such, any parameters
needed
// on called page, need to exist in the calling page.
HttpContext.Current.Server.Execute("~/" + URL, pageResponse);
// Create the html file for conversion
StreamWriter fileIO = File.CreateText(filepath + filename + ".html");
fileIO.WriteLine(pageResponse.ToString());
fileIO.Close();
// Execute hghtmldoc.exe to convert our html to PDF
System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo.FileName =
ConfigurationSettings.AppSettings["HtmlDocInstallPath"];
process.StartInfo.Arguments = "--webpage --quiet --browserwidth
800 --right 1in --left 1in --size Letter -f " + filename + ".pdf " +
filename + ".html";
process.StartInfo.WorkingDirectory = filepath;
process.Start();
process.WaitForExit();
}
catch (Exception E)
{
string err = E.Message;
}
finally
{
// delete the html file
File.Delete(filepath + filename + ".html");
}
// return the pdf file
return (filepath + filename + ".pdf");
}
public static void DisplayPDF(string PDF)
{
// If the file exists
if(File.Exists(PDF))
{
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.ContentType = "Application/pdf";
try
{
HttpContext.Current.Response.WriteFile(PDF);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.Close();
}
catch
{
HttpContext.Current.Response.ClearContent();
}
finally
{
File.Delete(PDF);
}
}
else
{
// Could not find file
}
}
}