Prawn - Move cursor down after text_box expand

怎甘沉沦 提交于 2019-12-11 06:08:37

问题


I'm generating PDF's using the Prawn Gem and I'm unable to find a way to move the cursor down after text_box expands from overflow text, similar to the way a regular text call would.

Text_box example

pad(5) {
  text_box payable, :at => [bounds.left, cursor], :width => 540, :height => 15, :overflow => :expand, inline_format: true
}

move_down(15)

pad(5) {
  text_box address, :at => [bounds.left, cursor], :width => 250, :height => 15, :overflow => :expand, inline_format: true
  text_box city, :at => [250, cursor], :width => 100, :height => 15, :overflow => :expand, inline_format: true
  text_box state, :at => [350, cursor], :width => 75, :height => 15, :overflow => :expand, inline_format: true
  text_box zip, :at => [425, cursor], :width => 110, :height => 15, :overflow => :expand, inline_format: true
}

So above, I have to pad and move_down from the text_box payable in order for the next set of text_boxes to be formatted correctly without overlapping. If I use a straight text call on the payable string then the cursor moves down after all of the text is rendered, as expected.

The reason I'm using text_box over regular text is so that I can position text side by side on the same line. While this works great for strings thats all fit on a single line, it doesn't seem to play out well if one of those areas expands the text_box downwards because the cursor just starts at the next text line instead of below the expanded text_box.

Any insight or suggestions would be appreciated, thanks!


回答1:


You've probably figured out something by now but I'm also new to Prawn and had the same question so hopefully this will help someone else. This example shows both a text box and a formatted text box. There is probably somehow a better way but this is working for me.

  txt1 = "u" * 250
  txt2 = "v" * 600
  txt3 = "w" * 100
  txt4 = "x" * 500
  txt5 = "y" * 200
  txt6 = "z" * 400

  stroke_horizontal_rule

  options = {:document=>@pdf, :at=>[0,cursor]}
  text_box(txt1, options)
  measure = Prawn::Text::Box.new(txt1, options)
  measure.render(:dry_run => true)
  move_down(measure.height)

  options = {:document=>@pdf, :at=>[0,cursor]}
  text_box(txt2, options)
  measure = Prawn::Text::Box.new(txt2, options)
  measure.render(:dry_run => true)
  move_down(measure.height)

  options = {:document=>@pdf, :at=>[0,cursor]}
  text_box(txt3, options)
  measure = Prawn::Text::Box.new(txt3, options)
  measure.render(:dry_run => true)
  move_down(measure.height)

  array = [{:text=>txt4, :size=>12}]
  options = {:document=>@pdf, :at=>[0,cursor]}
  formatted_text_box(array, options)
  measure = Prawn::Text::Formatted::Box.new(array, options)
  measure.render(:dry_run => true)
  move_down(measure.height)

  array = [{:text=>txt5, :size=>16}]
  options = {:document=>@pdf, :at=>[0,cursor]}
  formatted_text_box(array, options)
  measure = Prawn::Text::Formatted::Box.new(array, options)
  measure.render(:dry_run => true)
  move_down(measure.height)

  array = [{:text=>txt6, :size=>12}]
  options = {:document=>@pdf, :at=>[0,cursor]}
  formatted_text_box(array, options)
  measure = Prawn::Text::Formatted::Box.new(array, options)
  measure.render(:dry_run => true)
  move_down(measure.height)

  stroke_horizontal_rule


来源:https://stackoverflow.com/questions/46555227/prawn-move-cursor-down-after-text-box-expand

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!