Rails 3 + shrimp pdf + html_safe
I use pearls to create PDF reports,
@user.description returns as string "<b>sample text</b> &nspb; <p>sample text</p>" when adding values to pdf table
pdftable = Prawn::Document.new pdftable.table([["#{@user.description}"]], :column_widths => {0 => 50, 1 => 60, 2 => 280, }, :row_colors => ["ffffff"]) in this case, the generated PDF has content with html tags, even I tried to use html_safe, but it does not escape the tags.
Is it possible to use / apply html_safe inside prawn pdftable to avoid html tags?
Once again, html_safe is not a method that you should use; he does not do what you think he does. All html_safe makes the line mark as safe, thus telling Rails that it does not need to hide it in the view. When using shrimp this will have no effect.
It looks like what you want to do is not escape the HTML, but cut the HTML tags from the string. Rails has an HTML sanitizer in ActionView::Helpers::SanitizeHelper , but by default it allows certain tags; you can disable this behavior using the tags attribute.
class MyClass include ActionView::Helpers::SanitizeHelper def remove_html(string) sanitize(string, :tags => {}) # empty tags hash tells it to allow no tags end end obj = MyClass.new obj.remove_html "<b>sample text</b> &nspb; <p>sample text</p>" => "sample text &nspb; sample text" You can include ActionView::Helpers::SanitizeHelper in your controller to access the sanitize method.
Please note that still in line; if you want to remove these HTML objects, you will need to use a different method; The HTMLEntities gem is one of these methods:
[1] pry(main)> require 'htmlentities' => true [2] pry(main)> coder = HTMLEntities.new => #<HTMLEntities:0x007fb1c126a910 @flavor="xhtml1"> [3] pry(main)> string = "sample text sample text" => "sample text sample text" [4] pry(main)> coder.decode string => "sample text sample text" (note that in your example the text says &nspb; instead of ).