[英]Insert QR code into PDF using gem receipts in Ruby on Rails
I'm using gem 'receipts' by Chris Oliver, and I want to insert QRcode with payment details (to the footer section for example).我正在使用 Chris Oliver 的 gem“收据”,我想插入带有付款详细信息的 QRcode(例如到页脚部分)。 Models: Partner and Charge.
型号:Partner 和 Charge。 I want QR code to contain model attributes, like these: charge.partner.iban, charge.amount and something like that.
我希望二维码包含 model 个属性,例如:charge.partner.iban、charge.amount 等等。 Charge .rb has method receipt:
Charge .rb 有方法收据:
def receipt
Receipts::Invoice.new(
details: [
["Receipt Number", "123"],
["Date paid", Date.today],
["Payment method", "ACH super long super long super long super long super long"]
],
company: {
name: "Example, LLC",
address: "123 Fake Street\nNew York City, NY 10012",
email: "support@example.com",
logo: File.expand_path("./app/assets/images/logo.png")
},
recipient: [
self.partner.name,
self.partner.address,
"City, State Zipcode",
nil,
"customer@example.org"
],
line_items: [
["<b>Item</b>", "<b>Unit Cost</b>", "<b>Quantity</b>", "<b>Amount</b>"],
["Subscription", "$19.00", "1", "$19.00"],
[nil, nil, "Subtotal", "$19.00"],
[nil, nil, "Tax", "$1.12"],
[nil, nil, "Total", "$20.12"],
[nil, nil, "<b>Amount paid</b>", "$20.12"],
[nil, nil, "Refunded on #{Date.today}", "$5.00"]
],
footer: "Thanks for your business. Please contact us if you have any questions."
)
end
charges_controller has: charges_controller 有:
def show
respond_to do |format|
format.html
format.json
format.pdf { send_pdf }
end
end
private
def send_pdf
send_data @charge.receipt.render,
filename: "#{@charge.created_at.strftime("%Y-%m-%d")}-gorails-receipt.pdf",
type: "application/pdf",
disposition: :inline # or :attachment to download
end
charges/show.html.erb:费用/show.html.erb:
<%= link_to "View invoice", charge_path(@charge, format: :pdf) %>
I tried using prawn-qrcode, but couldn't make it.我尝试使用 prawn-qrcode,但无法成功。 Maximum what I get is something as line of text in the footer.
我得到的最多是页脚中的文本行。
When I put this in receipts method:
当我把它放在收据方法中时:
qrcode = RQRCode::QRCode.new(self.partner.ico.to_s)
png = qrcode.as_png(
resize_gte_to: false,
resize_exactly_to: false,
fill: 'white',
color: 'black',
size: 120,
border_modules: 4,
module_px_size: 6,
file: nil # path to write
)
and this in the footer: footer: ActionController::Base.helpers.image_tag(png.to_data_url)
这在页脚中:
footer: ActionController::Base.helpers.image_tag(png.to_data_url)
What should I do to insert QRcode with desired data?我应该怎么做才能插入带有所需数据的二维码? Is there any examples of similar task?
有没有类似任务的例子? Thanks
谢谢
Note : This is untested as-is but should work based on the documentation of the libraries themselves.注意:这是未经测试的,但应该基于库本身的文档工作。
receipts
uses prawn
to generate pdfs. receipts
使用prawn
生成 pdf。
Someone has created a QR Code renderer for prawn called prawn-qrcode
which uses rqrcode
so you may be able to use this as a simple bridge between the 2 libraries.有人为 prawn 创建了一个名为
prawn-qrcode
的 QR 代码渲染器,它使用rqrcode
,因此您可以将其用作两个库之间的简单桥梁。
Theoretical Example:理论例子:
def send_pdf
receipt = @charge.receipt
qr_code = RQRCode::QRCode.new(self.partner.ico.to_s)
receipt.render_qr_code(qr_code, extent: 72)
send_data receipt.render,
filename: "#{@charge.created_at.strftime("%Y-%m-%d")}-gorails-receipt.pdf",
type: "application/pdf",
disposition: :inline # or :attachment to download
end
Additional Notes:附加条款:
render_qr_code(qr_code, pos: [x,y])
render_qr_code(qr_code, pos: [x,y])
self.partner.ico.to_s
generatesself.partner.ico.to_s
生成了什么
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.