ruby-on-rails - Prawn/PDF : Multiple formats/text on one line?

标签 ruby-on-rails pdf prawn prawnto

当我尝试以下代码时:

text "Hello "
text "World"

他们在 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

order.pdf.prawn:

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

#code....

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

正在生成附加图像。如何让签名行与正文处于同一层级?

enter image description here

最佳答案

ruby 2.5.1

rails 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

示例:Rendered PDF Page

关于ruby-on-rails - Prawn/PDF : Multiple formats/text on one line?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42278149/

相关文章:

ruby-on-rails - FactoryGirl 与协会

ruby-on-rails - 现有 Postgres 表与新表中的 Json 列

ruby-on-rails - ActiveAdmin:为 has_many/belongs_to 关系生成表单下拉列表

c# - 单元格内的 ITextSharp 图像定位

c# - 无法使用iTextSharp读取一些PDF文件

ruby-on-rails - Prawn :带页码的目录

ruby-on-rails - Redis + websocket-rails(将 Redis 用于不止一项服务)

pdf - 将 PDF 转换为 JPEG 时,如何防止带有法语口音的字符消失?

ruby-on-rails - 如何使用prawn在pdf中插入图像