Created
February 5, 2009 18:17
-
-
Save bradediger/58890 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'prawn' | |
CHECKBOX = "\xE2\x98\x90" # "☐" | |
FILLED_CHECKBOX = "\xE2\x98\x91" # "☑" | |
Prawn::Document.generate("checkbox_test.pdf") do | |
# Use a font with the checkbox characters: | |
# http://www.fileformat.info/info/unicode/char/2610/fontsupport.htm | |
font "fonts/dejavu/ttf/DejaVuSans.ttf" | |
text "#{CHECKBOX} Not done yet" | |
text "#{FILLED_CHECKBOX} Complete!" | |
end |
And for when you go to Ruby 192 the Unicode codes are:
"\u2610" # "☐"
"\u2611" # "☑"
"\u2612" # "☒"
Thanks a lot, this was a lot of help!
Just what I was looking for - thanks for sharing!
With newer versions of Prawn, you'll get RuntimeError: Bad font family
from the above. Easy fix though: use the block version of font
:
font "fonts/dejavu/ttf/DejaVuSans.ttf" do
text "#{CHECKBOX} Not done yet"
end
Also, you can use formatted_text
so the label is in your default font:
EMPTY_CHECKBOX = "\u2610" # "☐"
CHECKED_CHECKBOX = "\u2611" # "☑" I don't use this, but you could.
EXED_CHECKBOX = "\u2612" # "☒"
CHECKBOX_FONT = "#{Prawn::BASEDIR}/data/fonts/DejaVuSans.ttf"
def checkbox(label, checked)
box = checked ? EXED_CHECKBOX : EMPTY_CHECKBOX
formatted_text [
{ text: box, font: CHECKBOX_FONT },
{ text: " #{label}" }
]
end
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For an X instead of a √:
FILLED_CHECKBOX = "\xE2\x98\x92" # "☒"
To get the font to load, I needed:
font("#{Prawn::BASEDIR}/data/fonts/DejaVuSans.ttf")
Nice work!