I'm using gem 'receipts' by Chris Oliver, and I want to insert QRcode with payment details (to the footer section for example). Models: Partner and Charge. I want QR code to contain model attributes, like these: charge.partner.iban, charge.amount and something like that. Charge.rb has method receipt:
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: "[email protected]",
logo: File.expand_path("./app/assets/images/logo.png")
},
recipient: [
self.partner.name,
self.partner.address,
"City, State Zipcode",
nil,
"[email protected]"
],
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:
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:
<%= link_to "View invoice", charge_path(@charge, format: :pdf) %>
I tried using prawn-qrcode, but couldn't make it. 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)
What should I do to insert QRcode with desired data? Is there any examples of similar task? Thanks
CodePudding user response:
Note: This is untested as-is but should work based on the documentation of the libraries themselves.
receipts
uses prawn
to generate pdfs.
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.
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:
- You may have to play around with positioning e.g.
render_qr_code(qr_code, pos: [x,y])
- I am not sure what
self.partner.ico.to_s
generates