20

This is what I have so far, but I need to set margins:

def send_fax 
    contact = Contact.find_by_id(self.contact_id)

    pdf = Prawn::Document.new
    pdf.font "Times-Roman"
    pdf.move_down(20)
    pdf.text "ATTN: #{contact.first_name} #{contact.last_name}", :size => , :style => :bold
    pdf.text "RE: #{self.subject}"
    pdf.move_down(20)

    pdf.text "#{self.body}"

    OutboundMailer.deliver_fax_email(contact, self, pdf)

  end
Bastien Léonard
  • 60,478
  • 20
  • 78
  • 95
Satchel
  • 16,414
  • 23
  • 106
  • 192

2 Answers2

31

Prawn::Document.new( :margin => [0,0,0,0] )

:margin:    Sets the margin on all sides in points [0.5 inch]
:left_margin:   Sets the left margin in points [0.5 inch]
:right_margin:  Sets the right margin in points [0.5 inch]
:top_margin:    Sets the top margin in points [0.5 inch]
:bottom_margin: Sets the bottom margin in points [0.5 inch]

http://rdoc.info/github/sandal/prawn/master/Prawn/Document

George Anderson
  • 972
  • 8
  • 14
  • thanks, I couldn't follow the instructions, you made it much clearer – Satchel Jan 05 '11 at 16:32
  • How did you figure this out, I checked the document and I still can't se what to do...should I just set :margin => [0.5,0.5,0.5,0.5] if it is 0.5 inch all around? Do I need to use inch? [0.5 inch]? – Satchel Jan 11 '11 at 02:42
  • 5
    `[0.5 inch]` is the default setting for the given attribute. So, if you don't explicitly set a value for `:margin`, it will default to a half inch on all 4 sides. If you want to override any of the default values, you should provide a number that corresponds to the number of points you want. There are 72 points per inch. It's a little confusing since the default value is provided in inches, while you set the value in points. It may make more sense if you think of the default value as being `[36 points]` – George Anderson Jan 13 '11 at 03:59
  • 1
    Just for the sake of clarity, the syntax above works without the trailing colon. For example, `:margin` instead of `:margin:`. Also, this link will take a person straight to that syntax: http://rdoc.info/github/sandal/prawn/master/Prawn/Document:initialize – Tass Jan 28 '14 at 15:10
5

Just adding to the pantheon of knowledge here, but in case you came here looking to do this using the Prawn Label gem, you can't set the document margin this way. You'll have to do a work around. Here's a quick and flexible snippet for creating a bounding box with a uniform pad that sits inside the document bounding box.

pad = 5

pdf.bounding_box([pad, pdf.bounds.height - pad], :width => pdf.bounds.width - (pad * 2), :height => pdf.bounds.height - (pad * 2)) do
    #Draw all your content in this block and it will be padded uniformly by 5
end

Remove the pdf from .bounding_box and .bounds if you're using an Implicit version of Prawn.

Hairgami_Master
  • 5,429
  • 10
  • 45
  • 66