Skip to content

Gradient Transformations

Dan Allen edited this page Aug 11, 2015 · 2 revisions

By default, Prawn 2.x and earlier failed to position gradients correctly when created inside a transformation, such as in a #scale block. During the 2.x development cycle we added an apply_transformations: true option to the #fill_gradient and #stroke_gradient methods. Setting this value to true will correctly apply all transformations to that gradient, and will be the default behavior starting with Prawn 3, to be released some time in the future.

To upgrade your code, just pass the additional option.

require 'prawn'

# Before
Prawn::Document.generate("before.pdf") do
  rotate(45) do
    fill_gradient [50, 100], [150, 200], 'ff0000', '0000ff'
    fill_rectangle [50, 100], 100, 100
  end
end

# After
Prawn::Document.generate("after.pdf") do
  rotate(45) do
    fill_gradient [50, 100], [150, 200], 'ff0000', '0000ff', apply_transformations: true
    fill_rectangle [50, 100], 100, 100
  end
end

If you see the following output, you are still using the incorrect gradient transformation and need to upgrade.

Gradients in Prawn 2.x and lower are not correctly positioned when a transformation has been made to the document. Pass 'apply_transformations: true' to correctly transform the gradient, or see > https://github.com/prawnpdf/prawn/wiki/Gradient-Transformations for more information.

Clone this wiki locally