• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 424
  • Last Modified:

Trouble with Response.OutputStream

I have a script that generates a bunch of PDFs on the fly.  It works fine so long as it's the only code and I put it in Page_Load() event.  However, I want to put it inside of a Button Click event now and I'm having problems.

What happens is I click the button, call a routine to create the PDFs as follows:

Response.Clear();
Response.ContentType = "application/pdf";
// a bunch of code that creates the PDFs.
Response.OutputStream.Flush();
Response.OutputStream.Close();
Response.End();

When I put it in the button click it works fine, but at the end I have a blank screen.  What I need is my form back with the button and other form objects.  That's not happening because I have the  Response.End statement in there.  The problem is, if I remove it I get the following Adobe Reader error:

File does not begin with '%PDF-'.
Local\EWH+!v8(_k$

Thanks for any help.
0
dtburdick
Asked:
dtburdick
  • 2
2 Solutions
 
madginoCommented:
Once the headers had been send to the client (browser) it's too late to change the page content type. Button click event occurs way after the page is loaded so the browser already knows that it's a page instead of pdf file.
You need to do this on page_load and rely on IsPostBack value to load the page or write the pdf.
Other solution would be to open pdf in a new window. Use Content-Disposition=inline to display it in the browser otherwise you'll be asked to save the pdf and the new window will remain blank.
0
 
dtburdickAuthor Commented:
Thanks, so much.  I got it working based on what you said, but I have no idea what I'm doing here and I don't want to crush my server by doing it wrong.  Can you please tell me if this is going to cause a huge memory leak.  I am closing the writer, but I don't know if there is something I'm supposed to do to destroy the MemoryStream.  I know .NET has some Garbage Collector that cleans that kind of thing up, but I don't know how much I'm supposed to do.

What I did was comment out all of the lines of code regarding the Response object and just ran the code the creates the PDFs and it worked.  The only reason I did that is because it was in the sample code I was using to begin with.

The code that creates the PDF opens a stream

        System.IO.MemoryStream m = new System.IO.MemoryStream();
        iText.Document doc = new iText.Document(iText.PageSize.LETTER);
        iText.pdf.PdfWriter writer = iText.pdf.PdfWriter.GetInstance(doc, m);
        writer.ViewerPreferences = PdfWriter.HideToolbar;
        doc.Open();

A bunch of code to create the PDF.

        // Delete the file if it exists.
        if (System.IO.File.Exists(path))
        {
            System.IO.File.Delete(path);
        }
        using (System.IO.FileStream fs = System.IO.File.Create(path))
        {
            fs.Write(content, 0, (int)content.Length);
        }

        writer.Flush();
0
 
dtburdickAuthor Commented:
I selected my own comment as part of the solution as it contains useful information.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now