使用 repeat(:all) 时,Prawn 似乎没有向下推布局 [英] Prawn doesn't seem to push layout down when using repeat(:all)

查看:32
本文介绍了使用 repeat(:all) 时,Prawn 似乎没有向下推布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在生成一个文档,其中的数据流到每个后续页面上,每个页面都有一个标准标题.但是,当我使用 repeat(:all) 将标题放在每个页面上时,我发现在除第一页之外的每一页上,下一个内容没有被我放在页面上的标题横幅的大小向下移动.

I am generating a document with data that flows onto each subsequent page, each page has a standard header. However, when I use repeat(:all) to put the header on each page, I find that on every page but the first page, the next content is not being moved down by the size of the header banner I have put on the page.

我用于生成横幅的代码:

My code for generating the banner:

class SmartsoftPdf < Prawn::Document
  BOX_MARGIN = 30
  RHYTHM = 10
  INNER_MARGIN = 30

  # Colors
  #
  BLACK      = "000000"
  LIGHT_GRAY = "F2F2F2"
  GRAY       = "DDDDDD"
  DARK_GRAY  = "333333"
  BROWN      = "A4441C"
  ORANGE     = "F28157"
  LIGHT_GOLD = "FBFBBE"
  DARK_GOLD  = "EBE389"
  BLUE       = "08C"
  GREEN      = "00ff00"
  RED        = "ff0000"


  def show_header(text,date)
    header_box do
      image "#{Rails.root}/app/assets/images/smart_records_logo_h60.png", :height => 40
      draw_text text,
        :at => [80,25], :size => 12, :style => :bold, :color => BLUE
      draw_text "Date: #{ausDate(date)}", 
        :at => [bounds.right - 100,bounds.top - 15], :size => 10 if date
    end
  end

  def header_box(&block)
    bounding_box([-bounds.absolute_left, cursor + BOX_MARGIN + 8],
                 :width  => bounds.absolute_left + bounds.absolute_right,
                 :height => BOX_MARGIN*2) do

      fill_color LIGHT_GRAY
      fill_rectangle([bounds.left, bounds.top],
                      bounds.right,
                      bounds.top - bounds.bottom)
      fill_color BLACK
      move_down(RHYTHM)

      indent(BOX_MARGIN, &block)
    end

    stroke_color GRAY
    stroke_horizontal_line(-BOX_MARGIN, bounds.width + BOX_MARGIN, :at => cursor)
    stroke_color BLACK

    move_down(RHYTHM*4)
  end
end

然后在 pdf 生成本身我做:

Then within the pdf generation itself I do:

repeat(:all) do
  show_header("Custom Report",DateTime.now())
end

但是,当我开始将内容放到页面上时,我希望当内容溢出到下一页时,内容将显示在页眉之后.我发现标题与内容重叠.

However, when I start putting content onto the pages, I expect when the content overflows onto the next page that the content will show up after the header. I'm finding that the header overlaps the content instead.

这是说明问题的图像:http://i.imgur.com/mSy2but.png

我是否错误地构建了标题框?我是否需要做一些额外的事情来使溢出到下一页的内容被推下适当的数量?

Am I building the header box incorrectly? Do I need to do something additional to make it so that the content which spills into the next page gets pushed down the appropriate amount?

推荐答案

好的.我自己解决了这个问题.最新版本的 Prawn 有更好的方法来处理这种情况.当您使用 repeat(:all) 时,页面会在文档创建后重新打开,然后添加您的内容创建项目.这不会将页面向下推.将此页眉添加到每个页面的正确方法是使用画布"方法,该方法允许您在页边距边界之外进行操作.使用canvas在页面顶部绘制一个框,并设置页面的top_margin将所有内容推送到banner下方.

Okay. I have solved this myself. Most recent version of Prawn has a better way to handle this case. When you use repeat(:all) the page is reopened AFTER document creation and your content creation items are then added. This doesn't push the page down. The correct way to add this header to every page is to use the "canvas" method which allows you to operate out of the bounds of the page margin. Use canvas to draw a box at the top of the page, and set the top_margin of the page to push all content below the banner.

canvas do
      bounding_box([bounds.left,bounds.top],
                   :width  => bounds.absolute_left + bounds.absolute_right,
                   :height => BOX_MARGIN*2) do

        fill_color LIGHT_GRAY
        fill_rectangle([bounds.left, bounds.top],
                        bounds.right,
                        bounds.top - bounds.bottom)
        fill_color BLACK
        move_down(RHYTHM)

        indent(BOX_MARGIN, &block)
      end

      stroke_color GRAY
      stroke_horizontal_line(-BOX_MARGIN, bounds.width + BOX_MARGIN, :at => cursor)
      stroke_color BLACK
 end

在创建文档时...

 def initialize(options = {})
    super(:page_layout => :landscape,:top_margin => HEIGHT_OF_BANNER)
 end

这篇关于使用 repeat(:all) 时,Prawn 似乎没有向下推布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆