Wrap text around an image in rails and prawn
-
01-10-2019 - |
Question
I have a document with dynamic image and dynamic text and would like the text around the image. The image is right aligned on the landscape page. Here is what I have so far:
pdf.bounding_box([0,pdf.bounds.top - 50], :width => pdf.bounds.width, :height => pdf.bounds.height-50) do
pdf.text @article.title, :size => 30, :style => :bold
pdf.text @article.content, :align => :left
# image
pdf.bounding_box([pdf.bounds.right - 250, pdf.bounds.top], :width => 250, :height => 250) do
pdf.image image_path, :width => 250
end
end
The text goes right underneath the image. I tried doing this ruby prawn how to wrap text around an aligned right image? but it didn't work.
Help is appreciated, thanks.
Solution
If you know the width and height of the image, you can use text_box to position a text box next to the image, and collect the returned string of the text that did not fit. Then create a second text box or an ordinary text() call below the image and text_box, and you should be good to go.
This example should help: http://github.com/sandal/prawn/blob/0.9.1/examples/text/text_box_returning_excess.rb
OTHER TIPS
I don't have much experience with prawn, so this is just a guess. Have you tried putting your pdf.text statements after the image bounding box?
pdf.bounding_box([0,pdf.bounds.top - 50],
:width => pdf.bounds.width,
:height => pdf.bounds.height-50) do
# image
pdf.bounding_box([pdf.bounds.right - 250, pdf.bounds.top],
:width => 250, :height => 250) do
pdf.image image_path, :width => 250
end
pdf.text @article.title, :size => 30, :style => :bold
pdf.text @article.content, :align => :left
end