The 'raw' method is not a Prawn method; it is a OutputSafetyHelper method from Rails. I get the same error as you (I am running the Prawn code in a model, not a view) but after looking at the source for #raw I realized all it was doing is calling String#html_safe. I replaced your original:
text raw "unicode_for_\e501"
with
text "unicode_for_\uE501".html_safe
Note the following:
- The Unicode code you want us E501, and you need to escape with \u before it.
- You must use double-quotes around your string to ensure \u gets picked up properly.
- .html_safe is a method that can be called on a string.
The double quotes and \u escapement are important, but I have found that (for my purposes) I did not need the .html_safe call, because I was able to print Unicode characters without it.
This is my text call, which loads a glyph icon:
pdf.font("vabicons") do
pdf.text "\uE61d"
end
For reference, I am loading a custom font from IcoMoon.io and all printable characters must be referenced by Unicode. Keep in mind that you need your font to be properly declared or else even properly formatted text may not be displayed.
This is my font declaration:
sym = Pathname.new( Rails.root.join('app', 'assets', 'stylesheets', 'fonts', 'vabicons.ttf').to_s )
pdf.font_families["vabicons"] = {
:normal => { :file => sym, :font => "Regular" }
}