Ruby Prawn PDF防止标题空间填充下一页

时间:2014-01-23 01:55:08

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

我正在使用Prawn生成pdf收据。理想情况下,我想要以下格式:

  • 标题
  • 客户信息
  • 订购商品

然而,当它进入第二页时,标题间距顶部有一个巨大的间隙。如何防止它在顶部添加额外的间距?

下面是生成收据的代码(抱歉有很多代码):

class OrderPdf < Prawn::Document
def initialize(order)
    super()
    @order = order

    bounding_box([10, 700], :width => 500) do
        logo
    end
    message
    date_shipped
    order_number
    bounding_box([0, -70], :width => 2000) do
        bounding_box([65, 560], :width => 200) do
            shipping_address
        end

        bounding_box([320, 560], :width => 200) do
            payment_method
        end

        bounding_box([25, 450], :width => 500, :position => :center) do
            line_items
        end
    end

end

def logo
    image "#{Rails.root}/app/assets/images/letterhead-header.jpg" , :scale => 0.25, :position => :center
end

def message
    move_down 20
    text "Thanks for shopping with Dwellers. If you have any questions, please email support@dwellers.com"
end

def date_shipped
    move_down 10
    data = [["Shipment on #{@order.date_to_ship.strftime("%B %d, %Y")}",
        "Items: #{@order.order_items.count}",
        "Total: #{helpers.humanized_money(Money.new((@order.total_cents), "USD"), {:no_cents_if_whole => false, :symbol => true})}"]]

        table data do
            cells.style(:borders => [], :background_color => "DDDDDD", :valign => :center)
            column(0).font_style = :bold
            column(0).width = 400
            column(0).size = 18
        end
end

def order_number
    move_down 10
    # order_id = Digest::MD5.hexdigest "#{@order.id}#{@order.date_to_ship}"
    text "Order\# #{@order.number}", size: 14, style: :bold
end

def shipping_address
    move_down 10
    text "Shipping address:", size: 14, style: :bold
    indent(20) do
        text "#{@order.user.name} #{@order.user.name_last}"
        address_fields = @order.user.address.split(",")
        text "#{address_fields[0]}"
        if !address_fields[1].empty?
            text "#{address_fields[1]}"
        end
        text "#{address_fields[2]}, #{address_fields[3]} #{address_fields[4]}"
    end
end

def payment_method
    move_down 10
    @card = @order.user.card
    text "Payment Information:", size: 14, style: :bold
    if @card != nil
        indent(20) do
            text "#{@card[:card_type].capitalize} **** **** **** #{@card[:last_four]}"
            billing_address
        end
    end 
end

def billing_address
    move_down 10
        @billing_address = @order.user.billing_address.split
        address_fields = @order.user.billing_address.split(",")
        text "#{@card[:name]}"
        text "#{address_fields[0]}"
        if !address_fields[1].empty?
            text "#{address_fields[1]}"
        end
        text "#{address_fields[2]}, #{address_fields[3]} #{address_fields[4]}"

end

def line_items
    move_down 10
    text "In this shipment:", size: 14, style: :bold
    table line_item_rows + summary do
        row(0).font_style = :bold
        columns(1..3).align = :left
        self.header = true
        cells.style(:borders => [])
        column(0).width = 380

        row(row_heights().length-5).borders = [:bottom]
        row(row_heights().length-2..row_heights().length-1).column(1).font_style = :bold
        row(row_heights().length-1).column(1..2).size = 14
    end

end

def line_item_rows
    [["Item names", "Qty", "Price"]] +
    @order.order_items.map do |order_item|
        product = order_item.item.name + " - " +  order_item.item.description
        quantity = " x " +order_item.quantity.to_s
        [product, quantity, helpers.humanized_money(Money.new((order_item.price_paid_cents), "USD"), {:no_cents_if_whole => false, :symbol => true})]
    end
end

def summary
    # move_down 15
    data = [["", "", ""],
                    ["Subtotal:", "Shipping:", "Total:"],
                    [helpers.humanized_money(Money.new((@order.subtotal_cents), "USD"), {:no_cents_if_whole => false, :symbol => true}),
                    helpers.humanized_money(Money.new((@order.shipping_fee_cents), "USD"), {:no_cents_if_whole => false, :symbol => true}),
                    helpers.humanized_money(Money.new(@order.total_cents, "USD"), {:no_cents_if_whole => false, :symbol => true})]].transpose

end


# Need this to get access to View Helpers, such as number_to_currency
def helpers
  ActionController::Base.helpers
end

end

以下是收据的样子。 enter image description here

1 个答案:

答案 0 :(得分:3)

对于标题下方的流畅文字,您可以使用span而不是bounding_box,它将从页面顶部开始。手册中有一些示例,只需在其中搜索span

http://prawn.majesticseacreature.com/manual.pdf