Question

Is it possible to clip or apply a mask to an image using Prawn.

For example, I'm embedding an image into the PDF using image http://path/to/image. The image is square, but the PDF design requires a circle.

With HTML/ CSS I would apply a radius to the image to achieve this effect. Is there any way to do something similar with Prawn?

Était-ce utile?

La solution

Based on sunil-antony's answer, I came up with the following solution (using save_graphics_state to enclose drawing instructions, see Prawn documentation):

Prawn::Document.generate("x.pdf") do 
  image_width = 200
  image_x = 100
  image_y = 100 

  save_graphics_state do
    soft_mask do 
      fill_color 0,0,0,0 
      fill_circle [image_x + image_width/2, image_y - image_width/2], image_width/2
    end 

    image "example.jpg", at: [image_x, image_y], width: image_width, height: image_width
  end
end 

Autres conseils

I did it with this code:

Prawn::Document.generate("x.pdf") do 
    width, height = 200, 200 

    soft_mask do 
        fill_color 0,0,0,0 
        fill_circle [100, bounds.top - 100], 100 
    end 

    image "example.jpg", :width => width, :height => height 
end 
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top