Problem sending PDF as stream using iTextSharp

Discussion in 'ASP .Net' started by antonyliu2002@yahoo.com, Jul 11, 2007.

  1. Guest

    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.
     
    , Jul 11, 2007
    #1
    1. Advertising

  2. Guest

    On Jul 11, 12:31 pm, ""
    <> 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.
     
    , Jul 11, 2007
    #2
    1. Advertising

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



    "" wrote:

    > On Jul 11, 12:31 pm, ""
    > <> 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.
    >
    >
     
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=, Jul 11, 2007
    #3
  4. Guest

    On Jul 11, 3:12 pm, Peter Bromberg [C# MVP]
    <> 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.


    >
    > "" wrote:
    > > On Jul 11, 12:31 pm, ""
    > > <> 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.
     
    , Jul 11, 2007
    #4
  5. Guest

    On Jul 11, 3:12 pm, Peter Bromberg [C# MVP]
    <> 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.
     
    , Jul 11, 2007
    #5
  6. Guest

    On Jul 11, 4:06 pm, ""
    <> wrote:
    > On Jul 11, 3:12 pm, Peter Bromberg [C# MVP]
    >
    > <> 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?
     
    , Jul 11, 2007
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?U2Vhbg==?=

    Using itextsharp to generate pdf from asp.net

    =?Utf-8?B?U2Vhbg==?=, Feb 10, 2004, in forum: ASP .Net
    Replies:
    5
    Views:
    40,671
    mfreidge
    Jul 16, 2008
  2. Nique
    Replies:
    0
    Views:
    807
    Nique
    Aug 17, 2004
  3. Patrick Olurotimi Ige

    using itextsharp assembly with asp.net?

    Patrick Olurotimi Ige, Mar 23, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    1,566
    Brian Munroe
    Mar 23, 2005
  4. guoqi zheng
    Replies:
    4
    Views:
    12,205
    Levitikon
    Oct 13, 2011
  5. Neven Klofutar

    Creating PDF in ASP.NET - iTextSharp

    Neven Klofutar, May 10, 2007, in forum: ASP .Net
    Replies:
    1
    Views:
    929
    Neven Klofutar
    May 10, 2007
Loading...

Share This Page