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
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, null, new 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
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?
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
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
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
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.
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
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?
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
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));
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
I tried the same with .Net core, MHTML rendering not working?
Getting error "MHTML rendering format is not available."
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/.