简体   繁体   中英

Open newly generated PDF in new window

I have some code that generate a PDF and then shows it on the screen, without creating a PDF on the server.

I use this piece of code to generate the PDF and show it in the same window where the create button is:

using (Stream theStream = temp.GetStream())
                {
                    Response.Clear();
                    Response.AddHeader("content-disposition", "inline; filename=MyPDF.PDF");
                    Response.AddHeader("content-length", theStream.Length.ToString());
                    Response.ContentType = "application/pdf";

                    //Response.TransmitFile(theStream.ToString());

                    long theLen = theStream.Length;
                    byte[] theData = new byte[theLen >= 32768 ? 32768 : (int)theLen];
                    while (theLen > 0)
                    {
                        theStream.Read(theData, 0, theData.Length);
                        Response.BinaryWrite(theData);
                        theLen -= theData.Length;
                        if (theLen < theData.Length && theLen > 0)
                            theData = new byte[(int)theLen];
                    }


                }

How do I get the PDF to be shown in a new window/tab?

There is no way to force new window from server-side code. You should consider replacing the button with the link, like this:

 <form target="_blank" method="get" action="http://somesite.com/user/getpdf">
        <input type="submit" value="View PDF Invoice">
    </form>

this will open the link in new window and you should just return PDF content generated on /user/getpdf/ call.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM