如何在红宝石中设置Prawn的边距?

Tim*_* T. 16 pdf-generation ruby-on-rails prawn

这是我到目前为止,但我需要设置边距:

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
Run Code Online (Sandbox Code Playgroud)

Geo*_*son 24

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]
Run Code Online (Sandbox Code Playgroud)

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

  • `[0.5英寸]`是给定属性的默认设置.所以,如果你没有为`:margin`明确设置一个值,它将在所有4个边上默认为半英寸.如果要覆盖任何默认值,则应提供与所需点数对应的数字.每英寸有72个点.这有点令人困惑,因为默认值以英寸为单位,而您以磅为单位设置值.如果你认为默认值是"[36分]",这可能更有意义 (4认同)

Hai*_*ter 5

只是在这里添加知识万神殿,但如果您来这里是想使用 Prawn Label gem 来完成此操作,则无法以这种方式设置文档边距。你必须解决这个问题。这是一个快速灵活的代码片段,用于创建带有位于文档边界框内的统一垫的边界框。

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
Run Code Online (Sandbox Code Playgroud)

如果您使用的是 Prawn 的隐式版本,请从 .bounding_box 和 .bounds 中删除 pdf。