rubyprawnprawnto

How do you add a border to an image with Prawn?


How can I add a border to an image with Prawn (pdf library)? If this is possible, can you add padding as well?


Solution

  • You can add a border using a bounding_box and stroke_bounds. In this example, I've put a border around an image. I've even given it padding of 15. It should be relatively easy to make this a function where a padding parameter could be used to calculate the difference between the image width and the bounding_box width.

    require 'prawn'
    
    Prawn::Document.generate("test.pdf") do
        text "Boxed Image", :align=>:center, :size=>20
        bounding_box([0, cursor], :width => 330) do
            move_down 15
            image "image.jpg", :fit => [300, 600], :position => :center
            move_down 15
            stroke_bounds
        end
    end