2010-04-22 100 views
28

我已阅读了所有关于Prawn的相关文章,但没有发现页眉和页脚没有提及(即使在Prawn自己的文档中)。Prawn中的页眉和页脚PDF

但是,我确实在Prawnto自己的网站上看到有关页眉和页脚的演示。我复制了该演示的完整源代码,以查看它是否有效,但未定义方法“标题”的错误被抱怨。我是否明白,虾最近拿出页眉和页脚在宝石中,还是有什么我需要首先使用页眉和页脚?

演示页: 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 

并通过头,以防万一,我的意思的话,在每一个角落出现通常的片段文件的页面。就像帐单页面中的帐号一样。

谢谢!

回答

18

@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 

然而,在我的情况我还需要格式化/风格和右对齐的页码,以匹配公司的风格指南。我使用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 
+4

我仍然不知道如何管理,是对虾在这种情况下,不会在您的页脚框中写入内容。我使用相同的方式,但它不起作用。在你的例子中,当你增加文本数量时也会发生同样的情况。只需删除start_new_page并在循环内的文本行中添加一个“* 100”即可。我通常会将整个文档移动到边界框中,并减去页眉/页脚框的大小以避免这样的问题。 – unexist 2011-03-17 12:56:23

+1

这个页脚的例子确实节省了我几个小时的痛苦。 – Vigrant 2013-09-17 16:00:33

0

我发现在页面上获得重复项目的唯一方法是使用Prawn::Document::LazyBoundingBox方法。基本上这允许你定义一个边界框,只有在你调用它的时候才会被渲染。所以平时pseudo-code步骤

1. Define lazy bounding box element assign to somevar 
2. One each new page call the element. 

从源的例子展示如何

file = "lazy_bounding_boxes.pdf" 
Prawn::Document.generate(file, :skip_page_creation => true) do 
    point = [bounds.right-50, bounds.bottom + 25] 
    page_counter = lazy_bounding_box(point, :width => 50) do 
    text "Page: #{page_count}" 
end 

10.times do 
    start_new_page 
    text "Some filler text for the page" 
    page_counter.draw 
end 

这给你的每个新页面在页面上数文本输出。最理想的是,如果这可以应用于设置一个页面模板,该模板不需要手动调用就可以重用元素。与文本流相结合,它将提供页眉和页脚的传统解决方案。

32

你指的,从prawnto插件,使用对虾的旧版本的样品。

因为我还需要页眉和页脚,所以我对此进行了一些观察。看起来这个版本的虾有页眉和页脚方法,这些方法是使用惰性边界框实现的。 (通过检查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 

你可以检查重复的文档页面,让你可以准确地指定要中继的其他选项。

13

这是一个与虾的最新版本略有不同,你必须过时的哈希

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 
+0

这一个适合我,其他答案21 +票,不适合我.. – Kokizzu 2013-10-03 09:29:43

+1

总是乐于帮助 – 2013-10-14 14:19:57

+0

感谢:链接到当前文档也:http://www.rubydoc.info/github/sandal/prawn/Prawn%2FDocument%3Anumber_pages – 2016-09-12 09:45:01

2

START EDIT

这工作对虾> = 0.12

编辑完

这是我的解决方案使用重复,画布d细胞。本质上,我在每个页面的绝对顶部和底部绘制边界框。我正在使用单元格对它进行更好的样式控制。希望这会对某人有所帮助。 (我用的有点恼人的颜色,以更好地说明如何控制页眉和页脚的造型)

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 
+1

非常感谢你,这正是我一直在寻找的。 – Thrasher 2017-11-02 02:25:23

7

如果你想不写东西,在你的文本页脚,你必须创建的利润率低于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 

它应该看起来像,你可以看到页脚是保证金底部外:

example result

希望它可以帮助别人