472,809 Members | 2,800 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,809 software developers and data experts.

Replacing page content w/ a dynamically created pdf at https

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
}
}
}
Nov 19 '05 #1
7 2517
Hi Ryan,

Are there any empty href tags, images outside of the https directory, or
absolute URLs in the picture?
"Ryan Taylor" <rt*****@stgeorgeconsulting.com> wrote in message
news:OP**************@TK2MSFTNGP14.phx.gbl...
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
}
}
}


Nov 19 '05 #2
Hi Ken.

No, there are no images outside of the https directory. Even the PDF is
created within the https directory at runtime. Also, there are no absolute
URLs in the PDF. I think it has something to do with the way I am replacing
the page contents with the PDF. But I can not figure out what.

"Ken Cox [Microsoft MVP]" <BA************@sympatico.ca> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
Hi Ryan,

Are there any empty href tags, images outside of the https directory, or
absolute URLs in the picture?
"Ryan Taylor" <rt*****@stgeorgeconsulting.com> wrote in message
news:OP**************@TK2MSFTNGP14.phx.gbl...
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
}
}
}

Nov 19 '05 #3
Update.

So I replaced the display code to display just a .txt file with some garbage
message in it. The application still creates a PDF, but when it goes to
display it, the .txt file is displayed instead. So, it appears that
something about the PDF itself is causing the security warning. Another
"clue" is that I can navigate to the URL where the PDF is created,
https://mywebsite/pdfs/tmp342.pdf and it displays without the security
warning. I tried using Response.BinaryWrite() but I had the same problem.

"Ryan Taylor" <rt*****@stgeorgeconsulting.com> wrote in message
news:OE**************@TK2MSFTNGP09.phx.gbl...
Hi Ken.

No, there are no images outside of the https directory. Even the PDF is
created within the https directory at runtime. Also, there are no absolute
URLs in the PDF. I think it has something to do with the way I am replacing the page contents with the PDF. But I can not figure out what.

"Ken Cox [Microsoft MVP]" <BA************@sympatico.ca> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
Hi Ryan,

Are there any empty href tags, images outside of the https directory, or
absolute URLs in the picture?
"Ryan Taylor" <rt*****@stgeorgeconsulting.com> wrote in message
news:OP**************@TK2MSFTNGP14.phx.gbl...
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
}
}
}


Nov 19 '05 #4
Update 2.

I do not get this warning message in Firefox 1.0.

"Ryan Taylor" <rt*****@stgeorgeconsulting.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Update.

So I replaced the display code to display just a .txt file with some garbage message in it. The application still creates a PDF, but when it goes to
display it, the .txt file is displayed instead. So, it appears that
something about the PDF itself is causing the security warning. Another
"clue" is that I can navigate to the URL where the PDF is created,
https://mywebsite/pdfs/tmp342.pdf and it displays without the security
warning. I tried using Response.BinaryWrite() but I had the same problem.

"Ryan Taylor" <rt*****@stgeorgeconsulting.com> wrote in message
news:OE**************@TK2MSFTNGP09.phx.gbl...
Hi Ken.

No, there are no images outside of the https directory. Even the PDF is
created within the https directory at runtime. Also, there are no absolute
URLs in the PDF. I think it has something to do with the way I am

replacing
the page contents with the PDF. But I can not figure out what.

"Ken Cox [Microsoft MVP]" <BA************@sympatico.ca> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
Hi Ryan,

Are there any empty href tags, images outside of the https directory, or absolute URLs in the picture?
"Ryan Taylor" <rt*****@stgeorgeconsulting.com> wrote in message
news:OP**************@TK2MSFTNGP14.phx.gbl...
> 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
> }
> }
> }
>
>



Nov 19 '05 #5
Is it possible that the ASPNET account doesn't have sufficient read
permissions in that PDF directory where the file is being created?

"Ryan Taylor" <rt*****@stgeorgeconsulting.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Update.

So I replaced the display code to display just a .txt file with some
garbage
message in it. The application still creates a PDF, but when it goes to
display it, the .txt file is displayed instead. So, it appears that
something about the PDF itself is causing the security warning. Another
"clue" is that I can navigate to the URL where the PDF is created,
https://mywebsite/pdfs/tmp342.pdf and it displays without the security
warning. I tried using Response.BinaryWrite() but I had the same problem.

"Ryan Taylor" <rt*****@stgeorgeconsulting.com> wrote in message
news:OE**************@TK2MSFTNGP09.phx.gbl...
Hi Ken.

No, there are no images outside of the https directory. Even the PDF is
created within the https directory at runtime. Also, there are no
absolute
URLs in the PDF. I think it has something to do with the way I am

replacing
the page contents with the PDF. But I can not figure out what.

"Ken Cox [Microsoft MVP]" <BA************@sympatico.ca> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
> Hi Ryan,
>
> Are there any empty href tags, images outside of the https directory,
> or
> absolute URLs in the picture?
>
>
> "Ryan Taylor" <rt*****@stgeorgeconsulting.com> wrote in message
> news:OP**************@TK2MSFTNGP14.phx.gbl...
> > 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
> > }
> > }
> > }
> >
> >
>




Nov 19 '05 #6
It's unlikely. When I run the same application in FireFox I am not getting
the security warning. So it seems like it is something that is browser
dependent.

"Ken Cox [Microsoft MVP]" <BA************@sympatico.ca> wrote in message
news:uy**************@TK2MSFTNGP14.phx.gbl...
Is it possible that the ASPNET account doesn't have sufficient read
permissions in that PDF directory where the file is being created?

"Ryan Taylor" <rt*****@stgeorgeconsulting.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Update.

So I replaced the display code to display just a .txt file with some
garbage
message in it. The application still creates a PDF, but when it goes to
display it, the .txt file is displayed instead. So, it appears that
something about the PDF itself is causing the security warning. Another
"clue" is that I can navigate to the URL where the PDF is created,
https://mywebsite/pdfs/tmp342.pdf and it displays without the security
warning. I tried using Response.BinaryWrite() but I had the same problem.
"Ryan Taylor" <rt*****@stgeorgeconsulting.com> wrote in message
news:OE**************@TK2MSFTNGP09.phx.gbl...
Hi Ken.

No, there are no images outside of the https directory. Even the PDF is
created within the https directory at runtime. Also, there are no
absolute
URLs in the PDF. I think it has something to do with the way I am

replacing
the page contents with the PDF. But I can not figure out what.

"Ken Cox [Microsoft MVP]" <BA************@sympatico.ca> wrote in message news:%2****************@TK2MSFTNGP14.phx.gbl...
> Hi Ryan,
>
> Are there any empty href tags, images outside of the https directory,
> or
> absolute URLs in the picture?
>
>
> "Ryan Taylor" <rt*****@stgeorgeconsulting.com> wrote in message
> news:OP**************@TK2MSFTNGP14.phx.gbl...
> > 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
> > }
> > }
> > }
> >
> >
>


Nov 19 '05 #7
Hi Ryan,

I want to create a similar functionality as you have mentioned, creation
of a printable pdf dynamically using c#
According to your post, I downloaded htmldoc trail version for now.
I went through the code you have given and tried to use the same, just
changing some parameters here and there.But I am not able to generate
the pdf.
I had a few questions, as I am not very well versed with c#.net
programming.I would really appreciate if you could give me some help
regarding this.

1.Do I need to configure htmldoc with IIS?
2.Do I need to use any other name space in particular other than
system.io, system.diagnostics and system.configuration?
3.According to what I understood, I created a page pdf.aspx which is
called on the click event of the print button on querydata.aspx. And on
the page load of the pdf.aspx wrote the code which you have posted, to
call the pdfgenerator and displaypdf. Is this right?
4.What is "Htmldocinstallpath" in your program? Is it the path where
htmldoc exe is present?
5.I think, Pdfs is a folder where you store the pdf files generated.Is
this folder in the root directory or just any folder on the local
machine.
6.Also, do I need to give any security permissions, rights etc to the
folder?

I would really be thankful if you could help me with this.

Thanks again.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 19 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Moon | last post by:
Seems I still haven't got the hang of all those window generating code in Javascript. I've got a page with about 15 photo thumbnails. When you click on a thumbnail a new window pops up which shows...
1
by: Andrew Poulos | last post by:
Say I have a page, which has been created by a third party, and the page may contain some pre-specified text. How can I find and replace that text dynamically? For example, if the page I have...
0
by: Epson Barnett | last post by:
I'm new to ASP.NET and have run across a problem several times while working on web apps. I often need to create content dynamically (based on an event) which contains dynamically created...
3
by: Vince Mele | last post by:
We are having a small problem with a couple of reports we developed for a client website. On two of the reports, sometimes (most of the time) we receive a warning message before the output of...
6
by: Tony Doyle | last post by:
All, I have a web form with 3 embedded panels, all working fine. However, I have now added a Checkbox where autopostback=true, and only the ascx pages on my page appear, none of the panels /...
6
by: scottyman | last post by:
I can't make this script work properly. I've gone as far as I can with it and the rest is out of my ability. I can do some html editing but I'm lost in the Java world. The script at the bottom of...
5
by: Charlie King | last post by:
I'm trying to send data to a remote script (a credit card processing third party) from my site using POST. Currently, I'm doing it using the ususal form dynamically built with my values. This...
3
by: Aaron | last post by:
I'm trying to parse a table on a webpage to pull down some data I need. The page is based off of information entered into a form. when you submit the data from the form it displays a...
3
by: powersakthi | last post by:
Hi All, I am using LWP::UserAgent and HTML::TreeBuilder to communicate to the below site Here is the website map: https://secure.server.com/index.htm which asks for two fields user_id...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.