Skip to content

Instantly share code, notes, and snippets.

@zinkkrysty
Created December 18, 2012 10:35
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zinkkrysty/4326973 to your computer and use it in GitHub Desktop.
Save zinkkrysty/4326973 to your computer and use it in GitHub Desktop.
Automatic padding of bounding box in Prawn, and allows for filling in the background color. Currently only when specifying height, but I'm open to suggestions.
require 'prawn'
module PaddedBoxHelper
#
# Fills the background of the box you're in
#
def fill_bg_color color
float do
fill_color color
fill_rectangle [0, bounds.height], bounds.width, bounds.height
reset_font_color
end
end
#
# Pads a bounding box by nesting another bounding_box inside it
# Can also take bg_color as a parameter (you have to specify height for this to work)
# Syntax:
# padded_box([x,y], <padding:Integer>, <opts...>, :bg_color => <hex:String>) do
# <your block content here>
# end
#
def padded_box *args, &block
opt = args.extract_options!
bg_color = opt.delete(:bg_color)
padding = args.delete_at(1)
bounding_box *args, opt do
fill_bg_color bg_color if bg_color
bounding_box [padding, bounds.height-padding], width: bounds.width-2*padding, height: bounds.height-2*padding do
block.call
end
end
end
end
@mzahor
Copy link

mzahor commented Oct 14, 2013

great gist! saved me a lot of time!

@jrna
Copy link

jrna commented Jul 31, 2015

Thank you 👍

@renemeye
Copy link

renemeye commented Aug 6, 2015

Good Job! Thank you

@ruinunes
Copy link

ruinunes commented Jan 6, 2016

Can't find reset_font_color in the prawn api. Are you sure?

@jschee
Copy link

jschee commented Mar 30, 2020

Bro, this gist is super clutch. Kudos and thankyou!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment