页脚在每个页面不工作(Prawn PDF)在rails 3.2



我已经在rails 3应用程序中安装了prawn gem。

我想知道为什么我的页脚不能在每个页面上工作。

它只在最后一页的末尾工作。

下面是我的代码:

(I am very new to Prawn)

所以请原谅我。

任何解决方法都将非常感谢

require 'prawn'
require 'rubygems'
require 'date'
pdf = Prawn::Document.new(:page_layout => :landscape,:skip_page_creation => true,:margin => [5,5,5,5]) do
    start_new_page
    pdf.font "Helvetica"    
end

pdf.text "Project Procurement Management Plan (PPMP)", :size=> 12, :spacing => 4, :align=> :center
pdf.move_down 400

pdf.text "Text"
pdf.text "Text"
pdf.text "Text"
pdf.text "Text"
pdf.text "Text"
pdf.text "Text"
pdf.text "Text"
pdf.text "Text"
pdf.text "Text"
pdf.text "Text"
pdf.text "Text"
pdf.text "Text"
pdf.text "Text"
pdf.text "Text"
pdf.text "Text"
pdf.text "Text"
pdf.text "Text"
pdf.text "Text"
pdf.text "Text"
pdf.text "Text"

# This is the footer code
pdf.bounding_box([pdf.bounds.right - 59, pdf.bounds.bottom - -20], :width => 60, :height => 20) do
  pagecount = pdf.page_count
  pdf.text "Page #{pagecount}"
end

解决!

嗨,我已经找到答案了。

对于那些有这样麻烦的人:

pdf.repeat :all do
  pdf.bounding_box [pdf.bounds.left, pdf.bounds.bottom + 25], :width  => pdf.bounds.width do
  pdf.stroke_horizontal_rule
  pdf.move_down(5)
  pdf.text "#{current_user.first_name}", :size => 10
end
end

我尝试在每一页的底部边距之外生成left/center/right - content
number_pages只生成一个区块。
几种方法:

。使用repeattext_box。这里重要的是,text_box需要一个显式的高度来显示在边界之外:

repeat :all do
  text_box "title: some text", at: [bounds.left, -20], height: 20
  text_box "printet at #{Time.now}", height: 20, width: 200, align: :right, at: [bounds.right-200,-20]
end

b。您可以在PDF生成结束时多次调用number_pages。您不需要提供页码模板。number_pages可以在边界外生成文本(它在内部设置高度):

number_pages "title: some text", at: [bounds.left, -20]
number_pages "page <page> of <total>", at: [300,-20]
number_pages "printed at #{Time.now}", at: [bounds.right-200,-20], width: 200, align: :right

如果您不需要显示水平线分隔符,那么这是最小的解决方案,因为pdf.number_pages即使没有pdf.repeat(:all)也可以工作:

options = {
        :at => [pdf.bounds.right - 150, 0],
        :width => 150,
        :align => :right,
        :start_count_at => 1
}
pdf.number_pages "Page <page> of <total>", options

试试这个

pdf.page_count.times do |i|
  pdf.go_to_page(i+1)
  pdf.bounding_box([0, pdf.bounds.bottom + 25], :width => pdf.bounds.width) {
    pdf.line_width(3)
    pdf.stroke_horizontal_rule
    pdf.move_down(5)
    pdf.draw_text "Page #{i+1}" ,:at => [240, pdf.bounds.height - 20]
    pdf.draw_text "#{Time.now.strftime("%B %d, %Y")}" ,:at => [455, pdf.bounds.height - 20]
  }
end

试试这个:

pdf.repeat(:all) do
  pdf.stroke do
    pdf.horizontal_line 0, 540, :at => 10
  end
  pdf.number_pages "(<page>/<total>)", :size => 9, :at => [500, 0]
end

所以我的理解,我需要使用这个number_pages来创建它,基本上我所做的不是干净和正确的,因为创建页眉和页脚我使用所有的number_pages,但我的工作,因为我们可以自由地分配它的位置,这很简单。

最新更新