gpt4 book ai didi

ruby - 用 Prawn 控制内容流

转载 作者:数据小太阳 更新时间:2023-10-29 06:39:06 26 4
gpt4 key购买 nike

假设我们要在占据页面上半部分的第一页上显示一个标题。页面的下半部分应该填满我们的文章文本,文本应该继续流入后续页面直到用完:

enter image description here

这是一个非常基本的布局场景,但我不明白如何在 Prawn 中实现它。

这是从他们的在线文档中派生的一些示例代码:

pdf = Prawn::Document.new do
text "The Prince", :align => :center, :size => 48
text "Niccolò Machiavelli", :align => :center, :size => 20
move_down 42

column_box([0, cursor], :columns => 3, :width => bounds.width) do
text((<<-END.gsub(/\s+/, ' ') + "\n\n") * 20)
All the States and Governments by which men are or ever have been ruled,
have been and are either Republics or Princedoms. Princedoms are either
hereditary, in which the bla bla bla bla .....
END
end
end.render

但这只会继续显示每个页面的标题空间:

enter image description here

正确的做法是什么?

最佳答案

我一直在和同样的问题作斗争。我最终继承了 ColumnBox 并添加了一个助手来调用它:

module Prawn
class Document

def reflow_column_box(*args, &block)
init_column_box(block) do |parent_box|
map_to_absolute!(args[0])
@bounding_box = ReflowColumnBox.new(self, parent_box, *args)
end
end

private

class ReflowColumnBox < ColumnBox
def move_past_bottom
@current_column = (@current_column + 1) % @columns
@document.y = @y
if 0 == @current_column
@y = @parent.absolute_top
@document.start_new_page
end
end
end
end
end

然后它就像普通的列框一样被调用,但在下一个分页符将回流到父边界框。改变你的线路:

  column_box([0, cursor], :columns => 3, :width => bounds.width) do

  reflow_column_box([0, cursor], :columns => 3, :width => bounds.width) do

希望对你有帮助。 Prawn 的级别很低,是一把双刃剑,它有时无法满足您的需求,但工具可以扩展和构建更复杂的结构。

关于ruby - 用 Prawn 控制内容流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17282100/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com