468,284 Members | 1,530 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,284 developers. It's quick & easy.

Creating PDFs dynamically forces page reload

Hi all,

Im having difficulty serving synamically generated PDFs to clients via
asp.net. Im using crystal reports to generate them. In my page load I have
the following:

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
try
{
ReportDocument rd = new ReportDocument();
rd.Load(file);
for (int i = 1; i < Request.QueryString.Count; ++i)
{
//setup report parameter
}
MemoryStream oStream = null; // using System.IO
oStream =(MemoryStream)rd.ExportToStream

(CrystalDecisions.Shared.ExportFormatType.Portable DocFormat);
Response.Clear();
Response.ContentType = "application/pdf";
Response.Buffer= true;
Response.BinaryWrite(oStream.ToArray());
}
catch (Exception ex)
{
throw new ApplicationException(ex.Message, ex);
}
Response.End();
}

Although this works fine, Page_load gets called twice before the pdf is
served to the client. Any ideas why this is? For a time intesive reoprt its
pretty critical that its only run the once!

Some notes:

If the Response.BinaryWrite call is commented out, page_load is only called
once, however I get a blank page (as expected).

On the other hand, if Response.End is commented out, then the pdf is served
as a download ("do you wish to open or save etc") with the aspx page as the
default filename - saving this as a pdf results in the report. Again,
page_load is only called the once.

Keeping both calls in displays the pdf correctly, but only after the second
call to page_load. There is a screen reload between the two calls though.

Thanks in advance,

Shak
Nov 18 '05 #1
1 1695
when IE it loads an file that requires a viewer object (like pdf) it may
request more than once. Older versions of IE will actually do 3 requests.

when the browser get a pdf response, it knows it must load acrobat reader,
so it checks again with a request to only fetch the mime-type (of course if
your page doesn't check the request header, it send the whole response.)
after validation the mime-type, it loads acrobat, then gives acrobat the url
of the pdf. acrobat then downloads the file and displays. with more recent
versions of IE, an attempt is made to keep the file in the download cache to
not bother with extra downloads, but does not always work out.

-- bruce

"spammy" <me@privacy.net> wrote in message
news:2p************@uni-berlin.de...
Hi all,

Im having difficulty serving synamically generated PDFs to clients via
asp.net. Im using crystal reports to generate them. In my page load I have
the following:

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
try
{
ReportDocument rd = new ReportDocument();
rd.Load(file);
for (int i = 1; i < Request.QueryString.Count; ++i)
{
//setup report parameter
}
MemoryStream oStream = null; // using System.IO
oStream =(MemoryStream)rd.ExportToStream

(CrystalDecisions.Shared.ExportFormatType.Portable DocFormat);
Response.Clear();
Response.ContentType = "application/pdf";
Response.Buffer= true;
Response.BinaryWrite(oStream.ToArray());
}
catch (Exception ex)
{
throw new ApplicationException(ex.Message, ex);
}
Response.End();
}

Although this works fine, Page_load gets called twice before the pdf is
served to the client. Any ideas why this is? For a time intesive reoprt its pretty critical that its only run the once!

Some notes:

If the Response.BinaryWrite call is commented out, page_load is only called once, however I get a blank page (as expected).

On the other hand, if Response.End is commented out, then the pdf is served as a download ("do you wish to open or save etc") with the aspx page as the default filename - saving this as a pdf results in the report. Again,
page_load is only called the once.

Keeping both calls in displays the pdf correctly, but only after the second call to page_load. There is a screen reload between the two calls though.

Thanks in advance,

Shak

Nov 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Ola Fjelddahl | last post: by
2 posts views Thread by Pawan | last post: by
8 posts views Thread by Donald Xie | last post: by
3 posts views Thread by Steve Wark | last post: by
4 posts views Thread by Harry | last post: by
12 posts views Thread by Mats Lycken | last post: by
6 posts views Thread by Rain | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.