【问题标题】:how do I set margins in Prawn in ruby?如何在 ruby​​ 中设置 Prawn 的边距?
【发布时间】:2011-06-03 18:19:48
【问题描述】:

这是我目前所拥有的,但我需要设置边距:

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

【问题讨论】:

  • 值得注意的是,您正在寻找专门设置页边距。

标签: ruby-on-rails pdf-generation prawn


【解决方案1】:

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

【讨论】:

  • 谢谢,我无法按照说明进行操作,您说得更清楚了
  • 你是怎么想出来的,我检查了文档,但我仍然不知道该怎么做...我应该设置 :margin => [0.5,0.5,0.5,0.5] if周围是0.5英寸?我需要使用英寸吗? [0.5 英寸]?
  • [0.5 inch] 是给定属性的默认设置。因此,如果您没有明确设置:margin 的值,它将默认为所有 4 个边上的半英寸。如果您想覆盖任何默认值,您应该提供一个与您想要的点数相对应的数字。每英寸有 72 个点。这有点令人困惑,因为默认值以英寸为单位提供,而您以磅为单位设置值。如果您将默认值视为[36 points],则可能更有意义
  • 为了清楚起见,上面的语法在没有尾随冒号的情况下有效。例如,:margin 而不是 :margin:。此外,此链接将带人直接使用该语法:rdoc.info/github/sandal/prawn/master/Prawn/Document:initialize
【解决方案2】:

只是在这里添加知识的万神殿,但如果您来这里是为了使用虾标签 gem 来执行此操作,则不能以这种方式设置文档边距。你必须做一个工作。这是一个快速灵活的 sn-p 方法,用于创建一个带有统一填充的边界框,该填充位于文档边界框内。

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-19
    • 2022-10-21
    • 1970-01-01
    • 2012-09-16
    • 2017-12-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多