2010-04-22 166 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

希望它可以幫助別人