大虾-如何将pdf文档中常见的东西概括成一个单独的模块以供代码复用?

发布于 2024-09-28 06:06:56 字数 1182 浏览 8 评论 0原文

我正在使用 Rails Prawn 生成 pdf 文件。现在我能够生成包含我需要的所有必要内容的 pdf(例如表格、页眉、页脚、徽标、边框等...)。现在我需要在单独的模块内的方法中使用常见的东西(标题,页脚,边框)并从我的原始程序中调用此方法?

我原来的程序: 旅行报告.rb 模块 TravelReport

包括标题 def self.generate(start_ts, end_ts, format_type, obu_ids, 间隔) Prawn::Document.generate("public/test.pdf") 做

边框

page_count.times do |i|
go_to_page(i+1)
Header.border
end

页眉和边界线

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

页脚

 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 

单独的边框模块 模块头 #class Cheader <大虾::文档::BoundingBox #包括虾 def self.border

      pdf = Prawn::Document.new
      pdf.bounding_box([5, 705], :width => 540, :height => 680) do
         pdf.stroke_bounds
        end

end
#end

end

此代码不会创建任何边框...知道如何为此创建单独的模块吗???

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
         pdf.stroke_bounds
        end

end
#end

end

This code doesnt creates any border... Any idea how to create separate module for this????

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦途 2024-10-05 06:06:56
#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

这工作正常...

#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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文