ruby-on-railspdfruby-on-rails-5prawnstring-interpolation

Prawn PDF Inline Color Helper Method


So I'm using Prawn PDF gem for my rails application and I'm trying to write a helper method for part of a text string to be black when the rest of the string is blue, the reason I'm trying to write a helper is because this will be used for many different fields. Not sure if Prawn supports this but I've been giving it a shot.

text string

pad_bottom(5) { text "Owner: #{inline_black_helper(@order.first.owner)}", align: :center, inline_format: true, color: blue, size: 16 }

inline_black_helper

def inline_black_helper(text)
  "<color rbg='000000'>#{text}</color>"
end

Aka the same as doing

pad_bottom(5) { text "Owner: <color rbg='000000'>#{@order.first.owner}</color>", align: :center, inline_format: true, color: blue, size: 16 }

Obviously the last example works fine but as stated, since this will be used for many fields, having to write <color rbg='000000'>..</color> 50 times seems very 'un-rails' to me. This current implementation doesn't raise and error and the actual information is interpolating correctly but the entire text is blue instead of the first part being blue and the desired @order.first.owner being black. Any insight is appreciated as always, thanks!


Solution

  • Your helper is wrong. You need:

    <color rgb='000000'>

    You currently have rbg