Skip to content

Instantly share code, notes, and snippets.

@bradediger
Created February 5, 2009 18:17
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save bradediger/58890 to your computer and use it in GitHub Desktop.
Save bradediger/58890 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
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
@ratbeard
Copy link

ratbeard commented Jul 1, 2010

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!

@benfyvie
Copy link

And for when you go to Ruby 192 the Unicode codes are:
"\u2610" # "☐"
"\u2611" # "☑"
"\u2612" # "☒"

@dylancashman
Copy link

Thanks a lot, this was a lot of help!

@walteryu
Copy link

walteryu commented Aug 6, 2012

Just what I was looking for - thanks for sharing!

@henrik
Copy link

henrik commented Feb 11, 2013

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

@henrik
Copy link

henrik commented Feb 11, 2013

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