Ruby Prawn PDF Prevent Header Space From Populating Next Page

Pan Wangperawong

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 [email protected]"
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

Gregory Brown

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Ruby Prawn PDF Prevent Header Space From Populating Next Page

From Dev

Prevent Next button from going to next page

From Dev

HTML prevent space bar from scrolling page

From Dev

Generating pdf from html using Prawn

From Dev

Render Prawn pdf and rqrcode display QR with Ruby on rails

From Dev

Render Prawn pdf and rqrcode display QR with Ruby on rails

From Dev

Ruby - prevent GzipWriter from adding metadata onto its header

From Dev

Prevent empty element from occupying space at bottom of page

From Dev

Space between top of page and header

From Dev

Prawn using a pdf template

From Dev

Attach Prawn pdf to email

From Dev

Prawn PDF & PUA unicode

From Dev

Populating a ruby array reading from a file

From Dev

Add a Header to Every Page in a PDF?

From Dev

Populating a page with data from a database in mvc 5

From Dev

Using Rotativa to generate PDF From a view when the page breaks data continue on the header of the second page disregarding CSS

From Dev

Using Rotativa to generate PDF From a view when the page breaks data continue on the header of the second page disregarding CSS

From Dev

How to show group header in next page if details is on the next page?

From Dev

Prevent page from loading

From Dev

Prevent page from exiting

From Dev

Prevent space and arrow keys scrolling web page?

From Dev

Generate PDF with Prawn and Pdfkit on RoR

From Dev

Changing the background color of a pdf with Prawn?

From Dev

Is there any way to prevent the next pages from changing when I edit spacings on current page?

From Dev

How to prevent form elements from pre-populating in Chrome

From Dev

TCPDF table header with different margin on next page

From Dev

variable is not going to next php page by header()

From Dev

Prawn: specify page for content to be placed

From Dev

css pdf page - header overlapping with content

Related Related

  1. 1

    Ruby Prawn PDF Prevent Header Space From Populating Next Page

  2. 2

    Prevent Next button from going to next page

  3. 3

    HTML prevent space bar from scrolling page

  4. 4

    Generating pdf from html using Prawn

  5. 5

    Render Prawn pdf and rqrcode display QR with Ruby on rails

  6. 6

    Render Prawn pdf and rqrcode display QR with Ruby on rails

  7. 7

    Ruby - prevent GzipWriter from adding metadata onto its header

  8. 8

    Prevent empty element from occupying space at bottom of page

  9. 9

    Space between top of page and header

  10. 10

    Prawn using a pdf template

  11. 11

    Attach Prawn pdf to email

  12. 12

    Prawn PDF & PUA unicode

  13. 13

    Populating a ruby array reading from a file

  14. 14

    Add a Header to Every Page in a PDF?

  15. 15

    Populating a page with data from a database in mvc 5

  16. 16

    Using Rotativa to generate PDF From a view when the page breaks data continue on the header of the second page disregarding CSS

  17. 17

    Using Rotativa to generate PDF From a view when the page breaks data continue on the header of the second page disregarding CSS

  18. 18

    How to show group header in next page if details is on the next page?

  19. 19

    Prevent page from loading

  20. 20

    Prevent page from exiting

  21. 21

    Prevent space and arrow keys scrolling web page?

  22. 22

    Generate PDF with Prawn and Pdfkit on RoR

  23. 23

    Changing the background color of a pdf with Prawn?

  24. 24

    Is there any way to prevent the next pages from changing when I edit spacings on current page?

  25. 25

    How to prevent form elements from pre-populating in Chrome

  26. 26

    TCPDF table header with different margin on next page

  27. 27

    variable is not going to next php page by header()

  28. 28

    Prawn: specify page for content to be placed

  29. 29

    css pdf page - header overlapping with content

HotTag

Archive