jyurek
jyurek

Reputation: 1177

Group text and images as a block with Prawn?

I'm trying to build a PDF from user-generated content and I have a chunk of information that should be grouped together. I know of the group method to make sure text all gets rendered together, but this doesn't seem to work with a mix of text and images. Is there something that can do this with Prawn, or do I need to try to calculate cursor position and manually linebreak?

Edit: For illustration of what I'm looking to do:

pdf = PDF::Document.new
20.times do
  pdf.group do
    pdf.text "Something"
    pdf.image "path/to/image.jpg"
    pdf.text Time.now.to_s
  end
end

And I would expect to not ever have "Something" on one page and the image on the next, but that is what I see. Is there some way I can achieve what I want?

Upvotes: 2

Views: 1065

Answers (1)

Onno Faber
Onno Faber

Reputation: 661

Okay, I've figured it out. Prawn does not seem to take the image height into account when grouping, but you can hack your way around it:

pdf.group do
  pdf.text "Best regards, (...)"
  pdf.image "#{Rails.root}/vendor/signature.jpg", {
    :height => 30,
    :at => [0, pdf.y.to_i - @bottom_margin]
  }
  pdf.move_down(35)
  pdf.text " "
end

The trick is to use absolute positioning for the image, and move the text cursor down manually.

Upvotes: 4

Related Questions