This is a migrated thread and some comments may be shown as answers.

Export to HTML

13 Answers 833 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Scott R
Top achievements
Rank 1
Scott R asked on 17 Jun 2008, 07:16 PM

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.

13 Answers, 1 is accepted

Sort by
0
Chavdar
Telerik team
answered on 18 Jun 2008, 01:55 PM
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
0
Scott
Top achievements
Rank 1
answered on 20 Jun 2008, 04:25 AM
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?
0
Chavdar
Telerik team
answered on 20 Jun 2008, 11:11 AM
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
0
Scott
Top achievements
Rank 1
answered on 20 Jun 2008, 02:49 PM
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

0
Chavdar
Telerik team
answered on 23 Jun 2008, 08:54 AM
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
0
Scott R
Top achievements
Rank 1
answered on 24 Jun 2008, 09:32 PM
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.
0
Chavdar
Telerik team
answered on 25 Jun 2008, 12:18 PM
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
0
Scott
Top achievements
Rank 1
answered on 25 Jun 2008, 02:21 PM
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?
0
Steve
Telerik team
answered on 25 Jun 2008, 03:40 PM
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
0
Steven
Top achievements
Rank 1
answered on 20 Dec 2019, 03:23 PM

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));

0
Ivan Hristov
Telerik team
answered on 25 Dec 2019, 09:10 AM

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
0
Mukesh
Top achievements
Rank 1
answered on 16 Feb 2021, 05:41 AM

I tried the same with .Net core, MHTML rendering not working?

Getting error "MHTML rendering format is not available."

0
Dimitar
Telerik team
answered on 16 Feb 2021, 10:54 AM

Hello Mukesh,

As we discussed in support ticket #1506785 MHTML rendering is indeed not working in .NET Core due to some limitations. If it is an option for you, you can try doing that in a .NET Framework based project.

Please let me know if you have any other questions.

Regards,
Dimitar
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Tags
General Discussions
Asked by
Scott R
Top achievements
Rank 1
Answers by
Chavdar
Telerik team
Scott
Top achievements
Rank 1
Scott R
Top achievements
Rank 1
Steve
Telerik team
Steven
Top achievements
Rank 1
Ivan Hristov
Telerik team
Mukesh
Top achievements
Rank 1
Dimitar
Telerik team
Share this question
or