Prawnto Displaying Tables That Don't Break When New Page

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



Leave a reply



Submit