首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Prawn PDF中的页眉和页脚

Prawn PDF中的页眉和页脚
EN

Stack Overflow用户
提问于 2010-04-23 06:41:12
回答 7查看 30.9K关注 0票数 31

我已经通读了有关Prawn的所有相关帖子,但没有发现(即使在Prawn自己的文档中)也没有提到页眉和页脚。

然而,我确实在Prawnto自己的网站上看到了一个关于页眉和页脚的演示。我复制了该演示的整个源代码,只是为了看看它是否工作,但有一个未定义的方法"header“的错误被投诉。我是不是可以理解为,最近在gem中,对虾去掉了页眉和页脚,或者我还需要先做些什么才能使用页眉和页脚?

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

代码中值得关注的部分:

代码语言:javascript
复制
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

说到标题,以防万一,我指的是通常出现在文档每页角落的单词片段。就像账单页上的账号一样。

谢谢!

EN

回答 7

Stack Overflow用户

回答已采纳

发布于 2010-05-27 15:45:06

@GrantSayer thx作为示例,但这将只允许您显示当前页码,而不是总页数。

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

代码语言:javascript
复制
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)创建了我自己的页眉和页脚函数,它在创建所有页面之后将页眉和页脚添加到每个页面中。这为我提供了样式选项和总页数:

代码语言:javascript
复制
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
票数 22
EN

Stack Overflow用户

发布于 2010-09-07 20:11:29

您引用的示例,来自prawnto插件,使用的是较旧版本的对虾。

因为我还需要页眉和页脚,所以我对此进行了更深入的研究。似乎对虾版本有页眉和页脚方法,这些方法是使用惰性边界框实现的。(通过查看github上的代码找到)

在新的对虾版本中,你可以使用中继器做同样的事情。

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

代码语言:javascript
复制
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

您可以查看repeat的文档页面中的其他选项,这些选项允许您准确地指定您想要中继器的位置。

票数 35
EN

Stack Overflow用户

发布于 2011-07-15 19:37:13

与最新版本的Prawn稍有不同,您必须传递一个散列

代码语言:javascript
复制
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
票数 17
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2695019

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档