Question

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

Was it helpful?

Solution

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:

  1. The Unicode code you want us E501, and you need to escape with \u before it.
  2. You must use double-quotes around your string to ensure \u gets picked up properly.
  3. .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" }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top