prawnto 显示新页面时不会中断的表格

发布于 2024-08-18 07:49:31 字数 516 浏览 6 评论 0原文

我有数量可变的表,行数可变,我想让它们一个接一个地显示,但如果当前页面不适合表,请将其放在下一页上,然后继续。我已将表格放入事务中,这样如果高度适合当前页面,我可以回滚然后打印它,但如何获取表格高度?

我现在有这段代码,

pdf.transaction do 

pdf.table @data,
    :font_size  => 12, 
    :border_style => :grid,
    :horizontal_padding => 10,
    :vertical_padding   => 3,
    :border_width       => 2,
    :position           => :left,
    :row_colors => ["FFFFFF","DDDDDD"]

pdf.move_down 20

#pdf.rollback 
end

对此有任何帮助都会很棒。或者还有其他方法可以做到这一点吗?

此致 瑞克

I have a variable number of tables with variable number of rows and I want to have them displaying one after the other but if a table doesn't fit on the current page put it on the next then continue on. I have put the table in a transaction so I can roll back then print it if the height will fit on curent page but how do I get the table height?

I have this code at the moment

pdf.transaction do 

pdf.table @data,
    :font_size  => 12, 
    :border_style => :grid,
    :horizontal_padding => 10,
    :vertical_padding   => 3,
    :border_width       => 2,
    :position           => :left,
    :row_colors => ["FFFFFF","DDDDDD"]

pdf.move_down 20

#pdf.rollback 
end

Any help on this would be great. Or any other way to do this ?

Best Regards
Rick

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

帅气尐潴 2024-08-25 07:49:31

4 年后...:)

正如 @mx 所写,出于安全原因,回滚被禁用,就像“组”一样,并且仍然没有实现。所以,这里我如何处理表格的分页:

大而简单的表格(每个数据一行)

只需使用标题选项

pdf.table @data,
  header: true, # You can use 'header: 2' if your header take two rows
  font_size: 12, 
  border_style: :grid,
  horizontal_padding: 10,
  vertical_padding: 3,
  border_width: 2,
  position: :left,
  row_colors: ["FFFFFF","DDDDDD"]

小表格或复杂表格

  • 使表格
  • 检查是否需要分页
  • 绘制表格

以您的示例:

t = pdf.make_table @data,
  font_size: 12, 
  border_style: :grid,
  horizontal_padding: 10,
  vertical_padding: 3,
  border_width: 2,
  position: :left,
  row_colors: ["FFFFFF","DDDDDD"]

if cursor - t.height < 0
  start_new_page
end

t.draw

希望有帮助

4 years later... :)

As @m-x wrote it, rollback was disabled for security reason, like "group", and is still not implemented. So, here how I deal with break pages for tables :

Big and simple table (one row per data)

Just use header option

pdf.table @data,
  header: true, # You can use 'header: 2' if your header take two rows
  font_size: 12, 
  border_style: :grid,
  horizontal_padding: 10,
  vertical_padding: 3,
  border_width: 2,
  position: :left,
  row_colors: ["FFFFFF","DDDDDD"]

Small table or complexe table

  • make table
  • check if you need break page
  • draw table

With your example :

t = pdf.make_table @data,
  font_size: 12, 
  border_style: :grid,
  horizontal_padding: 10,
  vertical_padding: 3,
  border_width: 2,
  position: :left,
  row_colors: ["FFFFFF","DDDDDD"]

if cursor - t.height < 0
  start_new_page
end

t.draw

Hope that helps

欢你一世 2024-08-25 07:49:31

@current_page = pdf.page_count

@roll = pdf.transaction do 
  pdf.move_down 20

  pdf.table @data,
    :font_size  => 12, 
    :border_style => :grid,
    :horizontal_padding => 10,
    :vertical_padding   => 3,
    :border_width       => 2,
    :position           => :left,
    :row_colors => ["FFFFFF","DDDDDD"]

  pdf.rollback if pdf.page_count > @current_page

end 

if @roll == false

  pdf.start_new_page

  pdf.table @data,
    :font_size  => 12, 
    :border_style => :grid,
    :horizontal_padding => 10,
    :vertical_padding   => 3,
    :border_width       => 2,
    :position           => :left,
    :row_colors => ["FFFFFF","DDDDDD"]
end

我希望这对你和我都有效:-)

@current_page = pdf.page_count

@roll = pdf.transaction do 
  pdf.move_down 20

  pdf.table @data,
    :font_size  => 12, 
    :border_style => :grid,
    :horizontal_padding => 10,
    :vertical_padding   => 3,
    :border_width       => 2,
    :position           => :left,
    :row_colors => ["FFFFFF","DDDDDD"]

  pdf.rollback if pdf.page_count > @current_page

end 

if @roll == false

  pdf.start_new_page

  pdf.table @data,
    :font_size  => 12, 
    :border_style => :grid,
    :horizontal_padding => 10,
    :vertical_padding   => 3,
    :border_width       => 2,
    :position           => :left,
    :row_colors => ["FFFFFF","DDDDDD"]
end

I hope this works for you as for me :-)

路弥 2024-08-25 07:49:31

我是虾初学者,所以这可能不是最好的解决方案,但它应该有效。

如果考虑字体大小和垂直填充以及 @data 中的记录数,您可以获得表格高度,并且可以通过调用 Prawn::Document 获得当前光标位置。光标方法。

有了这两个数字,您应该能够检查该表是否适合此页面。如果没有,只需启动一个新页面(通过调用 Prawn::Document.start_new_page 方法)。

否则表格将自动中断并在下一页继续。

I'm a Prawn beginner, so this might not be the best solution, but it should work.

You can get the table height if you consider the font size and vertical padding and the number of records you have in @data and you can get the current cursor position by calling Prawn::Document.cursor method.

Having these two numbers you should be able to check whether the table fits on this page or not. If not, just start a new one (by calling Prawn::Document.start_new_page method).

Otherwise the table will break automatically and will continue on the next page.

谁把谁当真 2024-08-25 07:49:31

谢谢伊戈尔,

我目前正在设置当前页面,然后在呈现新表之后和回滚设置 new_page 变量之前的事务中。然后我可以回滚并检查新页面是否> var >当前页面变量,如果是则开始新页面并打印表格。请参阅下面的代码。

现在的问题是 pdf.start_new_page 说错误,但如果我只是将 pdf.rollback 行取出来,它就可以工作。请参阅下面的错误。

任何想法,任何一种或任何更简单的方法,一定有一个!

谢谢
瑞克

@current_page = pdf.page_count

pdf.transaction do 
  pdf.move_down 20

  pdf.table @data,
    :font_size  => 12, 
    :border_style => :grid,
    :horizontal_padding => 10,
    :vertical_padding   => 3,
    :border_width       => 2,
    :position           => :left,
    :row_colors => ["FFFFFF","DDDDDD"]

  @the_next_page = pdf.page_count
  pdf.rollback

end 

if @the_next_page > @current_page

  pdf.start_new_page

  pdf.table @data,
    :font_size  => 12, 
    :border_style => :grid,
    :horizontal_padding => 10,
    :vertical_padding   => 3,
    :border_width       => 2,
    :position           => :left,
    :row_colors => ["FFFFFF","DDDDDD"]
end 

的错误

> You have a nil object when you didn't expect it!
The error occurred while evaluating nil.identifier

Extracted source (around line #158): 

155: end



RAILS_ROOT: C:/InstantRails/rails_apps/Macrotec-Application

Application Trace | Framework Trace | Full Trace 
c:/InstantRails/ruby/lib/ruby/gems/1.8/gems/prawn-core-0.7.1/lib/prawn/document.rb:302:in `go_to_page'
c:/InstantRails/ruby/lib/ruby/gems/1.8/gems/prawn-core-0.7.1/lib/prawn/document/internals.rb:128:in `finalize_all_page_contents'
c:/InstantRails/ruby/lib/ruby/gems/1.8/gems/prawn-core-0.7.1/lib/prawn/document/internals.rb:127:in `each'
c:/InstantRails/ruby/lib/ruby/gems/1.8/gems/prawn-core-0.7.1/lib/prawn/document/internals.rb:127:in `finalize_all_page_contents'
c:/InstantRails/ruby/lib/ruby/gems/1.8/gems/prawn-core-0.7.1/lib/prawn/document.rb:344:in `render'
C:/InstantRails/rails_apps/Macrotec-Application/app/views/quotations/show.pdf.prawn:158:in `_run_prawn_app47views47quotations47show46pdf46prawn'

Thanks Igor

I am currently setting the current page and then in the transaction after the new table has been rendered and before the roll back setting new_page variable. Then i can roll back and chek if the new page var > current page var and if it is start new page and print the table. See code below.

The problem is now the pdf.start_new_page says error but if i just take the pdf.rollback line out it works. See error below.

Any ideas any one or any easier ways, there must be one!!

thanks
rick

@current_page = pdf.page_count

pdf.transaction do 
  pdf.move_down 20

  pdf.table @data,
    :font_size  => 12, 
    :border_style => :grid,
    :horizontal_padding => 10,
    :vertical_padding   => 3,
    :border_width       => 2,
    :position           => :left,
    :row_colors => ["FFFFFF","DDDDDD"]

  @the_next_page = pdf.page_count
  pdf.rollback

end 

if @the_next_page > @current_page

  pdf.start_new_page

  pdf.table @data,
    :font_size  => 12, 
    :border_style => :grid,
    :horizontal_padding => 10,
    :vertical_padding   => 3,
    :border_width       => 2,
    :position           => :left,
    :row_colors => ["FFFFFF","DDDDDD"]
end 

The error

> You have a nil object when you didn't expect it!
The error occurred while evaluating nil.identifier

Extracted source (around line #158): 

155: end



RAILS_ROOT: C:/InstantRails/rails_apps/Macrotec-Application

Application Trace | Framework Trace | Full Trace 
c:/InstantRails/ruby/lib/ruby/gems/1.8/gems/prawn-core-0.7.1/lib/prawn/document.rb:302:in `go_to_page'
c:/InstantRails/ruby/lib/ruby/gems/1.8/gems/prawn-core-0.7.1/lib/prawn/document/internals.rb:128:in `finalize_all_page_contents'
c:/InstantRails/ruby/lib/ruby/gems/1.8/gems/prawn-core-0.7.1/lib/prawn/document/internals.rb:127:in `each'
c:/InstantRails/ruby/lib/ruby/gems/1.8/gems/prawn-core-0.7.1/lib/prawn/document/internals.rb:127:in `finalize_all_page_contents'
c:/InstantRails/ruby/lib/ruby/gems/1.8/gems/prawn-core-0.7.1/lib/prawn/document.rb:344:in `render'
C:/InstantRails/rails_apps/Macrotec-Application/app/views/quotations/show.pdf.prawn:158:in `_run_prawn_app47views47quotations47show46pdf46prawn'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文