PDF export question

G

glbdev

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
 
T

Tim Mackey

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
 
G

glbdev

Thanks Tim, I will give it a try.

- Gary
------------------------------
Tim said:
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

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
 
G

glbdev

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 said:
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

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
 
T

Tim Mackey

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


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 said:
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

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
 
G

glbdev

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
-------------------------------------------------------------------------------------------------------------------------------------------------------------------


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 said:
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

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
 
T

Tim Mackey

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



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
-------------------------------------------------------------------------------------------------------------------------------------------------------------------


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 said:
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

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
 
G

glbdev

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 said:
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



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
-------------------------------------------------------------------------------------------------------------------------------------------------------------------


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.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

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Name in pdf export dialog 3
export to PDF problem 2
Open a PDF in another window/tab 1
Export to pdf 1
PowerPoint Printing PPT to PDF without losing quality 1
Export from crystal report to pdf 4
.ASPX to PDF? 2
Access Access variable string for export 0

Top