31

私はPrawnに関するすべての関連する投稿を読みましたが、ヘッダーとフッターについての言及は(Prawn自身のドキュメントでも)見つかりませんでした。

ただし、Prawnto自身のWebサイトで、ヘッダーとフッターに関するデモを確認しました。そのデモのソース全体をコピーして、動作するかどうかを確認しましたが、未定義のメソッド「ヘッダー」のエラーが表示されます。エビが最近宝石のヘッダーとフッターを取り出したことを理解していますか、それともヘッダーとフッターを使用するために最初に行う必要がある何かがありますか?

デモページ: 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

ヘッダーとは、念のため、ドキュメントのすべてのページの隅に通常表示される単語の抜粋を意味します。請求書ページのアカウント番号のように。

ありがとう!

4

7 に答える 7

35

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

リピーターが必要な場所を正確に指定できる他のオプションについては、リピータのドキュメントページを確認できます。

于 2010-09-07T12:11:28.687 に答える
22

この例では@GrantSayerthxですが、これでは現在のページ番号のみが表示され、合計ページ数は表示されません。

フッターに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
于 2010-05-27T07:45:06.030 に答える
17

最新バージョンのエビとは少し異なり、ハッシュを渡す必要があります

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
于 2011-07-15T11:37:13.410 に答える
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

それはそのように見えるはずです、あなたはフッターがマージンの底の外側にあるのを見ることができます:

結果の例

それが誰かを助けることを願っています

于 2014-06-11T12:07:07.680 に答える
6

編集を開始

これはエビ>=0.12で機能します

編集終了

これが、repeat、canvas、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
于 2013-05-22T17:42:46.567 に答える
1

ページ上で繰り返しアイテムを取得するために私が見つけた唯一の方法は、Prawn::Document::LazyBoundingBoxメソッドを使用することです。基本的に、これにより、呼び出したときにのみレンダリングされるバウンディングボックスを定義できます。したがって、通常のpseudo-code手順は次のとおりです。

  1. somevarに割り当てるレイジーバウンディングボックス要素を定義する
  2. 新しいページごとに要素を呼び出します。

ソースからの例は、

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
end

これにより、新しいページごとにページ数のテキスト出力が得られます。理想的なのは、要素をレンダリングするための手動呼び出しなしで再利用されるページテンプレートのセットアップにこれを適用できるかどうかです。テキストフローと組み合わせると、ヘッダーとフッターの従来のソリューションが得られます。

于 2010-05-26T04:21:00.150 に答える
1

カスタムフッターのコンテンツを作成するためにを使用する場合の問題はbounding_box次のとおりです...それでもマージンの範囲内でレンダリングされます。

余白部分に内容を一緒に書き込むものを探していましnumber_pagesた(通常は下余白部分にフッターが設定されているため)...なかったようです。

代わりに、次のようにtext_box座標をメインのバウンディングボックスの外側に配置しました。

repeat :all do
      text_box "My custom footer", size: 7, align: :center, :at => [bounds.left, bounds.bottom], :height => 100, :width => bounds.width
end

repeat :all、はこのフッターテキストをすべてのページにレンダリングすることに注意してください。

于 2018-03-12T05:55:26.850 に答える