Created
January 6, 2013 13:16
-
-
Save jasonneylon/4467068 to your computer and use it in GitHub Desktop.
Create a form style layout with filled in values for a PDF using Prawn
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" | |
def form(pdf, fields) | |
pdf.float do | |
pdf.bounding_box([5, pdf.cursor], width: 250, height: 200) do | |
fields.keys.each do |label| | |
pdf.pad(5) { pdf.text label } | |
end | |
end | |
end | |
pdf.bounding_box([260, pdf.cursor], width: 250, height: 200) do | |
pdf.stroke_color "000000" | |
fields.values.each do |value| | |
pdf.fill_color "ffffff" | |
pdf.fill_and_stroke_rectangle([5, pdf.cursor], 240, 18) | |
fill_color "000000" | |
pdf.text_box value, at: [10, pdf.cursor - 5] | |
pdf.move_down 24 | |
end | |
end | |
end | |
Prawn::Document.generate("filledinform.pdf") do | |
move_down 5 | |
text "Application form", style: :bold, size: 25 | |
bounding_box([0,cursor], width: bounds.width, height: 120) do | |
fill_color "DCDCDC" | |
fill_rectangle [0, cursor], bounds.right, bounds.top | |
fill_color "000000" | |
transparent(0.5) { stroke_bounds } | |
move_down 10 | |
form_fields = { | |
"First name" => "John", | |
"Last name" => "Doe", | |
"Address line 1:" => "20 Shrimp lane", | |
"Address line 2:" => "" | |
} | |
form(self, form_fields) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment