Reputation: 13
I'm trying to include a Private Use Area unicode in a Prawn document and can't manage to make it work.
I have looked at this thread Prawn:Print unicode string in PDF but can't make the raw work. I get this error:
pdf.rb:90:in `block in <main>': undefined method `raw' for #<Prawn::Document:0x007fa579adfef0> (NoMethodError)
Without the raw, it doesnt render the Glyphs.
font("#{Prawn::DATADIR}/fonts/icons-webfont.ttf") do
text raw "unicode_for_\e501"
end
Thanks
Upvotes: 0
Views: 918
Reputation: 26
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 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" }
}
Upvotes: 1