Question

I am using Prawn to generate a pdf receipt. Ideally I would like the following format:

  • Header
  • Customer Info
  • Items for order
  • Total

However when it goes to the second page there is a huge gap on the top from the Header spacing. How can I prevent it from adding the extra spacing on top?

Here is the code generating the receipt below (Sorry there is a lot of code):

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

Below is how the receipt looks like. enter image description here

Était-ce utile?

La solution

For your flowing text that is beneath the header, you can use a span rather than a bounding_box, and it will start at the top of the page. There are some examples in the manual, just search for span in there:

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

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top