Prawn - how to generalize the common things in the pdf document into a separate module for code re-usage?
I am using the Rails Prawn to generate pdf files. Now that i am able to generate pdf with all necessary things that i need(eg. table, header, footer, logo, borders etc...). Now I need to use the common things(header, foooter, borders) in a method inside separate module and call this method from my original program?
My original program: travel_report.rb module TravelReport
include Header def self.generate(start_ts, end_ts, format_type, obu_ids, interval) Prawn::Document.generate("public/test.pdf") do
Borders
page_count.times do |i|
go_to_page(i+1)
Header.border
end
Header and Boundary Lines
page_count.times do |i|
go_to_page(i+1)
ask = "public/ashok.jpg"
image ask, :at => [15, 750], :width => 120
alert = "public/alert.jpg"
image alert, :at => [410, 740], :width => 120
end
footer
page_count.times do |i|
go_to_page(i+1)
lazy_bounding_box([bounds.left+30, bounds.bottom + 20], :width => 100) {
text "Bypass Report"
}.draw
end
end
Separate Module for Borders module Header #class Cheader < Prawn::Document::BoundingBox #include Prawn def self.border
pdf = Prawn::Document.new
pdf.bounding_box([5, 705], :width => 540, :height => 680) do
pd开发者_如何学Cf.stroke_bounds
end
end
#end
end
This code doesnt creates any border... Any idea how to create separate module for this????
#create a separate module
#program
include HeaderFooter
Prawn::Document.generate("public/test.pdf") do |pdf|
pdf.page_count.times do |i|
pdf.go_to_page(i+1)
HeaderFooter.border(pdf)
#render :partial => 'header', :locals => {:ppdf => pdf}
end
#Header and Boundary Lines
pdf.page_count.times do |i|
pdf.go_to_page(i+1)
HeaderFooter.image(pdf)
end
#footer
pdf.page_count.times do |i|
pdf.go_to_page(i+1)
HeaderFooter.footer(pdf)
end
end
create a module to define the methods(header_footer.rb)
module HeaderFooter
#Method for border creation in pdf
def self.border(ppdf)
ppdf.bounding_box([5, 705], :width => 540, :height => 680) do
ppdf.stroke_bounds
end
end
#method to create the logos in the pdf
def self.image(ppdf)
ask = "public/ashok.jpg"
ppdf.image ask, :at => [15, 750], :width => 120
alert = "public/alert.jpg"
ppdf.image alert, :at => [410, 740], :width => 120
end
#method to print footer text in the pdf
def self.footer(ppdf)
ppdf.lazy_bounding_box([ppdf.bounds.left+30, ppdf.bounds.bottom + 20], :width => 100) {
ppdf.text "Bypass Report"
}.draw
end
end
This works fine...
精彩评论