ruby-on-railspdfprawnprawnto

Prawn/PDF: Multiple formats/text on one line?


When I try the following code:

text "Hello "
text "World"

They render Hello on top of World instead of World right after Hello. I have some complicated formatting (highlighting, different font sizes etc) on text that I need on one line. I know that the :inline_formatting option exists but it seems this is too complicated to use that option.

I have the following code:

highlight_callback.rb:

class HighlightCallback
  def initialize(options)
    @color = options[:color]
    @document = options[:document]
  end

  def render_behind(fragment)
    original_color = @document.fill_color
    @document.fill_color = @color
    @document.fill_rectangle(fragment.top_left,
    fragment.width,
    fragment.height)

    @document.fill_color = original_color
  end
end

order.pdf.prawn:

highlight = HighlightCallback.new(:color => 'ffff00', :document => self)

#code....

text "Authorized Signature: "
formatted_text [{:text => "_" * 15, :callback => highlight }], :size => 20

which is producing the attached image. How can I get the signature line on the same level as the text?

enter image description here


Solution

  • To place text at a exact position you can use text_box with the option :at.

    You can get the width of your text with pdf.width_of(str) (use the same style optione :size etc. otherwise it will use the default settings to calculate)