Prawn中的页眉和页脚PDF

Nik*_* So 29 ruby-on-rails prawn prawnto

我已阅读了有关Prawn的所有相关帖子,但未发现(即使在Prawn自己的文档中)页眉和页脚.

但是,我确实在Prawnto自己的网站上看到了有关页眉和页脚的演示.我复制了该演示的整个源代码,只是为了查看它是否有效但是未定义方法"header"的错误被抱怨了.我是否理解Prawn最近在宝石中取出了页眉和页脚或者我还需要先做其他什么才能使用页眉和页脚?

演示页面:http: //cracklabs.com/prawnto/code/prawn_demos/source/text/flowing_text_with_header_and_footer

关注代码的一部分:

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

  header margin_box.top_left do 
      text "Here's My Fancy Header", :size => 25, :align => :center   
  end   

  text "hello world!"
end
Run Code Online (Sandbox Code Playgroud)

通过标题,以防万一,我的意思是通常出现在文档每个页面一角的单词片段.就像账单页面中的账号一样.

谢谢!

mem*_*cal 34

您从prawnto插件中引用的示例是使用较旧版本的prawn.

由于我还需要页眉和页脚,我看起来更多.似乎那个版本的虾有头和页脚方法,这些方法是使用惰性边界框实现的.(通过检查github上的代码找到)

在新的虾版本中,您可以使用中继器执行相同的操作.

以下是使用新版本重写的完整示例:

require "#{File.dirname(__FILE__)}/../example_helper.rb"

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

   repeat :all do
    # 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

  bounding_box([bounds.left, bounds.top - 50], :width  => bounds.width, :height => bounds.height - 100) do                 
   text "this is some flowing text " * 200    

   move_down(20)

   font "#{Prawn::BASEDIR}/data/fonts/DejaVuSans.ttf"
   table [["????? ??????",    "baaar",             "1" ],
          ["This is","a sample",          "2" ],
          ["Table",  "dont\ncha\nknow?",  "3" ],
          [ "It",    "Rules",             "4" ],     
          [ "It",    "Rules",             "4" ],     
          [ "It",    "Rules",             "4" ],     
          [ "It",    "Rules",             "4" ],     
          [ "It",    "Rules",             "4" ],     
          [ "It",    "Rules",             "4" ],     
          [ "It",    "Rules",             "4" ],     
          [ "It",    "Rules",             "4" ],     
          [ "It",    "Rules",             "4" ],     
          [ "It",    "Rules\nwith an iron fist", "x" ],     
          [ "It",    "Rules",             "4" ],     
          [ "It",    "Rules",             "4" ],     
          [ "It",    "Rules",             "4" ],     
          [ "It",    "Rules",             "4" ],     
          [ "It",    "Rules",             "4" ],     
          [ "It",    "Rules",             "4" ],     
          [ "It",    "Rules",             "4" ],   
          [ "It",    "Rules",             "4" ],     
          [ "It",    "Rules",             "4" ],     
          [ "It",    "Rules",             "4" ],     
          [ "It",    "Rules",             "4" ],     
          [ "It",    "Rules",             "4" ],     
          [ "It",    "Rules",             "4" ],     
          [ "It",    "Rules",             "4" ],
          [ "It",    "Rules",             "4" ],     
          [ "It",    "Rules",             "4" ],     
          [ "It",    "Rules",             "4" ],     
          [ "It",    "Rules",             "4" ],     
          [ "It",    "Rules",             "4" ],     
          [ "It",    "Rules",             "4" ],     
          [ "It",    "Rules",             "4" ],  
          [ "It",    "Rules",             "4" ],     
          [ "It",    "Rules",             "4" ]],     

     :font_size          => 24, 
     :horizontal_padding => 10,
     :vertical_padding   => 3,
     :border_width       => 2,
     :position           => :center,
     :headers            => ["Column A","Column B","#"]

  end
end
Run Code Online (Sandbox Code Playgroud)

您可以查看repeat的文档页面,了解其他选项,以便您准确指定转发器的位置.


Gaw*_*win 21

@GrantSayer thx为例,但这只会让你显示当前的页码,而不是总页数.

您还可以使用页脚的number_pages函数:

Prawn::Document.generate("page_with_numbering.pdf") do
  text "Hai"
  start_new_page
  text "bai"
  start_new_page
  text "-- Hai again"
  number_pages "<page> in a total of <total>", [bounds.right - 50, 0]
end
Run Code Online (Sandbox Code Playgroud)

但是,在我的情况下,我还需要格式化/样式并右对齐页码以匹配公司样式指南.我使用go_to_page(k)创建自己的页眉和页脚功能,在创建所有页面,将页眉和页脚添加到每个页面.这给了我样式选项和总页数:

Prawn::Document.generate("footer_example.pdf", :skip_page_creation => true) do
  10.times do
    start_new_page
    text "Some filler text for the page"
  end

  # footer
  page_count.times do |i|
    go_to_page(i+1)
    lazy_bounding_box([bounds.right-50, bounds.bottom + 25], :width => 50) {
      text "#{i+1} / #{page_count}"
    }.draw
  end
end
Run Code Online (Sandbox Code Playgroud)

  • 我仍然想知道你是如何管理的,在这种情况下,大虾不会在页脚框上写东西.我使用相同的方式,它只是无法正常工作.当您增加文本量时,在您的示例中也会发生相同的情况.只需删除start_new_page并在循环内的文本行中添加"*100"即可.我通常将整个文档移动到一个边界框中并减去页眉/页脚框的大小以避免这样的问题. (4认同)

Joe*_*MAR 16

与最新版本的Prawn有点不同,你必须传递哈希

Prawn::Document.generate("page_with_numbering.pdf") do
  text "Hai"
  start_new_page
  text "bai"
  start_new_page
  text "-- Hai again"
  number_pages "<page> in a total of <total>", { :start_count_at => 0, :page_filter => :all, :at => [bounds.right - 50, 0], :align => :right, :size => 14 }
end
Run Code Online (Sandbox Code Playgroud)


oni*_*psy 10

如果您想要一个不在文本上写东西的页脚,则必须使用创建bounding_box文档下边距bounds.bottom.

require 'prawn'

file_name = 'hello.pdf'
random_table = (0..50).map{|i|[*('a'..'z')]} # generate a 2D array for example (~2 pages)

Prawn::Document::generate(file_name) do |pdf|
    pdf.table random_table
    pdf.page_count.times do |i|
        pdf.bounding_box([pdf.bounds.left, pdf.bounds.bottom], :width => pdf.bounds.width, :height => 30) {
        # for each page, count the page number and write it
        pdf.go_to_page i+1
             pdf.move_down 5 # move below the document margin
             pdf.text "#{i+1}/#{pdf.page_count}", :align => :center # write the page number and the total page count
        }
    end
end
Run Code Online (Sandbox Code Playgroud)

它看起来应该是这样的,你可以看到页脚位于边缘底部之外:

示例结果

希望它对某人有帮助


kon*_*ung 5

开始编辑

这在虾> = 0.12中起作用

结束编辑

这是我使用重复,画布和单元格的解决方案。本质上,我是在每页的绝对顶部和底部绘制边框。我正在使用cell对其进行更好的样式控制。希望这会对某人有所帮助。(我使用了一些令人讨厌的颜色来更好地说明如何控制页眉和页脚的样式)

 Prawn::Document.generate("headers_and_footers_with_background.pdf") do
  repeat :all do
    # header
    canvas do
      bounding_box([bounds.left, bounds.top], :width => bounds.width) do
        cell :content => 'Header',
             :background_color => 'EEEEEE',
             :width => bounds.width,
             :height => 50,
             :align => :center,
             :text_color => "001B76",
             :borders => [:bottom],
             :border_width => 2,
             :border_color => '00FF00',
             :padding => 12
      end
    end
    # footer
    canvas do
      bounding_box [bounds.left, bounds.bottom + 50], :width  => bounds.width do
        cell :content => 'Footer',
             :background_color => '333333',
             :width => bounds.width,
             :height => 50,
             :align => :center,
             :text_color => "FFFFFF",
             :borders => [:top],
             :border_width => 2,
             :border_color => 'FF0000',
             :padding => 12
      end
    end
  end
  # body
  bounding_box([bounds.left, bounds.top - 25], :width  => bounds.width, :height => bounds.height - 50) do
    100.times do
      text "Some filler text for the page"
    end
  end
end
Run Code Online (Sandbox Code Playgroud)