Don't break paragraph when new page (Prawn)
You could just do :
pdf.group do
#Your code
end
Is that what you were looking for ??
Ruby: Prawn PDF out of memory when using the group method
---------- UPDATED ANSWER --------
The previous workaround wasn't good enough for the production server, so I had to use development version from git repo installed as a submodule under vendor/prawn, as described here: https://github.com/sandal/prawn/wiki/Using-Prawn-in-Rails
The memory issue with the group method is gone, but the syntax/options for things have changed somewhat. So I had to rewrite the code to generate PDF.
Also, getting the submodule to play nicely with the git repo for the Rails app is difficult. Deployment to production was tough.
---------- ORIGINAL ANSWER --------
This isn't a fix, but it makes the problem take a few more group iterations before it manifests itself:
- override the Prawn::Document instance method named 'group'
- use the code from the 'group' function from the newest development version of prawn (from github.com)
The way I did this is that I added a file to the /lib folder of my Rails app. This file will include the Prawn gems and defined the mime type for a PDF document:
class PdfPrawn
require 'prawn'
require 'prawn/core'
require 'prawn/table'
MIME_TYPE = "application/pdf"
end
class Prawn::Document
def group(second_attempt=false)
old_bounding_box = @bounding_box
@bounding_box = SimpleDelegator.new(@bounding_box)
def @bounding_box.move_past_bottom
raise RollbackTransaction
end
success = transaction { yield }
@bounding_box = old_bounding_box
unless success
raise Prawn::Errors::CannotGroup if second_attempt
old_bounding_box.move_past_bottom
group(second_attempt=true) { yield }
end
success
end
end
And then in a model file, I define a method to generate my PDF and use something like this:
def to_pdf
require "#{File.expand_path(RAILS_ROOT)}/lib/pdf_prawn"
pdf = Prawn::Document.new
# code to add stuff to PDF
pdf.render
end
Related Topics
After Ruby 2.4 Upgrade - Error While Trying to Load The Gem 'Uglifier' (Bundler::Gemrequireerror)
How to Specify a Minimum Ruby Version in a Gemspec
More Efficient Ruby Way to Map Attribute in Array of Objects to Another Array
Ruby Group Hashes by Value of Key
Ruby on Rails Active Record Return Value When Create Fails
Ruby Syntax: Break Out from 'Each.. Do..' Block
Ruby String Strip Defined Characters
Why Array.Reverse_Each Is Faster Than Array.Reverse.Each
How to Make Single Digit Number a Two Digit Number in Ruby
"Cannot Load Such File - Rubygems/Defaults/Operating_System" When Installing Ruby 2.0.0
Ruby Comparison Operators? == VS. ===
Cancan Load_And_Authorize_Resource Triggers Forbidden Attributes
How to Best Create a Random Float in a Range Between Two Floats
Mocking Chain of Methods in Rspec
Rails 3. How to Explicitly Round a Number to Two Decimal Places in The Model