Export to HTML

12 posts, 0 answers
  1. Scott R
    Scott R avatar
    63 posts
    Member since:
    Jun 2008

    Posted 17 Jun 2008 Link to this post

    How do I export a report to HTML? I can export to PDF, JPEG, and MHTML just fine, but not HTML. Here is the code I'm trying to use:

        public static byte[] GetInvoiceHTML(Guid id, HttpServerUtility Server)  
        {  
            // Get report object (with data).  
            Telerik.Reporting.Report rpt = GetInvoiceReport(id, Server);  
     
            // Render report object to HTML (byte array).  
            string mimeType = string.Empty;  
            string ext = string.Empty;  
            Encoding encoding = Encoding.Default;  
     
            byte[] bytes = Telerik.Reporting.Processing.ReportProcessor.Render("HTML", rpt, null, out mimeType, out ext, out encoding);  
     
            // Convert byte array to string.  
            return bytes;  
        } 

    The byte array returned is always 0 bytes.

  2. Chavdar
    Admin
    Chavdar avatar
    898 posts

    Posted 18 Jun 2008 Link to this post

    Hello Scott R,

    In contrast to the other export formats the HTML format has to be rendered with the second overload of the ReportProcessor.Render method. The reason is that almost always the html document consists of multiple streams which represent either pages or page resources such as images (e.g. shapes, charts and picturebox items are rendered as images). Given that you have to provide a CreateStream callback function in order to be able to handle all the possible streams created by the rendering extension.

    Usually instead of exporting to HTML it is preferable to export to MHTML which is also an HTML document but includes all the report pages and resources into a single file and thus can be represented by a single stream. However, if you still insist on exporting to HTML here is a sample code snippet which illustrates the approach. It is just a reference implementation so you can feel free to modify and improve it:

    class HtmlStream : MemoryStream 
        public string Name; 
        public string Extension; 
        public Encoding Encoding; 
        public string MimeType; 
     
            class HtmlStreams : List<HtmlStream> 
            {            
                public Stream CreateStreamCallback(string name, string extension, Encoding encoding, string mimeType) 
                { 
                    HtmlStream htmlStream = new HtmlStream(); 
                    htmlStream.Name = name; 
                    htmlStream.Extension = extension; 
                    htmlStream.Encoding = encoding; 
                    htmlStream.MimeType = mimeType; 
                    this.Add(htmlStream); 
                    return htmlStream; 
                } 
     
                public HtmlStream GetStream(string name) 
                { 
                    HtmlStream result = null
                    foreach (HtmlStream stream in this
                    { 
                        if (stream.Name == name) 
                        { 
                            result = stream; 
                            break
                        } 
                    } 
                    return result;                
                } 
            } 
     
            public byte[] GetInvoiceHTML(Report report) 
            { 
                // Get report object (with data).  
                Telerik.Reporting.Report rpt = GetInvoiceReport(id, Server);
     
                HtmlStreams htmlStreams = new HtmlStreams(); 
                Telerik.Reporting.Processing.ReportProcessor.Render("HTML", rpt, nullnew CreateStream(htmlStreams.CreateStreamCallback)); 
     
                MemoryStream stream = htmlStreams.GetStream("Page0"); 
                byte[] bytes = new byte[stream.Length]; 
                stream.Seek(0, SeekOrigin.Begin); 
                stream.Read(bytes, 0, bytes.Length); 
                return stream.ToArray(); 
                
            }  


    All the best,
    Chavdar
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  3. Scott
    Scott avatar
    23 posts
    Member since:
    Mar 2008

    Posted 19 Jun 2008 Link to this post

    I guess MHTML might work. I'm not very familiar with MHTML, so maybe I'm missing something obvious. I tried the code in this link (http://www.telerik.com/community/forums/thread/b311D-beatkt.aspx) but it doesn't work.

    Here's what I get:


    <>The XML page cannot be displayed

    Cannot view XML input using style sheet. Please correct the error and then click the Refresh button, or try again later.


    Invalid at the top level of the document. Error processing resource 'http://localhost:2612/Shared/PrintInvoice.aspx?id=8959...

    From: <Saved by Scott on SCOTT-NOTEBOOK>



    How do I get the MHTML to display correctly in the browser?
  4. Chavdar
    Admin
    Chavdar avatar
    898 posts

    Posted 20 Jun 2008 Link to this post

    Hi Scott,

    This could happen if you have removed the Response.AddHeader line from the sample code. If this is the case you have to set the Response.ContentType to "message/rfc822".

    Hope this helps.

    Best wishes,
    Chavdar
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  5. Scott
    Scott avatar
    23 posts
    Member since:
    Mar 2008

    Posted 20 Jun 2008 Link to this post

    Hi Chavdar,

    I don't want a file download, so I did remove the Content-Disposition: attachment header. I changed the Content-Type as you directed and now I get a different error message:

    Internet Explorer cannot display the webpage

    Most likely causes:

    • You are not connected to the Internet.
    • The website is encountering problems.
    • There might be a typing error in the address.

    Incidentally, the address bar now has "mhtml:" in front of it like this:

    mhtml:http://localhost:2612/Shared/PrintInvoice.aspx?id=8959fc9a-19c7-4114-bd1b-77dce52525ee&format=MHTML

    Firefox still gives me a file download dialog, but the file it downloads is NOT the MHTML.

    Would it be possible for you to provide a complete working example of exporting a report to MHTML and displaying it in the existing browser window? I searched the examples that installed with the product and didn't find "MHTML". Searches on your web site also just turn up forum posts with code snippets (that don't seem to work for me).

    I did notice that one forum post mentioned something about security in Vista might cause a problem. I am using VS2008 on Vista Home Premium, if that makes a difference.

    Thanks for all your help.

    Scott

  6. Chavdar
    Admin
    Chavdar avatar
    898 posts

    Posted 23 Jun 2008 Link to this post

    Hello Scott,

    Unfortunately the information in the web about this problem is not much. It seems that there is a security issue and for this reason the mhtml protocol is disabled in IE7. For more details you can take a look at the following security bulletin: Internet Explorer 7 "mhtml:" Redirection Information Disclosure

    Kind regards,
    Chavdar
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  7. Scott R
    Scott R avatar
    63 posts
    Member since:
    Jun 2008

    Posted 24 Jun 2008 Link to this post

    90% of my visitors use IE7, so that makes mhtml unusable.

    I've now gone about trying to use the report viewer, but it appears that it doesn't work inside an UpdatePanel. I started a new thread for that.
  8. Chavdar
    Admin
    Chavdar avatar
    898 posts

    Posted 25 Jun 2008 Link to this post

    Hello Scott R,

    We have answered your question in the other thread. Unfortunately there is nothing we can do about the mhtml format as the problem is not in our domain.

    All the best,
    Chavdar
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  9. Scott
    Scott avatar
    23 posts
    Member since:
    Mar 2008

    Posted 25 Jun 2008 Link to this post

    I was playing around with the report viewer last night and discovered that if I use its export function to create a "Web Archive" then IE7 displays it just fine. It's only if I create the MHTML in code that I have a problem.

    What is different about these two methods? Isn't "Web Archive" the same as MHTML? If not, how do I create a "Web Archive" in code?
  10. Steve
    Admin
    Steve avatar
    10940 posts

    Posted 25 Jun 2008 Link to this post

    Hi Scott,

    You are right about being able to open the mhtml file directly when prompted - it seems like this way we're bypassing the check responsible for not being able to open mhtml files. We tried locally to export directly in mhtml format and that did work for us - here is the code:

    protected void Page_Load(object sender, EventArgs e)
            {
               
                Report report = new Summary(); //our report class
                string mimeType;
                string ext;
                Encoding encoding;
                byte[] reportBytes = Telerik.Reporting.Processing.ReportProcessor.Render("MHTML", report, null, out mimeType, out ext, out encoding);
                string fileName = "Report1.mhtml";
                Response.Clear();
                Response.ContentType = "message/rfc822";
                Response.Cache.SetCacheability(HttpCacheability.Private);
                Response.Expires = -1;
                Response.Buffer = false;
                Response.OutputStream.Write(reportBytes, 0, reportBytes.Length);
                Response.End();            
            }

    Greetings,
    Steve
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  11. Steven
    Steven avatar
    6 posts
    Member since:
    Jul 2015

    Posted 20 Dec 2019 in reply to Chavdar Link to this post

    How can this example be modified to use a UriReportSource instead of the .Report? 

                                ReportProcessor reportProcessor = new ReportProcessor();
                                UriReportSource URS = new UriReportSource();
                                URS.Parameters.Add("reservationId", res.ReservationId);
                                URS.Uri = String.Format("../../Reporting/ReportsLibrary/{0}", String.Format(res.Location.ReceiptReport,"Summary"));
                                HtmlStreams htmlStreams = new HtmlStreams();
                                reportProcessor.RenderReport("HTML", URS, null, new CreateStream(htmlStreams.CreateStreamCallback));

  12. Ivan Hristov
    Admin
    Ivan Hristov avatar
    220 posts

    Posted 25 Dec 2019 Link to this post

    Hi Steven,

    Yes, the RenderReport method of the ReportProcessor class requires a ReportSource instance. UriReportSource inherits ReportSource, therefore it can be used. I just noticed in the snippet that RenderReport method misses the last parameter - documentName, which is an out parameter and will contain the name of the generated document once the rendering is finished. You can see a working example in our documentation here: Exporting a report to a multi document format.

    Regards,
    Ivan Hristov
    Progress Telerik

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top