Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 如何在Prawn中检查边界框是否适合当前页面_Ruby_Prawn - Fatal编程技术网

Ruby 如何在Prawn中检查边界框是否适合当前页面

Ruby 如何在Prawn中检查边界框是否适合当前页面,ruby,prawn,Ruby,Prawn,使用Ruby 2.4和Prawn 2.2.0,如果内容块不适合当前页面,我希望将其放入下一页面。 我尝试使用边界框,因为这样内部内容可以是我想要的任何内容,但我没有实现它 我有以下文件: require 'prawn' class Doc < Prawn::Document def initialize(options: {}) super(options.merge(**page_options)) setup_page header body

使用Ruby 2.4和Prawn 2.2.0,如果内容块不适合当前页面,我希望将其放入下一页面。 我尝试使用边界框,因为这样内部内容可以是我想要的任何内容,但我没有实现它

我有以下文件:

require 'prawn'

class Doc < Prawn::Document
  def initialize(options: {})
    super(options.merge(**page_options))
    setup_page
    header
    body
    footer
  end

  def header
    bounding_box([0, cursor], width: bounds.width) do
      bounding_box([0, bounds.top], width: bounds.width / 2) do
        text 'Left Header', size: 36
      end

      bounding_box([bounds.width / 2, bounds.top], width: bounds.width / 2) do
        text 'Right Header', size: 36
      end
      stroke_bounds
    end
  end

  def body
    bounding_box([0, cursor], width: bounds.width / 2) do
      text 'The stack has overflowed over the overflow' * 12, size: 24
    end
  end

  def footer
    bounding_box([0, cursor], width: bounds.width) do
      bounding_box([0, bounds.top], width: bounds.width / 2) do
        text 'Left Footer' * 3, size: 36, color: '0000FF'
        stroke_bounds
      end

      bounding_box([bounds.width / 2, bounds.top], width: bounds.width / 2) do
        text 'Right Footer' * 3, size: 36, color: '0000FF'
        stroke_bounds
      end
    end
  end

  def setup_page
    define_grid(columns: 12, rows: 8, gutter: 10)
  end

  def page_options
    {
      page_size: ::PDF::Core::PageGeometry::SIZES['A4'],
      page_layout: :portrait
    }
  end
end

Doc.new.render_file 'doc.pdf'
需要“对虾”
类文档<对虾::文档
def初始化(选项:{})
超级(选项。合并(**第_页选项))
设置页面
标题
身体
页脚
结束
def收割台
边界框([0,光标],宽度:bounds.width)do
边框([0,bounds.top],宽度:bounds.width/2)do
文本“左标题”,大小:36
结束
边界框([bounds.width/2,bounds.top],宽度:bounds.width/2)do
文本“右标题”,大小:36
结束
笔划界
结束
结束
def主体
边框([0,光标],宽度:bounds.width/2)do
文本“堆栈溢出溢出”*12,大小:24
结束
结束
def页脚
边界框([0,光标],宽度:bounds.width)do
边框([0,bounds.top],宽度:bounds.width/2)do
文本“左脚”*3,大小:36,颜色:“0000FF”
笔划界
结束
边界框([bounds.width/2,bounds.top],宽度:bounds.width/2)do
文本“右页脚”*3,尺寸:36,颜色:“0000FF”
笔划界
结束
结束
结束
def设置页面
定义网格(列:12,行:8,边沟:10)
结束
def页面_选项
{
页面大小:::PDF::Core::PageGeometry::SIZES['A4'],
页面布局::纵向
}
结束
结束
Doc.new.render_文件“Doc.pdf”
由于我想将
页脚
放在同一页上,即没有分页符,因此我需要检查页脚的高度,看看它是否适合当前页面,如果不适合,则插入分页符

问题是我没有办法创建一个用于检查的边界框,然后再绘制它


或者有另一种方法来确保内容不被分割,确保所有内容都在同一页上?

你有没有想过?我也有同样的问题。有一次,我尝试创建一种方法来计算一个框中所有字符的宽度,计算出要写的行数,计算出这个框的总高度,然后用这个值来指定高度,将其与页面左侧的剩余高度进行比较,然后在必要时稍作休息。@oneworkingheephone我最终与表格一起使用了。。。这不是最好的解决方案,但目前它仍然有效。