Skip to content Skip to sidebar Skip to footer

Creating Custom Ssrs Handler For Field With Html

I have an SSRS 2008 report with a field that contains and is configured to render as HTML. Some of the text in this field may contain IMG tags, and the IMG tag is not among the tag

Solution 1:

EDIT: I have created and implemented this solution within the SSRS development environment and it works. WOOHOO :) It did require some hoop-jumping with creating a Single-Threaded Apartment thread to host the WebBrowser control, and to create a message pump, but it does work! **

As I was literally typing up the message to a co-worker that this issue was a non-starter, I did have a bit of an inspiration on a way to solve this problem. I know this post hasn't generated a great deal of response, but just in case someone else finds themselves in a similar problem, I'm going to share what I've implemented in a "petri dish" scenario that, provided I get all the code permission issues resolved, should allow me a decent solution to this problem.

With SSRS inability to handle an IMG tag insurmountable, I actually thought of an idea that took the HTML rendering away from SSRS entirely. To do this, I created custom code that hands off the HTML rendering to a WebBrowser control, then copies the rendered result as an image. It does the following:

  1. Instantiates a WebBrowser control of a given width and height.
  2. Sets the DocumentText property of that control to the HTML from TinyMCE
  3. Waits for the DocumentText to completely render.
  4. Creates a bitmap equal to the size of the control.
  5. Uses the undocumented and presumably unsupported DrawToBitmap method of the WebBrowser to draw the rendered HTML to a bitmap.
  6. Copies the Bitmap to an Image
  7. Saves the Image as a .png file
  8. Returns the path to the .png as the result of the function.

In SSRS, I plan to replace the erstwhile HTML text field with an external Image control that will then call the above method and render the image file. I may alter that to simply draw the image to the SSRS Image control directly, but that's a final detail I'll resolve later. I think this basic design is going to work. Its a little kludgey, but I think it will work.

I have some permissions issues to work out with the code that SSRS will allow me to call at runtime, but I'm confident I'll get those sorted out (even if I end up moving the code to a separate assembly). Once this is tested and working, I plan to mark this as the answer.

Thanks to those who offered suggestions.

Solution 2:

I've done something similar with success: We had an HTML "Comment" field that was collected on a web form. For a particular report we wanted to truncate this field to the first 1000 characters or so, but preserve valid HTML.

So I created a C# .dll & class with a public function:

publicstaticstringTruncateHtml(string html, int characters)
{
   ...
}

(I used the HtmlAgilityPack for most of the HTML parsing, and to create and close off my new HTML string, while I kept track of the content length.)

Then I could call that code with the fully qualified path to the function in an SSRS expression:

=ReportHtmlHandler.HtmlTruncate.TruncateHtml(Fields!Comment.Value,1000)

I could have added a calculated field to my dataset with this, but I was only using this value for one field, so I kept it at the field expression level.

All of this code gets called well before the HTML is processed or rendered by SSRS. I'm sure that any original IMG tag will be in the string.

This approach might work for you, possibly create a ExtractImg function which could be set as the source of an img on the report. I think some of the tricky bits for your requirement will be to handle multiple images as well as embedding the extracted img. But you might be able to do this simply with a external reference to an image. I haven't done much with external images in SSRS.

An MSDN blog entry on calling a custom dll from SSRS: http://support.microsoft.com/kb/920769

Post a Comment for "Creating Custom Ssrs Handler For Field With Html"