aarona
aarona

Reputation: 37303

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

Upvotes: 2

Views: 3260

Answers (2)

dimpiax
dimpiax

Reputation: 12677

Ruby 2.5.1

Rails 5.2.0

It's enough to change method text to text_box, i.e.:

bounding_box([0, cursor], width: 540, height: 40) do
  stroke_color 'FFFF00'
  stroke_bounds

  date = 'Date: '
  text_box date, style: :bold

  text_box DateTime.now.strftime('%Y/%m/%d'), at: [bounds.left + width_of(date), cursor]
  text_box "Signature ________________", align: :right
end

Example: Rendered PDF Page

Upvotes: 8

Fallenhero
Fallenhero

Reputation: 1583

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)

Upvotes: 2

Related Questions