如何使用 Prawn/labels 将图像放在标签上?

How to put an image on a label using Prawn/labels?

我使用 Ruby 编写脚本,Prawn-labels 打印书籍的 ISBN 标签,使用 Barby 生成 EAN-13 条形码。

ISBN 来自 MySQL。那很好用。我可以使用 Prawn/labels 创建包含书名的 30 个标签的 PDF。效果不错。

但是,我不太喜欢将正确的 EAN-13 条形码的 Barby-generated PNG 图像放在标签上。

这里是从数据库中获取信息并生成 EAN-13,非常简单:

query = "SELECT ISBN13, Title FROM s_library WHERE Has_barcode != 'y'"
result = client.query(query, symbolize_keys: true)
tags = []
result.each do |row|
    tags << [row[:ISBN13], row[:Title], Barby::EAN13.new(row[:ISBN13].chop)]
end
tags.sort! {|a, b| a[1] <=> b[1]} # Sort by book title

所以现在我有一个 two-dimensional 数组,每行包含一个 ISBN 字符串、一个标题字符串和一个包含 ISBN 的 EAN-13 编码的 Barby (# ).

现在我正在按照 Prawn::Labels 的示例进行操作,如果我不尝试 #embed_image PNG 数据,我可以成功生成 30 个标签,其中包含每本书的标题:

Prawn::Labels.generate('EAN_tags.pdf', tags, :type => 'Avery5160') do |pdf, tag|
    pdf.font '/Library/Fonts/Erasdemi.TTF'
    pdf.text(tag[1], :align => :center)
    png = Barby::PngOutputter.new(tag[2]).to_png
#   pdf.embed_image(png, Prawn::Images::PNG.new(png), {})
end
`open EAN_tags.pdf`

当我 un-comment 图像嵌入时,我得到一个我不太明白的神秘回溯:

    25: from /Users/jan/bin/Print_ISBN13.rb:73:in `<main>'
    24: from /usr/local/lib/ruby/gems/2.7.0/gems/prawn-labels-1.2.6/lib/prawn/labels.rb:18:in `generate'
    23: from /usr/local/lib/ruby/gems/2.7.0/gems/prawn-2.3.0/lib/prawn/document.rb:408:in `render_file'
    22: from /usr/local/lib/ruby/gems/2.7.0/gems/prawn-2.3.0/lib/prawn/document.rb:408:in `open'
    21: from /usr/local/lib/ruby/gems/2.7.0/gems/prawn-2.3.0/lib/prawn/document.rb:408:in `block in render_file'
    20: from /usr/local/lib/ruby/gems/2.7.0/gems/prawn-2.3.0/lib/prawn/document.rb:400:in `render'
    19: from /usr/local/lib/ruby/gems/2.7.0/gems/pdf-core-0.8.1/lib/pdf/core/renderer.rb:167:in `render'
    18: from /usr/local/lib/ruby/gems/2.7.0/gems/pdf-core-0.8.1/lib/pdf/core/renderer.rb:202:in `render_body'
    17: from /usr/local/lib/ruby/gems/2.7.0/gems/pdf-core-0.8.1/lib/pdf/core/document_state.rb:72:in `render_body'
    16: from /usr/local/lib/ruby/gems/2.7.0/gems/pdf-core-0.8.1/lib/pdf/core/object_store.rb:70:in `each'
    15: from /usr/local/lib/ruby/gems/2.7.0/gems/pdf-core-0.8.1/lib/pdf/core/object_store.rb:70:in `each'
    14: from /usr/local/lib/ruby/gems/2.7.0/gems/pdf-core-0.8.1/lib/pdf/core/object_store.rb:71:in `block in each'
    13: from /usr/local/lib/ruby/gems/2.7.0/gems/pdf-core-0.8.1/lib/pdf/core/document_state.rb:78:in `block in render_body'
    12: from /usr/local/lib/ruby/gems/2.7.0/gems/pdf-core-0.8.1/lib/pdf/core/reference.rb:26:in `object'
    11: from /usr/local/lib/ruby/gems/2.7.0/gems/pdf-core-0.8.1/lib/pdf/core/pdf_object.rb:87:in `pdf_object'
    10: from /usr/local/lib/ruby/gems/2.7.0/gems/pdf-core-0.8.1/lib/pdf/core/pdf_object.rb:87:in `each'
     9: from /usr/local/lib/ruby/gems/2.7.0/gems/pdf-core-0.8.1/lib/pdf/core/pdf_object.rb:93:in `block in pdf_object'
     8: from /usr/local/lib/ruby/gems/2.7.0/gems/pdf-core-0.8.1/lib/pdf/core/pdf_object.rb:87:in `pdf_object'
     7: from /usr/local/lib/ruby/gems/2.7.0/gems/pdf-core-0.8.1/lib/pdf/core/pdf_object.rb:87:in `each'
     6: from /usr/local/lib/ruby/gems/2.7.0/gems/pdf-core-0.8.1/lib/pdf/core/pdf_object.rb:93:in `block in pdf_object'
     5: from /usr/local/lib/ruby/gems/2.7.0/gems/pdf-core-0.8.1/lib/pdf/core/pdf_object.rb:87:in `pdf_object'
     4: from /usr/local/lib/ruby/gems/2.7.0/gems/pdf-core-0.8.1/lib/pdf/core/pdf_object.rb:87:in `each'
     3: from /usr/local/lib/ruby/gems/2.7.0/gems/pdf-core-0.8.1/lib/pdf/core/pdf_object.rb:93:in `block in pdf_object'
     2: from /usr/local/lib/ruby/gems/2.7.0/gems/pdf-core-0.8.1/lib/pdf/core/pdf_object.rb:75:in `pdf_object'
     1: from /usr/local/lib/ruby/gems/2.7.0/gems/pdf-core-0.8.1/lib/pdf/core/pdf_object.rb:25:in `utf8_to_utf16'
/usr/local/lib/ruby/gems/2.7.0/gems/pdf-core-0.8.1/lib/pdf/core/pdf_object.rb:25:in `encode': "\x89" to UTF-8 in conversion from ASCII-8BIT to UTF-8 to UTF-16BE (Encoding::UndefinedConversionError)

我可以将 png 变量转储到文件中并查看条形码。

从堆栈来看,它似乎正在尝试将 PNG 数据解释为 UTF-16 字符串。啊。没想到。

关于我做错了什么的任何线索?

使用 pdf.image(png) 而不是 pdf.embed_image。这是 pdf.embed_image 的包装器,它完成从 PNG 文件中获取必要数据的必要工作。

答案似乎是使用一种叫做 StringIO 的东西,它显然为内存中(blob)图像提供了一个 File 接口。

所以,我在下面添加了中间线:

    png = Barby::PngOutputter.new(tag[2]).to_png
    str_png = StringIO.new(png)
    pdf.image(str_png)

而不是尝试通过 embed_image 传递二进制数据,我现在在最终的 PDF 输出中获取条形码。 (它仍然需要一些调整,但基本概念现在可以使用了。)

我在这里搜索了很多方法——真的!但是“blob”这个词在这个网站上得到了answer