2

当我尝试以下代码时:

text "Hello "
text "World"

它们在 Hello 之后将 Hello 渲染在 World 之上而不是 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

order.pdf.prawn:

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

#code....

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

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

在此处输入图像描述

4

2 回答 2

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

例子:渲染的 PDF 页面

于 2018-08-02T23:39:21.877 回答
2

要将文本放置在确切的位置,您可以使用text_box选项:at

你可以得到你的文本的宽度pdf.width_of(str)(使用相同的样式选项:size等,否则它将使用默认设置来计算)

于 2017-02-18T07:39:14.607 回答