Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Problem sending PDF as stream using iTextSharp

Reply
Thread Tools

Problem sending PDF as stream using iTextSharp

 
 
antonyliu2002@yahoo.com
Guest
Posts: n/a
 
      07-11-2007
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.

 
Reply With Quote
 
 
 
 
antonyliu2002@yahoo.com
Guest
Posts: n/a
 
      07-11-2007
On Jul 11, 12:31 pm, "(E-Mail Removed)"
<(E-Mail Removed)> wrote:
> 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.

 
Reply With Quote
 
 
 
 
=?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
Guest
Posts: n/a
 
      07-11-2007
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



"(E-Mail Removed)" wrote:

> On Jul 11, 12:31 pm, "(E-Mail Removed)"
> <(E-Mail Removed)> wrote:
> > 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.
>
>

 
Reply With Quote
 
antonyliu2002@yahoo.com
Guest
Posts: n/a
 
      07-11-2007
On Jul 11, 3:12 pm, Peter Bromberg [C# MVP]
<(E-Mail Removed)> wrote:
> 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.


>
> "(E-Mail Removed)" wrote:
> > On Jul 11, 12:31 pm, "(E-Mail Removed)"
> > <(E-Mail Removed)> wrote:
> > > 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.


 
Reply With Quote
 
antonyliu2002@yahoo.com
Guest
Posts: n/a
 
      07-11-2007
On Jul 11, 3:12 pm, Peter Bromberg [C# MVP]
<(E-Mail Removed)> wrote:
> 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.


 
Reply With Quote
 
antonyliu2002@yahoo.com
Guest
Posts: n/a
 
      07-11-2007
On Jul 11, 4:06 pm, "(E-Mail Removed)"
<(E-Mail Removed)> wrote:
> On Jul 11, 3:12 pm, Peter Bromberg [C# MVP]
>
> <(E-Mail Removed)> wrote:
> > 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.


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?

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
itextsharp pdf generation, insert image header. guoqi zheng ASP .Net 4 10-13-2011 05:47 PM
Using itextsharp to generate pdf from asp.net =?Utf-8?B?U2Vhbg==?= ASP .Net 5 07-16-2008 01:03 PM
Pdf generation by iTextSharp library Viswagreypad Software 2 10-24-2007 06:55 AM
Creating PDF in ASP.NET - iTextSharp Neven Klofutar ASP .Net 1 05-10-2007 09:42 PM
Using iTextSharp for PDF Creation... Nique ASP .Net 0 08-17-2004 10:23 AM



Advertisments