views:

433

answers:

4

When a user clicks on a link, it opens a new window (since target="_blank") calls an xyz.ASPX page that will dynamically create an PDF invoice, saving it into a Database (not a physical location) and then I need to be able to open the PDF file within the ASPX page.

Right now whats happening is that the aspx that generates the PDF file is called, it does its stuff but then opens Acrobat to display the invoice PDF. However, the xyz.aspx page is blank and does not display anything. I do not like this and would like xyz.aspx to display the PDF file.

From my research, it seems that if you want to display the PDF within the ASPX page, you need an iFrame or a custom control. However, since the PDF is dynamically created and not stored on disk, how do I set the source for the iFrame. Is there a way to set the src as a response object or some other in-memory object?

Is there an easy way of doing this? I dont want to use a custom control. I am using .NET 3.5 and C# with Master pages and CSS themes but I have disabled it for this page.

Source *xyz.aspx:*

`<%@ Page language="c#" EnableTheming="false"
ContentType="application/pdf" Codebehind="xyz.aspx.cs" AutoEventWireup="True" Inherits="namespace1.xyz" %>

'

xyz.aspx.cs (gets the PDF file from the DB and then writes to Response object)

protected void Page_Load(object sender, EventArgs e)
    {
        byte[] pdfBinaryFile = GetPDFFileFromDB(PDFId);

        if(pdfBinaryFile != null) 

            Response.BinaryWrite(pdfBinaryFile);

}

A: 

You can Response.Write the pdf to the browser...

To have the browser display it, you need to specify the correct HTTP Header...

Alexander
on the aspx page, I have the contentType="application/pdf" and I am binaryWriting to the response object (see source code). Am I missing anything else?
Pritika
The browser does exactly what it's told, it opens up Acrobat so the user can read the PDF. No HTML browser(that I know of) can natively read a PDF and have said PDF inline with other HTML elements(and I don't know of a plugin that works like that either).
Earlz
A: 

Look for info on what headers to use for this here: PHP header examples (Content-Disposition is one important one I think). Maybe this will help.

Bandi-T
+1  A: 

Look into custom handlers(.ashx files) and Response.Write.

Though your kinda at the whim of Adobe because I don't think their plugin plays nice in iframes. One thing I would look into is instead of displaying the PDF, instead have an HTML preview and then have a link so the user can download the actual PDF. That would be a much more sane way to do it because people without Acrobat installed are going to get a nice "download pdf" box show up no matter if its in an iframe or not. (note I say Acrobat. There are people that have a PDF reader but not a PDF reader plugin for their browser, such as myself)

For the PDF download, all you would need is a custom HTTP handler that gets sent a query string telling it which PDF they want to download. (and thus, would not require writing the PDF to disk)

Earlz
+1  A: 

Try adding a Content-Disposition: inline header:

protected void Page_Load(object sender, EventArgs e)
{
    byte[] pdfBinaryFile = GetPDFFileFromDB(PDFId);

    if (pdfBinaryFile != null)
    {
        Response.Clear();
        Response.ContentType = "application/pdf";
        Response.AppendHeader(
            "Content-Disposition", "inline; filename=\"test.pdf\"");
        Response.BinaryWrite(pdfBinaryFile);
        Response.End();
    }
}
LukeH