I want to display HTML in RDLC of reporting service. Report Viewer support html but with limited tags, I want to display HTML generated from rich text editor. After investigation I found the best solution is displaying it as image. Steps to do that:
1. Create class library convert HTML to image.
2. Use this class with report viewer.
3. Display report with asp net.
1. Create class library convert HTML to image
a. In the following url http://webapplication02.blogspot.com/2011/01/convert-html-to-image.html you find the way to create class library to convert html to image.
It is better to create image as bmp for better image quality but this will not work with excel.
You can convert image to another type but check its quality.
b. Need to add [assembly: AllowPartiallyTrustedCallers()] to trust class library for using by report viewer.
2. Use Class with report viewer:
a. Replace your html field in report with Image with properties:
i. Mime type image/bmp
ii. Type of field Database
iii. Field Expression =Code.GetHtmlImage(HtmlField)
b. Add this function to report:
Public Function GetHtmlImage(ByVal body as String) As Byte() return htmlConv.getWebPageBytes(body, nothing, nothing) End Function |
c. Add Assembly reference to 3 dlls:
i. System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
ii. System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
iii. Your class library
d. Add class library ImageConv class and instance htmlConv
e. To build your solution with report reference to your class library copy your class library to folder:
($Program Files Path)\Microsoft Visual Studio 10.0\Common7\IDE\PublicAssemblies
3. Display Report with ASPNET:
ReportViewer1.LocalReport.ExecuteReportInCurrentAppDomain(System.Reflection.Assembly.GetExecutingAssembly().Evidence); ReportViewer1.LocalReport.AddTrustedCodeModuleInCurrentAppDomain("System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"); ReportViewer1.LocalReport.AddTrustedCodeModuleInCurrentAppDomain("System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"); ReportViewer1.LocalReport.AddTrustedCodeModuleInCurrentAppDomain(Full Assembly name of your class libarary); |
Also need to add Full Trust in Web.Config