I'm trying to create a pdf report using prawn in a rails application. There are lots of sections that contain user generated content that I want to try and group together. Sometimes this will go over more that one page which results in a cannot group error. I then tried to use a transaction so that in the event of an error I can rollback and then output the content without using the group method.
我试图在rails应用程序中使用虾创建一个pdf报告。有很多部分包含用户生成的内容,我想尝试将它们分组在一起。有时这将超过导致不能组错误的那一页。然后,我尝试使用事务,以便在发生错误时,我可以回滚,然后不使用group方法输出内容。
The problem is the rollback stuffs up the pages. It removes the extra page from the pdf but still has the wrong page count and outputs over lapping content when I try to redo it. I reset the y position after the rollback, as per the prawn documentation but I still get the problems.
问题是页面的回滚。它从pdf中删除了多余的页面,但是当我试图重做时,仍然有错误的页面计数和输出。我在回滚之后重置了y的位置,根据对虾文档,但是我仍然得到了问题。
eg. The following test code writes 2 pages of numbers, does a rollback to the start and then tries to write the same numbers again. It results in a single page pdf with the second page of numbers overlapping the first and a page count of 2. The page counts at the bottom of the page also overlap one another even though I'm using the prawn number_pages method
如。下面的测试代码写了2页的数字,回滚到开始,然后再试着写相同的数字。它生成一个单页pdf,第二页的数字与第一页重叠,页数为2。即使我使用的是prawn number_pages方法,页面底部的页面也会相互重叠。
class TestReport < Prawn::Document
def to_pdf
font('Helvetica')
bounding_box([bounds.left, bounds.top - 50], :width => bounds.width, :height => bounds.height - 100) do
text 'begin'
y_pos = y
transaction do
begin
group do
64.times do|i|
text i.to_s
end
end
rescue
rollback
end
end
self.y = y_pos
64.times do|i|
text i.to_s
end
text 'end'
text page_number.to_s
end
page_numbers(1)
#render
end
def page_numbers(start)
string = "page <page> of <total>"
options = { :at => [bounds.right - 150, 40],
:width => 150,
:align => :right,
:start_count_at => start,
:color => "000000" }
number_pages string, options
end
end
def test_report
pdf = TestReport.new()
pdf.to_pdf
send_data pdf.render, filename: "test.pdf",
type: "application/pdf",
disposition: "inline"
end
The problems seem to be with transaction rollbacks. The main thing I want is to be able to use the group method. Is there another way?
问题似乎在于事务回滚。我想要的主要是能够使用组方法。有另一种方式吗?
Is my code wrong? Am I missing something or do transaction not currently work.
我的代码是错误的吗?我是否丢失了一些东西或做了当前不工作的事务。
I'm currently using the master prawn branch in a ruby on rails application ( gem 'prawn', :git => 'git://github.com/prawnpdf/prawn.git', :branch => 'master').
我现在使用的是在ruby on rails应用程序上的主对虾分支(gem 'prawn',:git => 'git://github.com/prawnpdf/prawn.git' .:branch => 'master')。
2 个解决方案
#1
3
This question is quite old now, but I'll post an answer since it is one of the first hits on Google when searching for the exception.
这个问题现在已经很老了,但是我将发布一个答案,因为它是在搜索异常时谷歌上的第一个命中。
Transactions still doesnt work with page breaks (v 1.0.0.rc2), so I created a helper method that tries to use grouping first and then if the exception occurs it just retries without grouping, making the content span more than one page.
事务仍然不能使用分页符(v 1.0.0.rc2),因此我创建了一个助手方法,它试图首先使用分组,然后如果出现异常,它就重试而不进行分组,使内容跨越多个页面。
def group_if_possible(pdf, &block)
begin
pdf.group { block.call }
rescue Prawn::Errors::CannotGroup
block.call
end
end
Example: Using it when creating a table:
示例:在创建表时使用它:
group_if_possible(pdf) do
pdf.table(rows)
end
EDIT:
Grouping were removed from Prawn 1.x but there is an unofficial grouping gem that works well for Prawn 2: https://github.com/ddengler/prawn-grouping
编辑:从大虾1中删除分组。但是有一个非官方的分组gem可以很好地用于虾2:https://github.com/ddengler/prawn- groups
#2
1
Looks like Brad Ediger answered your question on Google Groups, but for the benefit of anyone else looking for help with this, here's his response:
看起来Brad Ediger回答了你关于谷歌组的问题,但是为了其他人的利益寻求帮助,以下是他的回答:
Sadly, transactions do not yet work correctly when they start new pages or change the pages collection. It's a known issue:
遗憾的是,事务在启动新页面或更改页面集合时还不能正常工作。这是一个已知问题:
https://github.com/prawnpdf/prawn/issues/268
https://github.com/prawnpdf/prawn/issues/268
-be
它的未来
#1
3
This question is quite old now, but I'll post an answer since it is one of the first hits on Google when searching for the exception.
这个问题现在已经很老了,但是我将发布一个答案,因为它是在搜索异常时谷歌上的第一个命中。
Transactions still doesnt work with page breaks (v 1.0.0.rc2), so I created a helper method that tries to use grouping first and then if the exception occurs it just retries without grouping, making the content span more than one page.
事务仍然不能使用分页符(v 1.0.0.rc2),因此我创建了一个助手方法,它试图首先使用分组,然后如果出现异常,它就重试而不进行分组,使内容跨越多个页面。
def group_if_possible(pdf, &block)
begin
pdf.group { block.call }
rescue Prawn::Errors::CannotGroup
block.call
end
end
Example: Using it when creating a table:
示例:在创建表时使用它:
group_if_possible(pdf) do
pdf.table(rows)
end
EDIT:
Grouping were removed from Prawn 1.x but there is an unofficial grouping gem that works well for Prawn 2: https://github.com/ddengler/prawn-grouping
编辑:从大虾1中删除分组。但是有一个非官方的分组gem可以很好地用于虾2:https://github.com/ddengler/prawn- groups
#2
1
Looks like Brad Ediger answered your question on Google Groups, but for the benefit of anyone else looking for help with this, here's his response:
看起来Brad Ediger回答了你关于谷歌组的问题,但是为了其他人的利益寻求帮助,以下是他的回答:
Sadly, transactions do not yet work correctly when they start new pages or change the pages collection. It's a known issue:
遗憾的是,事务在启动新页面或更改页面集合时还不能正常工作。这是一个已知问题:
https://github.com/prawnpdf/prawn/issues/268
https://github.com/prawnpdf/prawn/issues/268
-be
它的未来