Skip to content

Instantly share code, notes, and snippets.

@bradediger
Created February 5, 2009 18:17
Show Gist options
  • Select an option

  • Save bradediger/58890 to your computer and use it in GitHub Desktop.

Select an option

Save bradediger/58890 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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

ratbeard commented Jul 1, 2010

Copy link
Copy Markdown

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
Copy Markdown

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

@dylancashman

Copy link
Copy Markdown

Thanks a lot, this was a lot of help!

@walteryu

walteryu commented Aug 6, 2012

Copy link
Copy Markdown

Just what I was looking for - thanks for sharing!

@henrik

henrik commented Feb 11, 2013

Copy link
Copy Markdown

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

henrik commented Feb 11, 2013

Copy link
Copy Markdown

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