Problem sending PDF as stream using iTextSharp

A

antonyliu2002

I have no problem writing PDF file to hard drive on the fly with
iTextSharp, but have had no luck in sending PDF as a stream in my
asp.net web application.

I am simply copying the example at

http://itextsharp.sourceforge.net/examples/Chap0105.cs

But, Adobe Acrobat reader complains that the file has been damaged or
cannot be opened, or says something like "if the file was sent as an
email attachment, it was not properly decoded" or something to that
effect.

At the www.asp.net forum, http://forums.asp.net/p/1093198/1643956.aspx#1643956
, someone says that we need to reset the value of the memory stream
position to 0. I tried that with no luck.

Any idea about how to shoot this problem? Thanks.
 
A

antonyliu2002

I have no problem writing PDF file to hard drive on the fly with
iTextSharp, but have had no luck in sending PDF as a stream in my
asp.net web application.

I am simply copying the example at

http://itextsharp.sourceforge.net/examples/Chap0105.cs

But, Adobe Acrobat reader complains that the file has been damaged or
cannot be opened, or says something like "if the file was sent as an
email attachment, it was not properly decoded" or something to that
effect.

At thewww.asp.netforum,http://forums.asp.net/p/1093198/1643956.aspx#1643956
, someone says that we need to reset the value of the memory stream
position to 0. I tried that with no luck.

Any idea about how to shoot this problem? Thanks.

I searched a little bit, and it looks like it's gonna be very hard to
send PDF as stream without saving it to file first. The workaround
would be to save it to file, and then redirect to that pdf file. When
the session expires, remove that pdf file to avoid accumulation of
junk files on the server.
 
G

Guest

Couple of ideas here:

1) if you are doing this from an ASP.NET Page, you need to ensure that no
content from the ASPX portion of the page is included. In other words, you
want ONLY the <@Page directive at the top and nothing else following.
2) Did you set the Response.ContentType header to the PDF Mime type? before
writing to the Response Output stream?
-- Peter
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
BlogMetaFinder(BETA): http://www.blogmetafinder.com
 
A

antonyliu2002

Couple of ideas here:

1) if you are doing this from an ASP.NET Page, you need to ensure that no
content from the ASPX portion of the page is included. In other words, you
want ONLY the <@Page directive at the top and nothing else following.
2) Did you set the Response.ContentType header to the PDF Mime type? before
writing to the Response Output stream?
-- Peter
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
BlogMetaFinder(BETA): http://www.blogmetafinder.com

I am not sure about your first point. I display something on
default.aspx web form, on which there is a button which says "export
to pdf".

About your 2nd point, yes, I did this:

Response.ContentType = "application/pdf";

As I said, I simply copied the example at

http://itextsharp.sourceforge.net/examples/Chap0105.cs

But, it does not work for me.
 
A

antonyliu2002

Couple of ideas here:

1) if you are doing this from an ASP.NET Page, you need to ensure that no
content from the ASPX portion of the page is included. In other words, you
want ONLY the <@Page directive at the top and nothing else following.
2) Did you set the Response.ContentType header to the PDF Mime type? before
writing to the Response Output stream?
-- Peter
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
BlogMetaFinder(BETA): http://www.blogmetafinder.com

Thank you so much Peter. I tried your method, and it works! Here is
what I did by following your idea.

1. Create another web form, call it ExportToPDF.aspx. Like what you
said, I have only the <% Page .... %> directive in the content page
(ExportToPDF.aspx).

2. In the code-behind, i.e., ExportToPDF.aspx.cs, I have the code to
create the PDF on the fly and send it as a stream, just like the
example of the iTextSharp tutorial.

3. Now in Default.aspx, when the "Export To PDF" button is clicked, I
simply redirect to "ExportToPDF.aspx".

Too bad, the iTextSharp tutorial does not mention this, and no
documentation about this seems to be available.
 
A

antonyliu2002

Thank you so much Peter. I tried your method, and it works! Here is
what I did by following your idea.

1. Create another web form, call it ExportToPDF.aspx. Like what you
said, I have only the <% Page .... %> directive in the content page
(ExportToPDF.aspx).

2. In the code-behind, i.e., ExportToPDF.aspx.cs, I have the code to
create the PDF on the fly and send it as a stream, just like the
example of the iTextSharp tutorial.

3. Now in Default.aspx, when the "Export To PDF" button is clicked, I
simply redirect to "ExportToPDF.aspx".

Too bad, the iTextSharp tutorial does not mention this, and no
documentation about this seems to be available.

As a follow-up, I am wondering which method is better in terms of
server performance/security.

1. Write the PDF to file and either redirect to that PDF file or give
the user a link to download that file, later on remove those PDF files
when the session expires (I tried removing the PDF files in
global.asax in Session_End with no luck).

2. Send the PDF as a stream to the end user. Suppose hundreds of
users try to generate PDF files at roughly the same time, will this
explode the memory of the server?

Any comments about this?
 

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

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top