5

Is it possible to have Prawn fill in check boxes depending on the result of a boolean? For example, I'd like to have a 'Yes' and a 'No' checkbox for a series of questions to record responses. Upon receiving the information, I'd like appropriate checkbox filled in. Is this possible, and if so, how?

Thanks!

Ryan
  • 414
  • 1
  • 5
  • 16

3 Answers3

4

It's not hard to draw this yourself. This has been a pretty flexible solution for me.

def checkbox(flag, x_position = 7, y_position = @pdf.cursor - 2)
  @pdf.bounding_box([x_position, y_position], width: 10, height: 12) do
    @pdf.stroke_bounds
    @pdf.text("X", align: :center, valign: :center) if flag
  end
end
sockmonk
  • 4,195
  • 24
  • 40
3

There is a gist here that discusses this. The basic idea though is you would need to use a font set that has support for checkboxes to get this to work.

If you want anything fancier than that, prawn does not support checkboxes out of the box, so you would need to create something for this.

bigtunacan
  • 4,873
  • 8
  • 40
  • 73
  • 1
    PDF viewers are only [guaranteed to have 14 fonts](http://www.enfocus.com/manuals/ReferenceGuide/PP/10/enUS/en-us/concept/c_aa1140975.html) and unicode support on pdf [is only so-so at best](http://stackoverflow.com/a/143702/616644). The most portable solution would probably be drawing them yourself. – Rick Smith Jul 23 '14 at 22:40
2

Prawn 2.2.2 (maybe earlier) comes with the ZapfDingbats font built-in. This means that if you produce text and use font ZapfDingbats, then you would only need to pass a text value of "3" (which is ASCII 51 which happens to be ZapfDingbats' check mark).

For instance, in my app I made some table cell content like this:

@pdf.make_cell(content: "3", font: "ZapfDingbats", width: 10)

And that produces a checkmark.

A lower case "o" will produce an unchecked box.

Hope that helps.