473,320 Members | 1,974 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

PDF export question

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

Jan 17 '07 #1
7 3361
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.CrystalR eports.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

<gl****@yahoo.comwrote in message
news:11**********************@v45g2000cwv.googlegr oups.com...
>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
Jan 17 '07 #2
Thanks Tim, I will give it a try.

- Gary
------------------------------
Tim Mackey wrote:
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.CrystalR eports.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

<gl****@yahoo.comwrote in message
news:11**********************@v45g2000cwv.googlegr oups.com...
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
Jan 17 '07 #3
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 wrote:
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.CrystalR eports.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

<gl****@yahoo.comwrote in message
news:11**********************@v45g2000cwv.googlegr oups.com...
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
Jan 17 '07 #4
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
<gl****@yahoo.comwrote in message
news:11*********************@38g2000cwa.googlegrou ps.com...
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 wrote:
>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.Crystal Reports.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

<gl****@yahoo.comwrote in message
news:11**********************@v45g2000cwv.googleg roups.com...
>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
Jan 17 '07 #5
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
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
gl****@yahoo.com wrote:
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 wrote:
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.CrystalR eports.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

<gl****@yahoo.comwrote in message
news:11**********************@v45g2000cwv.googlegr oups.com...
>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
>
Jan 18 '07 #6
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

<gl****@yahoo.comwrote in message
news:11**********************@38g2000cwa.googlegro ups.com...
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
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
gl****@yahoo.com wrote:
>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 wrote:
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.CrystalR eports.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

<gl****@yahoo.comwrote in message
news:11**********************@v45g2000cwv.googlegr oups.com...
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
Jan 18 '07 #7
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
-----------------------------------------------------------------------------------------------

Tim Mackey wrote:
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

<gl****@yahoo.comwrote in message
news:11**********************@38g2000cwa.googlegro ups.com...
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
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
gl****@yahoo.com wrote:
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 wrote:
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.CrystalR eports.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

<gl****@yahoo.comwrote in message
news:11**********************@v45g2000cwv.googlegr oups.com...
>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
>
Jan 19 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: R.A. Joemman | last post by:
Hi, We are migrating a database from Oracle 8.1.7.0.0 to Oracle 9.2.0.1.0 on AIX. Export as follows: exp ${user}/${pwd} compress=n file=${file} log=${log} owner=${schema} grants=y indexes=y...
10
by: New_user | last post by:
Hello. Excerpt from ISO/IEC 14882 : 14/6 A namespace-scope declaration or definition of ........skipped...... a non- inline member function of a class template or a static data member of a...
205
by: Jeremy Siek | last post by:
CALL FOR PAPERS/PARTICIPATION C++, Boost, and the Future of C++ Libraries Workshop at OOPSLA October 24-28, 2004 Vancouver, British Columbia, Canada http://tinyurl.com/4n5pf Submissions
6
by: maricel | last post by:
Is there anybody out there who have any idea why EXPORT is relatively slower when putting the output file on a network drive - map drive from onother PC compared to putting it on my local PC drive...
7
by: Pat | last post by:
I would like to send the Print Preview of a MS Access form to a Snapshot file. The form contains an OLE graph. BACKGROUND A snapshot of a report is possible. If I could I would use a report to...
1
by: Bridget Willey | last post by:
I am using ACT 6 and am trying to "split" the database between records for customers and junk records. The accounts designated as "customers" have that word in the ID field, and I am using that...
1
by: | last post by:
I am trying to export a database to a .txt file. I want to be able to specificy the deliminater but I'm unable to because the export wizard never appears. When I select export as txt,csv etc I...
4
by: paul.chae | last post by:
I have a table in Access with about 3000 records. There are ~60 unique values in the ID field for the 3000 records. What I would like to do is automatically generate multiple Excel worksheets...
17
by: Fabry | last post by:
Hi All, I'm new of this group and I do not know if this is the correct group for my question. I have a DLL with its export library (.lib) wrote in Borland C++ 6. In borland everything is OK and...
5
by: JHNielson | last post by:
I have a somewhat simple question, but have been baffled by it for a while, and now I'm on a tight deadline - have to get it done within 24 hours. I am trying to export a set of files to my hard...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.