Ruby on rails Prawn/PDF:一行上有多种格式/文本?

Ruby on rails Prawn/PDF:一行上有多种格式/文本?,ruby-on-rails,pdf,prawn,prawnto,Ruby On Rails,Pdf,Prawn,Prawnto,当我尝试以下代码时: text "Hello " text "World" 他们在世界之巅打招呼,而不是在打招呼之后打招呼。我有一些复杂的格式(突出显示,不同的字体大小等),我需要在一行文字。我知道存在:inline_formatting选项,但使用该选项似乎太复杂了 我有以下代码: text "Hello " text "World" 突出显示\u callback.rb: class HighlightCallback def initialize(options) @col

当我尝试以下代码时:

text "Hello "
text "World"
他们在世界之巅打招呼,而不是在打招呼之后打招呼。我有一些复杂的格式(突出显示,不同的字体大小等),我需要在一行文字。我知道存在
:inline_formatting
选项,但使用该选项似乎太复杂了

我有以下代码:

text "Hello "
text "World"
突出显示\u 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
highlight = HighlightCallback.new(:color => 'ffff00', :document => self)

#code....

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

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
highlight = HighlightCallback.new(:color => 'ffff00', :document => self)

#code....

text "Authorized Signature: "
formatted_text [{:text => "_" * 15, :callback => highlight }], :size => 20
正在生成附加的图像。如何获得与文本相同级别的签名行


要将文本放置在准确的位置,您可以使用带有
:at
选项的
文本框

您可以使用
pdf.width\u of(str)
(使用相同的样式optione
:size
等,否则将使用默认设置进行计算)

Ruby 2.5.1 Rails 5.2.0 将方法
文本
更改为
文本框
,即:

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

嗯,从来没有这样做过。我想你必须自己移动指针。但是prawn可以轻松计算文本的宽度我如何移动指针?我觉得我应该能够在第一个文本之后给它一组坐标来重置光标,然后在我的文本之后继续在同一位置。您可能需要使用
文本框
:at
选项,谢谢。我试试看。如果那样行得通,我会让你知道的。对这个问题作一个完整的回答?我将投票并将其标记为正确。这就成功了!发布一个详细的答案,我会把它标记为正确的,并投赞成票。