michalwiacek
9/28/2018 - 10:48 AM

Create a PDF Document using Prawn Library with header, footer and page numbering.

Create a PDF Document using Prawn Library with header, footer and page numbering.

require "prawn"

Prawn::Document.generate("report.pdf") do

    10.times do
        start_new_page
    end

    repeat :all do
        move_down 50
        pad(20) { text "This padded both before and after" }
        pad(20) { text "This is an awesome page" }

        # header
        bounding_box [bounds.left, bounds.top], :width  => bounds.width do
            font "Helvetica"
            text "Here's My Fancy Header", :align => :center, :size => 25
            stroke_horizontal_rule
        end

        # footer
        bounding_box [bounds.left, bounds.bottom + 25], :width  => bounds.width do
            font "Helvetica"
            stroke_horizontal_rule
            move_down(5)
            text "And here's a sexy footer", :size => 16
        end
    end

    string = "page <page> of <total>"
    # Green page numbers 1 to 11
    options = { :at => [bounds.right - 150, 0],
     :width => 150,
     :align => :right,
     :page_filter => (1..11),
     :start_count_at => 1,
     :color => "007700" }
    number_pages string, options
end