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: "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:
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
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
Related
I am trying to download my dynamic content as a pdf file. The pdf is being generated but with no content in it, not even a simple text(when I test with that).
As I need a lot of data which I need to calculate and send parameters without refreshing page so I am doing it through an Ajax request.
$("#download_pdf").click(function() {
report_type = $("#report_type").val();
start_date = $("#start_date").val();
end_date = $("#end_date").val();
date_type = $("#date_type").val();
$.ajax({
data: { report_type: report_type, start_date: start_date, end_date: end_date, date_type: date_type },
url: '/reports/generate_pdf.pdf',
success: function(data) {
var blob=new Blob([data]);
var link=document.createElement('a');
link.href=window.URL.createObjectURL(blob);
link.download="Report_"+new Date()+".pdf";
link.click();
console.log("pdf printed");
}
});
});
Here is my ruby code in the controller:
def generate_pdf
#results = get_report_result_by_datetype(params[:report_type], params[:start_date], params[:end_date], params[:date_type])
#type = params[:report_type].to_i
#start_date = params[:start_date]
#end_date = params[:end_date]
respond_to do |format|
format.html
format.pdf do
render pdf: "report",
layout: 'pdf_layout',
template: 'reports/generate_pdf.html.erb',
encoding: 'UTF8',
print_media_type: true,
disposition: 'attachment',
page_size: 'letter',
orientation: 'landscape',
lowquality: 'false',
debug: true
end
end
end
There is no issue in the data which I am fetching.
Note: The most strange thing I noticed is that when the calculated array is small the generated pdf has one page and if it is big the generated pdf shows multiple pages.
I would really appreciate if someone points where I am going wrong.
Thanks in Advance!!
Although I don't know how to fix your wicked_pdf code, I can recommend the following using the gem prawn (which is not rails-specific and removes some of the Rails magic from the equation):
format.pdf do
pdf_path = Rails.root.join("tmp", "my_file_name.pdf")
template_path = Rails.root.join("app", "views", "reports", "generate_pdf.html.erb")
this = binding
Prawn::Document.generate(pdf_path) do
text Erb.new(template_path).result(this)
end
render file: pdf_path
end
Haven't actually tested it so apologies if it doesn't work.
I have the following controller code in a simple Rails API:
class Api::V1::AccountsController < ApplicationController
def index
render json: Account.all
end
def show
begin
render json: Account.includes(:cash_flows).find(params[:id]), include: :cash_flows
rescue ActiveRecord::RecordNotFound => e
head :not_found
end
end
end
The problem with this is that, the generated json have the format:
{
id:2,
name: 'Simple account',
cash_flows: [
{
id: 1,
amount: 34.3,
description: 'simple description'
},
{
id: 2,
amount: 1.12,
description: 'other description'
}
]
}
I need that my generated json is camelCase('cashFlows' instead of 'cash_flows')
Thanks in advance!!!
Following the recommended by #TomHert, I used JBuilder and the available config:
Keys can be auto formatted using key_format!, this can be used to convert keynames from the standard ruby_format to camelCase:
json.key_format! camelize: :lower
json.first_name 'David'
# => { "firstName": "David" }
You can set this globally with the class method key_format (from inside your environment.rb for example):
Jbuilder.key_format camelize: :lower
Thanks!!!
Hi I'm trying to implement downloading a PDF receipt. I'm not sure how to integrate Prawn with Rails 4 app and I couldn't find any tutorials on how to do this. Please see below for things that I have done. Could someone please suggest some articles to me or some tips.
1 Added Prawn gem and did bundle install
2 Added controller code to render PDF
respond_to do |format|
format.html
format.pdf do
pdf = OrderPdf.new(#order, view_context)
send_data pdf.render, filename: "order_#{#order.order_number}.pdf",
type: "application/pdf",
disposition: "inline"
end
end
3 Have a link_to code in the view. The view is located App > PDF
<%= link_to "Download PDF", order_path(#order, format: pdf) %>
I'm not sure which part you need help with, but here is how I've done it. In the code below, I am creating a pdf of a receipt and storing it in the database. The output looks like this - Sample Receipt
Perhaps it may be of some help.
class Omni::ReceiptWorksheet < Omni::Receipt
def print(receipt)
pdf = header receipt
data = []
data[0] = ["PO Nbr","Carton Nbr","Sku Nbr","Sku Description","S/U Open","S/U per Pack","Packs Open", "Packs Received"]
receipt.receipt_details.each_with_index do |detail,i|
selling_units = detail.purchase_detail.selling_units_approved - detail.purchase_detail.selling_units_received - detail.purchase_detail.selling_units_cancelled
data[i+1] = [detail.purchase.purchase_nbr,' ', detail.sku.sku_nbr, detail.sku.display, selling_units, detail.receipt_pack_size, selling_units / detail.receipt_pack_size, ' ']
end
pdf.move_down 110
pdf.table(data) do |t|
t.style(t.row(0), :background_color => '0075C9')
t.header = true
end
pdf.number_pages "page <page> of <total>", { :at => [pdf.bounds.right - 150, 0], width: 150, align: :right, page_filter: (1..50), start_count_at: 1, color: "002B82" }
attach StringIO.new(pdf.render), "receiving_worksheet#{Date.today}.pdf", receipt
end
def header(receipt)
pdf = Prawn::Document.new
pdf.font_size = 12
pdf.draw_text "Printed on: #{Date.today}", at: [0, 670]
pdf.draw_text "Receiving Worksheet", at: [220, 670]
pdf.draw_text "Page 1", at: [480, 670]
pdf.draw_text "Receipt #: #{receipt.receipt_nbr}", at: [0, 650]
pdf.draw_text "Receipt Date: #{Date.today}", at: [400, 650]
pdf.draw_text "Receiving Location: #{receipt.location_display}", at: [0, 640]
pdf.draw_text "Carrier Name: #{receipt.carrier_supplier.display}", at: [0, 620]
pdf.draw_text "Bill of Lading: #{receipt.bill_of_lading_number}", at: [450, 620]
pdf
end
def attach(file, file_name, receipt)
attachment = Buildit::Attachment.create(
attachable_type: "Omni::Receipt",
attachable_id: receipt.receipt_id,
file_name: file_name,
mime_type: 'application/pdf',
byte_size: file.size,
locale: 'en',
is_enabled: true
)
Buildit::Content.create(
contentable_type: "Buildit::Attachment",
contentable_id: attachment.attachment_id,
data: file.read
)
end
end
Below is my controller for uploading and downloading the attachment.
class ContentController < ActionController::Base
def download
content = Buildit::Content.find_by_content_id(params[:file_id])
contentable = content.contentable
file_name = (contentable ? contentable.file_name : 'file')
send_data content.data, :disposition => 'attachment', :filename => file_name
end # def download
def upload
begin
content = Buildit::Content.create(
data: params[:file].read
)
result = {
success: true,
content_id: content.content_id,
file_name: params[:file].original_filename,
mime_type: params[:file].content_type,
byte_size: params[:file].size
}
rescue
result = {success: false}
end
render text: result.to_json, status: 200
end # def upload
end # class ContentController
Good luck! Let me know if you need something more specific.
I found the prawn_rails gem useful. Note in your gemfile you need gem prawn_rails with the underscore, not a dash
In my Rails app I use Paperclip to upload photos and store them in S3. So I wanted to bring that functionality into my iOS app. I got image uploading to work in my RubyMotion app using this gist, but it was incredibly slow. After seeing this dated issue in Paperclip, I tried a different approach: https://github.com/thoughtbot/paperclip/issues/254#issuecomment-321507.
So I tried using BubbleWrap's :form_data :format and passing UIImage.UIImageJPEGRepresentation(#form.render[:photo], 1) instead to see if that would speed things up. However it isn't working. It seems like whatever photo is selected isn't actually getting rendered properly, because I'm not seeing any photo params in my server. And the output of UIImage.UIImageJPEGRepresentation(#form.render[:photo], 1) doesn't look right.
My Formotion form:
#form = Formotion::Form.new({
title: "Settings",
sections: [{
title: "Personal Information",
rows: [{
title: "Photo",
type: :image,
key: :photo,
value: #profile_photo
}, {
title: "Name",
type: :string,
placeholder: "Name",
key: :name,
value: #profile['name']
}]
}]
})
My BubbleWrap PUT to update the profile:
profile_photo = UIImage.UIImageJPEGRepresentation(#form.render[:photo], 1)
data = {
'profile[name]' => #form.render[:name],
'profile[photo]' => profile_photo,
'user[api_token]' => CONFIG.user.api_token,
_method: 'PUT'
}
BW::HTTP.post("#{DEFAULT_URL}/profiles/#{CONFIG.user.profile_id}", { format: :form_data, payload: data }) do |response|
parsed_response = BW::JSON.parse(response.body.to_str)
if response.ok?
#data = parsed_response
self.dismissViewControllerAnimated(true, completion:lambda { parent.load_profile() })
else
App.alert("#{parsed_response.first}")
end
end
So my question is: Must I encode the image like the gist suggests with .pack("m0")? Is there a way to speed up this process at all with all the binary data I'm passing to my server?
I have no idea to do such a thing with BubbleWrap but ...
.. here is an example to upload files with AFMotion(which is a wrapper around AFNetworking).
client = AFMotion::Client.build("your endpoint") do
header "Accept", "application/json"
operation :json
end
image = my_function.get_image
data = UIImagePNGRepresentation(image)
client.multipart.post("avatars") do |result, form_data|
if form_data
# Called before request runs
# see: https://github.com/AFNetworking/AFNetworking/wiki/AFNetworking-FAQ
form_data.appendPartWithFileData(data, name: "avatar", fileName:"avatar.png", mimeType: "image/png")
elsif result.success?
...
else
...
end
end
You might want to take a look at the documentation/example of AFMotion here
Hope it helps.
I have an article model (which belongs to a user) and holds the number of facebook_shares, linkedin_shares and twitter_shares. I'd like to sum the number of facebook/linkedin/twitter shares for each user's articles and return them into an array I can put into a chart.
This is how I'm currently doing it, returning the results in a =content_tag as per this Railcast:
= content_tag :div, "", id: "shares-chart", data: {shares:
[
{y: 'Tweets', a: #user.articles.sum(:twitter_shares)},
{y: 'Facebook', a: #user.articles.sum(:facebook_shares)},
{y: 'LinkedIn', a: #user.articles.sum(:linkedin_shares)}
]
}
But this seems like an ugly way. Is there a nicer way using .map? It's a function I can't figure out how to apply to columns, rather than records.
Maybe you want this:
In your User model:
def construct_data
[{"Tweets" => :twitter_shares}, {"Facebook" => :facebook_shares}, {"LinkedIn" => :linkedin_shares}].map do |h|
{y: h.keys.first, a: #user.articles.sum(h.values.first)}
end
end
In your controller action:
#chart_data = #user.construct_data
In your view:
= content_tag :div, "", id: "shares-chart", data: {shares: #chart_data}