2

How can render HTML content from database in a prawn document

class PdfReport < Prawn::Document
  def initialize user
    super()
    text user.description #=> "<p>It's an html content</p>"
  end
end

The output i am getting from the above code is a string with HTML tags. I want something like html_safe. Is it possible in Prawn?

webster
  • 3,902
  • 6
  • 37
  • 59

1 Answers1

0

The html paragraph tag doesn't work in prawn. If you're looking for how to create a separation between paragraphs, see https://stackoverflow.com/a/49683036 which shows how to vertically measure text boxes (and formatted text boxes) and move the cursor accordingly.

But prawn can interpret some html tags such as bold:

s = "<b>It's an html content</b>"

text s # "<b>It's an html content</b>"

text s, :inline_format => true # It's an html content

adg
  • 552
  • 1
  • 6
  • 17
  • using `:inline_format => true`, it doesn't recognize special symbols. For example, I want to show pound currency symbol instead of £ Any idea how can I achieve? – Disha Jun 20 '18 at 12:53
  • 1
    I wish Prawn was better at supporting html notation. I think they have taken a stance that it's a problem for derivative projects to solve - very unfortunate. Just guessing but I would try copy and paste the symbol into your document rather than trying to represent it as an HTML entity. If you're generating it in rails make sure your encoding is UTF-8. – adg Jun 21 '18 at 20:19