Strange issue with building PDF programmatically

6 posts, 0 answers
  1. Sandra Walters
    Sandra Walters avatar
    29 posts
    Member since:
    Dec 2005

    Posted 22 Mar 2007 Link to this post

    Hello!  I'm attempting to build a simple set of reports against a development project that is currently using SQL Reporting 2005, which has some shortcomings I think your product may handily overcome.... if I can only figure out what I'm doing wrong here.

    My web app needs to generate PDF reports and display them to the user.  Here's the code I'm trying to use against the telerik Reporting.  Note that I'm trying to pull the rendered PDF into a byte array and pass that array directly to the context.Response object.  (This is very similar to what I do with SSRS, which is working.)  Unfortunately, with the telerik report in the code below, when the Adobe Reader opens on the client's system, an error message pops up that reads "there was an error opening this document.  The file is damaged and could not be repaired."  (This was with Adobe Reader 7.0.8 by the way.)

    Note that there's some lines in the code below that are commented out - these are pulling the PDF into a FileStream and writing it to a file - if I try to open the file thus created, it works like a charm!!  (same version of the reader.)

    I'd like to avoid all that sloppy file writing... any ideas what in the world I'm doing wrong here????

    Oh, and thanks for letting us use the beta of what looks to be a fabulous product... I was sold as soon as I realized the codebehind was C# and not some horrible VBA like what SSRS uses!

     

    JobList report = new JobList();  
    string mimeType = string.Empty;  
    string extension = string.Empty;  
    Encoding encoding = null;  
     
    byte[] RenderedRptArr = Telerik.Reporting.Processing.ReportProcessor.Render("PDF", report, null, out mimeType, out extension, out encoding);  
     
    //FileStream sOut = new FileStream("c:\\dev\\joblist.pdf", FileMode.CreateNew, FileAccess.Write);  
    //sOut.Write(RenderedRptArr, 0, RenderedRptArr.Length);  
    //sOut.Close();  
     
     
     
     
    System.Web.HttpContext context = System.Web.HttpContext.Current;  
     
    string strPath = context.Request.ApplicationPath;  
    Response.AppendHeader("content-disposition", "attachment; filename=JobList.pdf");  
     
    Response.Clear();  
    Response.ContentType = "application/pdf";  
    context.Response.BinaryWrite(RenderedRptArr);  
    Response.Flush();  
     

     

  2. Chavdar
    Admin
    Chavdar avatar
    898 posts

    Posted 26 Mar 2007 Link to this post

    Hi Sandra,

    Thanks for the interest and the kind words about our product. Any comments or suggestions that may help us improve it are highly appreciated.

    To export a PDF file from a web application you can use the function bellow. I believe that the problem is that you don't call the Response.End(); method after writing the PDF bytes into the Response stream.

    void ExportToPDF(string reportName, Telerik.Reporting.Report reportToExport) 
        { 
            string mimeType = string.Empty; 
            string ext = string.Empty; 
            Encoding encoding = Encoding.Default; 
     
            byte[] reportBytes = Telerik.Reporting.Processing.ReportProcessor.Render( 
                "PDF" 
                , reportToExport 
                , null 
                , out mimeType 
                , out ext 
                , out encoding); 
     
             
            string fileName = reportName + ".pdf"
     
            Response.Clear(); 
            Response.ContentType = mimeType; 
            Response.Cache.SetCacheability(HttpCacheability.Private); 
            Response.Expires = -1; 
            Response.Buffer = false
     
            Response.AddHeader("Content-Disposition"
                               string.Format("{0};FileName=\"{1}\""
                                             "attachment"
                                             fileName)); 
     
            Response.OutputStream.Write(reportBytes, 0, reportBytes.Length);         
            Response.End(); 
        } 


    Greetings,
    Chavdar
    the telerik team

    Instantly find answers to your questions at the new telerik Support Center
  3. DevCraft banner
  4. Sandra Walters
    Sandra Walters avatar
    29 posts
    Member since:
    Dec 2005

    Posted 26 Mar 2007 Link to this post

    Great, adding Response.End() after the Flush() call fixed it!  Thanks for catching that for me.

    Sandra
  5. Chavdar
    Admin
    Chavdar avatar
    898 posts

    Posted 27 Mar 2007 Link to this post

    Hi Sandra,

    It's good news that the problem is solved. We are glad to be able to help.

    Please, note, that there is no need to explicitly call Reponse.Flush() before Response.End() as it is called from the latter method.

    Best wishes,
    Chavdar
    the telerik team

    Instantly find answers to your questions at the new telerik Support Center
  6. Phil
    Phil avatar
    84 posts
    Member since:
    Apr 2005

    Posted 29 Aug 2007 Link to this post

    "Curiouser and curiouser" said Alice.

    I have the same error coming up, but it seems to only affect specific versons of Adobe Reader. My PDF's will open in 7.0.7, but not 7.0.5 or 7.0.9. Version 8.0 onwards seem fine.

    I have the Response.End call in my code:
        Private Sub ExportToPDF(ByVal reportName As String, ByVal reportToExport As Telerik.Reporting.Report)  
            ' Takes a telerik report and streams it to PDF file.  
            ' PDF files are being used in effect as a "print preview".  
            ' There is an outstanding issue here with the size of the PDF file generated, which is quite large.  
            Dim mimeType As StringString = String.Empty  
            Dim ext As StringString = String.Empty  
            Dim encoding As Encoding = encoding.Default  
     
            Dim reportBytes As Byte() = Telerik.Reporting.Processing.ReportProcessor.Render("PDF", reportToExport, Nothing, mimeType, ext, encoding)  
            Session.Item("pdfsize") = reportBytes.Length  
     
            Dim fileName As String = reportName + ".pdf"  
            'WriteToFile(Server.MapPath("~\temp\" & fileName), reportBytes)  
     
            Response.Clear()  
            Response.ContentType = mimeType 
            Response.Cache.SetCacheability(HttpCacheability.Private)  
            Response.Expires = -1  
            Response.Buffer = False 
     
            Response.AddHeader("Content-Disposition", String.Format("{0};FileName=""{1}""", "attachment", fileName))  
     
            Response.OutputStream.Write(reportBytes, 0, reportBytes.Length)  
            Response.End()  
    End Sub 

    As with Sandra, I can save the "damaged" PDF on the network and read it using a different version of Adobe Reader with no problem. I'm not going to lose too much sleep as if anyone hits this problem I'll just ask them to upgrade to 8.x, but in the interest of professionalism a clean resolution would be nice!


  7. Rossen Hristov
    Admin
    Rossen Hristov avatar
    2478 posts

    Posted 29 Aug 2007 Link to this post

    Hello Phil,

    WIth all due respect, this does not seem to be a problem with our tool, but rather with the different versions of Adobe reader. We do advise our clients to use the latest version of Adobe Reader, although this is not always possible.
     
    Nevertheless, we will research what is causing this problem and will do the necessary to get it fixed.


    Best wishes,
    Rossen
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
Back to Top
DevCraft banner