【发布时间】:2023-01-28 04:35:35
【问题描述】:
我正在使用 Chris Oliver 的 gem“收据”,我想插入带有付款详细信息的 QRcode(例如到页脚部分)。 型号:Partner 和 Charge。 我希望二维码包含模型属性,例如:charge.partner.iban、charge.amount 等等。收费.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 有:
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
费用/show.html.erb:
<%= link_to "View invoice", charge_path(@charge, format: :pdf) %>
我尝试使用 prawn-qrcode,但无法成功。 我得到的最多是页脚中的文本行。 当我把它放在收据方法中时:
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
)
这在页脚中:
footer: ActionController::Base.helpers.image_tag(png.to_data_url)
我应该怎么做才能插入带有所需数据的二维码?有没有类似任务的例子? 谢谢
【问题讨论】:
标签: ruby-on-rails pdf qr-code prawn receipt