4

从我的控制器我创建pdf:

def show
    @order = Order.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @order }
      format.pdf do
        pdf = InvoicePdf.new(@order, view_context)

        send_data pdf.render, filename: "invoice_#{@order.order_number}.pdf",
                              type: "application/pdf",
                              disposition: "inline",
                              size: 10
      end
    end
  end

invoice_pdf.rb:

require 'barby'
require 'barby/barcode/code_39'
require 'barby/outputter/prawn_outputter'

class InvoicePdf < Prawn::Document
    def initialize(order, view)
        super({
            :top_margin => 70,
            :page_size => 'A4',
            :font_size => 10,
            :text  => 8
            })

        @order = order
        @view = view
        order_number
        barcode
    end

    def order_number
        text "Order #{@order.order_number}"
    end

    def barcode
        barcode = Barby::Code39.new @order.order_number
        barcode.annotate_pdf(XXXX)
    end
end

我应该如何修改我的条形码方法或标记为XXXX的选项以将条形码嵌入 PDF 文档?

4

2 回答 2

6

编辑

在您的InvoicePdf课程中,将barcode方法更改为:

def barcode
  barcode = Barby::Code39.new @order.order_number
  barcode.annotate_pdf(self)
end

annotate_pdf方法将 aPrawn::Document作为其参数,即self此处。


原始答案

如果你想用你的条形码创建一个新的 pdf,你可以这样做:

def barcode
  _barcode = Barby::Code39.new(@order.order_number)
  outputter = Barby::PrawnOutputter.new(_barcode)
  outputter.to_pdf
end

PrawnOutputter请注意,您可以在新建或to_pdf调用时指定 pdf 选项(包括高度、边距、页面大小等) 。有关更多详细信息,请参阅文档:https ://github.com/toretore/barby/wiki/Outputters和http://rdoc.info/github/toretore/barby/Barby/PrawnOutputter

如果您想将其写入文件,请执行以下操作:

File.open("my_file.pdf","w") { |f| f.print barcode }

请注意,您也可以只调用_barcode.to_pdf,这似乎与创建新的效果相同PrawnOutputter,但芭比文档中没有描述此功能。

如果您有想要写入条形码的现有 pdf 文档(作为 a Prawn::Document),请注意您可以执行以下操作:

def barcode(p_pdf)
  _barcode = Barby::Code39.new(@order.order_number)
  _barcode.annotate_pdf(p_pdf) 
end
于 2013-07-29T16:51:07.467 回答
1

就我而言,barcode.annotate_pdf(self)由于无法识别类似 的属性,因此引发了一些错误fill_color,因此我最终将Barby 条形码创建为 PNG图像,然后将其插入 PDF。像这样的东西:

require 'barby'
require 'barby/barcode/code_128'
require 'barby/outputter/png_outputter'

...

pdf = Prawn::Document.new(page_size: 'A4', margin: 20)


# Print data as text
order = "Order ##{@data.order_number} "
pdf.text_box order, size: 12, at: [0, pdf.cursor], width: 550

# Print data as image
barcode = Barby::Code128.new @data.order_number
barcode_blob = Barby::PngOutputter.new(barcode).to_png
# barcode.png is stored in tmp directory
File.open("tmp/barcode.png", 'wb'){|f| f.write barcode.to_png } 

pdf.image "#{Rails.root.to_s}/tmp/barcode.png", image_width: 50, image_height: 50, position: :center

...


pdf.render

选项 #2:使用 StringIO 而不是 tmp 文件

# Print data as image
barcode = Barby::Code128.new @data.order_number
barcode_blob = Barby::PngOutputter.new(barcode).to_png
barcode_io = StringIO.new(barcode_blob)
barcode_io.rewind
pdf.image(barcode_io, image_width: 50, image_height: 50, position: :center)
...
pdf.render
于 2020-05-06T21:38:56.423 回答