Prawn/PDF:一行有多种格式/文本?

aar*_*ona 2 pdf ruby-on-rails prawn prawnto

当我尝试以下代码时:

text "Hello "
text "World"
Run Code Online (Sandbox Code Playgroud)

它们在 World 之上呈现 Hello,而不是在 Hello 之后立即呈现 World。我在一行上需要的文本上有一些复杂的格式(突出显示、不同的字体大小等)。我知道该:inline_formatting选项存在,但使用该选项似乎太复杂了。

我有以下代码:

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
Run Code Online (Sandbox Code Playgroud)

order.pdf.prawn:

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

#code....

text "Authorized Signature: "
formatted_text [{:text => "_" * 15, :callback => highlight }], :size => 20
Run Code Online (Sandbox Code Playgroud)

正在生成附加图像。如何获得与文本相同级别的签名行?

在此处输入图片说明

dim*_*iax 7

红宝石 2.5.1

导轨 5.2.0

将方法更改text为 就足够了text_box,即:

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
Run Code Online (Sandbox Code Playgroud)

例子: 渲染的 PDF 页面