每个页面中的页脚在rails 3.2中都不起作用(Prawn PDF)

时间:2013-03-04 07:51:42

标签: ruby-on-rails pdf pdf-generation prawn prawnto

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

我想知道为什么我的页脚在每个页面都不起作用。

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

这是我的代码:

(我对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

解决!

看看我在下面发现了什么。

5 个答案:

答案 0 :(得分:3)

您好我已经找到了答案。

对于那些遇到这样麻烦的人:

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

答案 1 :(得分:3)

我尝试在每个页面上,在底部边距之外生成左/中/右 - 内容
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

湾您可以在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

答案 2 :(得分:1)

如果您不需要显示水平线分隔符,那么这是最小的解决方案,因为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

答案 3 :(得分:1)

试试这个

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

答案 4 :(得分:0)

试试这个:

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所有但是我用它工作,我们可以自由地分配它的位置,而且很简单。

相关问题