Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

PDF export question

1 view
Skip to first unread message

glb...@yahoo.com

unread,
Jan 17, 2007, 11:35:24 AM1/17/07
to
I have aspnet code (C#) to create a Crystal Report and export to pdf
(in browser) without a Crystal Report Viewer.

Works great except when the user attempts to save the file from the
browser. In the "Save a copy ..." dialog box the filename defaults to
the name of aspx page. I need it to default to a custom report name.
How can I specify the new file name in aspnet and pass it to the new
PDF page?

Thanks for the help,
Gary

Tim Mackey

unread,
Jan 17, 2007, 12:25:59 PM1/17/07
to
hi gary,
you may be missing a 'Content-Disposition' http header to specify the
filename. here is what i use, it works for me with the correct filename:

public static void
ExportCrystalReportToPDF(CrystalDecisions.CrystalReports.Engine.ReportClass
rpt, string filename)
{
MemoryStream stream =
(MemoryStream)rpt.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);
byte[] bytes = new Byte[stream.Length];
stream.Read(bytes, 0, (int)stream.Length);
stream.Close();
HttpResponse response = HttpContext.Current.Response;
response.Clear();
response.ClearContent();
response.ClearHeaders();
response.Buffer= true;
response.ContentType = "application/pdf";
response.AddHeader("Content-Disposition", "inline;filename=\"" + filename
+ "\"");
response.BinaryWrite(bytes);
response.End();
}

hope this helps
tim

<glb...@yahoo.com> wrote in message
news:1169051724.1...@v45g2000cwv.googlegroups.com...

glb...@yahoo.com

unread,
Jan 17, 2007, 1:10:40 PM1/17/07
to
Thanks Tim, I will give it a try.

- Gary
------------------------------

glb...@yahoo.com

unread,
Jan 17, 2007, 1:31:22 PM1/17/07
to
No, it didn't work. Here is the original code I have:

HttpResponse oResponse = HttpContext.Current.Response;
oResponse.ClearContent();
oResponse.ClearHeaders();
oResponse.ContentType = "application/pdf";
oResponse.WriteFile(sOutFile.ToString().Trim());
oResponse.Flush();
oResponse.Close();

// ***** sOutFile = name of pdf file being read and output into the
browser. ***///

- Gary
--------------------------------------------------------------------------------------------------

Tim Mackey

unread,
Jan 17, 2007, 3:59:20 PM1/17/07
to
hi gary,
sorry to be pedantic, but could you post your updated code with the
content-disposition header specifying the filename? just so we can check
that you have it right. the code you posted is not supposed to send any
filename with the content. WriteFile just sends the bytes.
tim


<glb...@yahoo.com> wrote in message
news:1169058682....@38g2000cwa.googlegroups.com...

glb...@yahoo.com

unread,
Jan 18, 2007, 7:53:41 AM1/18/07
to
Tim,

Thanks for keeping on top of this problem, it's appreciated. Okay,
here is the code I have:

FileStream stream = new FileStream(sOutFile, FileMode.OpenOrCreate,
FileAccess.Read );


byte[] bytes = new Byte[stream.Length];
stream.Read(bytes, 0, (int)stream.Length);
stream.Close();

HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.ContentType = "application/pdf";
HttpContext.Current.Response.AppendHeader("Content-Disposition",
"attachment;filename=myfilename.pdf");
HttpContext.Current.Response.BinaryWrite(bytes);
HttpContext.Current.Response.End();

It opens the file as an attachment and asks the user if they want to
"save" or "open". It passes the correct file name. I thought that
would suffice, but found out this morning that it won't. They
(customer) want it to open into a browser and if the user decides to
save it, the name of the report (plus a time stamp) should default into
the "filename" area of the "Save a copy ..." dialog box. I tried the
"inline" and several other code changes that someone suggested but it
did not work, it keeps sending the aspx page name with ".pdf" tacked
onto it.

What am I doing wrong?

- Gary
-------------------------------------------------------------------------------------------------------------------------------------------------------------------

Tim Mackey

unread,
Jan 18, 2007, 10:13:14 AM1/18/07
to
hi gary,
i tried out your code and mine does the same thing. inline viewing always
loses the filename. i have a possible explanation: the firebug extension to
firefox shows the source of the inline page as follows:

<html>
<body marginwidth="0" marginheight="0">
<embed width="100%" height="100%" name="plugin"
src="http://localhost:63106/Default.aspx" type="application/pdf"/>
</body>
</html>

at least for firefox, it creates a html page to embed the pdf. perhaps IE
does the same thing. as you can see, there is no specification here of the
correct file name.

when you open the pdf directly, e.g. drag it into a browser window, the same
wrapper html page is created, except it has the proper filename, then when
you save in Acrobat, it gets the name of the file right.

what i suggest (and what i do in my apps) is to save the PDF file to a temp
folder within the web site, and just do a Response.Redirect to the pdf file.
it displays inline, and it will retain the correct filename. i run a script
every night on the server to clear out the temp folder.

hope this helps
tim

<glb...@yahoo.com> wrote in message
news:1169124821.1...@38g2000cwa.googlegroups.com...

glb...@yahoo.com

unread,
Jan 19, 2007, 2:07:20 PM1/19/07
to
That's an acceptable workaround ... I'll try that if the client insists
on having the filename gets transferred.

Thanks for all your help!

- Gary

0 new messages