I am using prawn gem to generate PDF reports,
@user.description returns as string "<b>sample text</b> &nspb; <p>sample text</p>"
while appending values to the pdf table
pdftable = Prawn::Document.new
pdftable.table([["#{@user.description}"]],
:column_widths => {0 => 50, 1 => 60, 2 => 280, }, :row_colors => ["ffffff"])
in this case generated pdf has content with html tags, even i tried applying html_safe but it is not escaping tags.
is it possible to use/apply html_safe inside prawn pdftable, in order to escape html tags?
Once again,
html_safe
is not the method you should be using; it doesn't do what you think it does. Allhtml_safe
does is mark the string as safe, thus telling Rails that it does not need to escape it in a view. When using Prawn it would have no effect.What it sounds like you want to do is not escape HTML, but strip HTML tags from the string. Rails has an HTML sanitizer in
ActionView::Helpers::SanitizeHelper
, but by default it allows certain tags; you can turn this behavior off using thetags
attribute.You can
include ActionView::Helpers::SanitizeHelper
in your controller to get access to thesanitize
method.Note that the
is still in the string; if you want to remove these HTML entities, you'll need to use some other method; the HTMLEntities gem is one such method:(note that in your example, the text says
&nspb;
instead of
).If you are looking for a way to use prawn's inline format than you could also do as following: