Pdf.Save(HttpResponse) code generates the PDF twice

We use this code to override the Response object in order to produce a PDF in a popup JavaScript window:

Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
lReportStruct = CType(Session(“REPORT_DESCRIPTION_OBJECT”), ReportDescStructure)
Dim objReport As New SM_Reporter(lReportStruct)
objReport.Header = lReportStruct.Report_Title
objReport.Orientation = lReportStruct.Report_Orientation
Dim objPdf As Pdf = objReport.GetPdf()
objPdf.Save(Me.Response)
End Sub

The problem is that when we debug, we see that this code is being executed twice (!).

Do you have any code examples of using the Pdf.Save(HttpResponse) method to return a PDF to the browser without it being generated twice?

Thanks,
Edward

Dear Edward,

Thanks for your consideration.

You can use the following code to avoid your code be executed twice:

[C#]

Pdf pdf = new Pdf();
pdf.BindXML(“C:/xml/Test.xml”,null);Response.ClearContent();
Response.ClearHeaders();
Response.ContentType=“application/pdf”;
pdf.Save(Response.OutputStream);
Response.End();

[Visual basic]

Dim pdf As Pdf = New Pdf()
pdf.BindXML(“C:/xml/Test.xml”,Nothing)Response.ClearContent()
Response.ClearHeaders()
Response.ContentType=“application/pdf”
pdf.Save(Response.OutputStream)
Response.End()


I used the above code but it still executes twice.

You can recreate this by using this example:
http://www.tanguay.info/images/books/pdfaspose-2004-05-18--TwoResponsesBug.zip
Just unzip this in a web directory with WRITE rights and run it in your browser.

After running it, you will see a file called “DebugOutput.htm” in the root directory
which will have two entries similar to these:

18.05.2004 15:39:12 [WAS HERE]
18.05.2004 15:39:17 [WAS HERE]

This shows that the execution created the report twice. You can even see in the popup window how after 5 seconds it blinks and the next generation of the report begins. Then when the second generated report is finished, it is displayed.

If you use this code in Visual Studio .NET, you can debug and see how it runs through the overwrite render method twice and even skips over the Response.End().

How can we get this so that it only generates the PDF once?

Thanks,
Edward

OK, we found out what was causing this:

1. The above code will post twice ONLY if you have your browser settings set to:

Tools | Internet Options | Settings | Check for newer versions of stored pages: Never

If you have it on “Every visit to the page” then it will work fine.

2. This is only true if the .aspx page is a pop-up page inside a javascript window, otherwise it will work fine with both settings.

It’s still unclear why this is so. Any further information on this would be great. Thanks.

Edward

Dear Edward,

Thanks for your consideration.

Thanks for your information. We will try to find the reason and give further information.

We have found that adding this line:

Response.Cache.SetCacheability(HttpCacheability.NoCache)

enables the report to be generated only once regardless of the browser settings.

This is our current solution to this problem.

Edward

Although the above line:

Response.Cache.SetCacheability(HttpCacheability.NoCache)

forces the page to only produce the PDF once, which solved our problem, it also causes the following problem:

When your browser settings (IE) are on “Every visit to page”, then the browser will pop up a Open/Save dialog instead of displaying the PDF in the pop-up window.

We still haven’t resolved this.

If anyone has any code examples to display a PDF inside a javascript window which works no matter how the Internet Explorer cache settings are set, we would love to have this code. Perhaps there is a totally different way of doing this. (If it is NOT a “window.open” Javascript window then everything works, but this is not an option for us, it has to be in a pop-up Javascript window).

Thanks,

Edward

Edward,

This is what we are doing and I wonder if you have tried adding “Content-Disposition” to the response header. This worked for us when we were getting the Open/Save dialog box. Let me know…

David


Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = “application/pdf”;

Response.AddHeader(“Content-Disposition”, “inline;filename=report.pdf”);

m_pdf.Save(“report.pdf”, Aspose.Pdf.SaveType.OpenInBrowser, Response);
Response.End();

If you put your code to create the pdf in the page_load between If IsPostback then… End If and in the htmlcode for the page you add a script after the form closing tag that submits the form like this:





then after the first cal of the form the submit will be executed and next to that the pdf is created. The response is ended with response.end. Works well on a non secure site.
On a secure site (SSL) an error is raised for non secure items on the page. Seems that somewhere while saving the pdf to the response a non secure item is loaded. (maybe from adobe ) but I haven’t solved this prblem yet. I anyone knows how to solve this??

Frans van Luit