-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support native Prawn template #27
base: master
Are you sure you want to change the base?
Changes from all commits
60f35a4
f12e6db
91e149c
b0046bb
52bd8c7
7086604
222c659
cf7e639
dbe8d43
7602acd
b6d81f4
71ca086
6d0035a
c4bb3d0
3991dd7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
2.7.1 | ||
2.7.6 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,10 +2,11 @@ | |
require 'qr-bills/qr-exceptions' | ||
require 'qr-bills/qr-params' | ||
require 'qr-bills/qr-html-layout' | ||
require 'qr-bills/qr-prawn-layout' | ||
require 'qr-bills/qr-creditor-reference' | ||
|
||
module QRBills | ||
def self.generate(qr_params) | ||
def self.generate(qr_params, pdf = nil) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thanks for the PR! I would find better to pass an additional params directly in the |
||
raise ArgumentError, "#{QRExceptions::INVALID_PARAMETERS}: bill type param not set" unless qr_params.has_key?(:bill_type) | ||
raise ArgumentError, "#{QRExceptions::INVALID_PARAMETERS}: validation failed" unless QRParams.valid?(qr_params) | ||
|
||
|
@@ -19,6 +20,8 @@ def self.generate(qr_params) | |
output = case qr_params[:output_params][:format] | ||
when 'html' | ||
QRHTMLLayout.create(qr_params) | ||
when 'prawn' | ||
QRPRAWNLayout.create(qr_params, pdf) | ||
else | ||
QRGenerator.create(qr_params, qr_params[:qrcode_filepath]) | ||
end | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
require 'qr-bills/qr-generator' | ||
require 'prawn' | ||
require 'prawn-svg' | ||
require "prawn/measurement_extensions" | ||
|
||
module QRPRAWNLayout | ||
attr_reader :document, :type | ||
|
||
def self.create(params, pdf = nil) | ||
qrcode = QRGenerator.create(params, params[:qrcode_filepath]) | ||
params[:qrcode_filepath] = convert_qrcode_to_data_url(qrcode) | ||
prawn_layout(params, pdf) | ||
end | ||
|
||
def self.convert_qrcode_to_data_url(qrcode) | ||
# Stolen from sprockets | ||
# https://github.com/rails/sprockets/blob/0f3e0e93dabafa8f3027e8036e40fd08902688c8/lib/sprockets/context.rb#L295-L303 | ||
data = CGI.escape(qrcode) | ||
data.gsub!('%3D', '=') | ||
data.gsub!('%3A', ':') | ||
data.gsub!('%2F', '/') | ||
data.gsub!('%27', "'") | ||
data.tr!('+', ' ') | ||
|
||
"data:image/svg+xml;charset=utf-8,#{data}" | ||
end | ||
|
||
def self.prawn_layout(params, pdf) | ||
pdf.canvas do | ||
I18n.with_locale(params[:bill_params][:language]) do | ||
pdf.bounding_box([0, 105.mm], width: 210.mm, height: 105.mm) do | ||
y_pos = pdf.cursor | ||
|
||
# Receipt Panel | ||
pdf.bounding_box([5.mm, y_pos], width: 52.mm, height: 105.mm) do | ||
pdf.move_down 5.mm | ||
|
||
pdf.text I18n.t("qrbills.receipt").capitalize, size: 11.pt, style: :bold | ||
pdf.move_down 4.mm | ||
|
||
pdf.text "#{I18n.t("qrbills.account").capitalize} / #{I18n.t("qrbills.payable_to").capitalize}", size: 6.pt, style: :bold | ||
pdf.text "#{params[:bill_params][:creditor][:iban]}", size: 8.pt | ||
pdf.text "#{render_address(params[:bill_params][:creditor][:address])}", size: 8.pt, inline_format: true | ||
|
||
if !params[:bill_params][:reference].nil? && !params[:bill_params][:reference].empty? | ||
pdf.move_down 4.mm | ||
pdf.text I18n.t("qrbills.reference").capitalize, size: 6.pt, style: :bold | ||
pdf.text "#{params[:bill_params][:reference]}", size: 8.pt | ||
end | ||
pdf.move_down 4.mm | ||
|
||
pdf.text I18n.t("qrbills.payable_by").capitalize, size: 6.pt, style: :bold | ||
pdf.text "#{render_address(params[:bill_params][:debtor][:address])}", size: 8.pt, inline_format: true | ||
pdf.move_down 8.mm | ||
|
||
bounding_box_cursor = pdf.cursor | ||
pdf.bounding_box([0, bounding_box_cursor], width: 20.mm) do | ||
pdf.text I18n.t("qrbills.currency").capitalize, size: 6.pt, style: :bold | ||
pdf.text "#{params[:bill_params][:currency]}", size: 8.pt | ||
end | ||
|
||
pdf.bounding_box([20.mm, bounding_box_cursor], width: 20.mm) do | ||
pdf.text I18n.t("qrbills.amount").capitalize, size: 6.pt, style: :bold | ||
pdf.text "#{format('%.2f', params[:bill_params][:amount])}", size: 8.pt | ||
end | ||
|
||
pdf.move_down 6.mm | ||
pdf.text I18n.t("qrbills.acceptance_point"), align: :right, size: 6.pt, style: :bold | ||
end | ||
|
||
# Payment Panel - QR code sub-section | ||
pdf.bounding_box([67.mm, y_pos], width: 51.mm, height: 90.mm) do | ||
pdf.move_down 5.mm | ||
|
||
pdf.text I18n.t("qrbills.payment_part").capitalize, size: 11.pt, style: :bold | ||
pdf.move_down 5.mm | ||
|
||
pdf.svg QRGenerator.build_svg(params[:bill_params]), width: 46.mm, height: 46.mm | ||
pdf.move_down 5.mm | ||
|
||
payment_currency_bounding_box_cursor = pdf.cursor | ||
pdf.bounding_box([0, payment_currency_bounding_box_cursor], width: 20.mm) do | ||
pdf.text I18n.t("qrbills.currency").capitalize, size: 8.pt, style: :bold | ||
pdf.text "#{params[:bill_params][:currency]}", size: 10.pt | ||
end | ||
|
||
pdf.bounding_box([20.mm, payment_currency_bounding_box_cursor], width: 20.mm) do | ||
pdf.text I18n.t("qrbills.amount").capitalize, size: 8.pt, style: :bold | ||
pdf.text "#{format('%.2f', params[:bill_params][:amount])}", size: 10.pt | ||
end | ||
end | ||
|
||
# Payment Panel - Account / Payable to sub-section | ||
pdf. bounding_box([118.mm, y_pos], width: 92.mm, height: 90.mm) do | ||
pdf.move_down 5.mm | ||
|
||
pdf.text "#{I18n.t("qrbills.account").capitalize} / #{I18n.t("qrbills.payable_to").capitalize}", size: 8.pt, style: :bold | ||
pdf.text "#{params[:bill_params][:creditor][:iban]}", size: 10.pt | ||
pdf.text "#{render_address(params[:bill_params][:creditor][:address])}", size: 10.pt, inline_format: true | ||
|
||
if !params[:bill_params][:reference].nil? && !params[:bill_params][:reference].empty? | ||
pdf.move_down 4.mm | ||
pdf.text "#{I18n.t("qrbills.reference").capitalize}", size: 8.pt, style: :bold | ||
pdf.text "#{params[:bill_params][:reference]}", size: 10.pt | ||
end | ||
|
||
if !params[:bill_params][:additionally_information].nil? && !params[:bill_params][:additionally_information].empty? | ||
pdf.move_down 4.mm | ||
pdf.text "#{I18n.t("qrbills.additional_information").capitalize}", size: 8.pt, style: :bold | ||
pdf.text "#{params[:bill_params][:additionally_information]}", size: 10.pt | ||
end | ||
pdf.move_down 4.mm | ||
|
||
pdf.text "#{I18n.t("qrbills.payable_by").capitalize}", size: 8.pt, style: :bold | ||
pdf.text "#{render_address(params[:bill_params][:debtor][:address])}", size: 10.pt, inline_format: true | ||
end | ||
|
||
# Payment Panel - Further information sub-section | ||
pdf.bounding_box([67.mm, y_pos - 85.mm], width: 138.mm, height: 10.mm) do | ||
if !params[:bill_params][:bill_information_coded].nil? && !params[:bill_params][:bill_information_coded].empty? | ||
pdf.text "<b>#{I18n.t("qrbills.name").capitalize}</b> AV1: #{params[:bill_params][:bill_information_coded]}", size: 7.pt, inline_format: true | ||
end | ||
|
||
if !params[:bill_params][:alternative_scheme_parameters].nil? && !params[:bill_params][:alternative_scheme_parameters].empty? | ||
pdf.text "<b>#{I18n.t("qrbills.name").capitalize}</b> AV2: #{params[:bill_params][:alternative_scheme_parameters]}", size: 7.pt, inline_format: true | ||
end | ||
end | ||
|
||
pdf.stroke_color("808080") | ||
pdf.dash(2, space: 2) | ||
pdf.stroke_vertical_line 0, 105.mm, at: 62.mm | ||
pdf.stroke_horizontal_line 0, 210.mm, at: 105.mm | ||
pdf.undash | ||
|
||
end | ||
end | ||
end | ||
end | ||
|
||
def self.render_address(address) | ||
case address[:type] | ||
when 'S' | ||
format("%s<br>%s %s<br>%s %s<br>", address[:name], address[:line1], address[:line2], address[:postal_code], address[:town]) | ||
when 'K' | ||
format("%s<br>%s<br>%s<br>", address[:name], address[:line1], address[:line2]) | ||
end | ||
end | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thanks for this, I would need to test it accordingly |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -54,15 +54,18 @@ | |
|
||
it "fails if currency type is empty" do | ||
@params[:bill_params][:currency] = "" | ||
@params[:bill_type] = QRParams::QR_BILL_WITH_QR_REFERENCE | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why this and the following rows are been added? |
||
expect{QRParams.base_params_valid?(@params)}.to raise_error(ArgumentError, "QR-bill invalid parameters: currency cannot be blank") | ||
end | ||
|
||
it "fails if currency is nil" do | ||
@params[:bill_params][:currency] = nil | ||
@params[:bill_type] = QRParams::QR_BILL_WITH_QR_REFERENCE | ||
expect{QRParams.base_params_valid?(@params)}.to raise_error(ArgumentError, "QR-bill invalid parameters: currency cannot be blank") | ||
end | ||
|
||
it "succeeds if the previous params are correctly set" do | ||
@params[:bill_type] = QRParams::QR_BILL_WITH_QR_REFERENCE | ||
expect{QRParams.base_params_valid?(@params)}.not_to raise_error | ||
expect(QRParams.base_params_valid?(@params)).to be_truthy | ||
end | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
require 'i18n' | ||
require 'fileutils' | ||
require 'qr-bills/qr-prawn-layout' | ||
require 'qr-bills/qr-params' | ||
|
||
RSpec.configure do |config| | ||
config.before(:each) do | ||
@pdf = Prawn::Document.new(:page_size => 'A4') | ||
@params = QRParams.get_qr_params | ||
@params[:fonts][:eot] = "../web/assets/fonts/LiberationSans-Regular.eot" | ||
@params[:fonts][:woff] = "../web/assets/fonts/LiberationSans-Regular.woff" | ||
@params[:fonts][:ttf] = "../web/assets/fonts/LiberationSans-Regular.ttf" | ||
@params[:fonts][:svg] = "../web/assets/fonts/LiberationSans-Regular.svg" | ||
@params[:locales][:path] = "config/locales/" | ||
@params[:qrcode_format] = 'svg' | ||
@params[:bill_params][:creditor][:iban] = "CH9300762011623852957" | ||
@params[:bill_params][:creditor][:address][:type] = "S" | ||
@params[:bill_params][:creditor][:address][:name] = "Compagnia di assicurazione forma & scalciante" | ||
@params[:bill_params][:creditor][:address][:line1] = "Via cantonale" | ||
@params[:bill_params][:creditor][:address][:line2] = "24" | ||
@params[:bill_params][:creditor][:address][:postal_code] = "3000" | ||
@params[:bill_params][:creditor][:address][:town] = "Lugano" | ||
@params[:bill_params][:creditor][:address][:country] = "CH" | ||
@params[:bill_params][:amount] = 12345.15 | ||
@params[:bill_params][:currency] = "CHF" | ||
@params[:bill_params][:debtor][:address][:type] = "S" | ||
@params[:bill_params][:debtor][:address][:name] = "Foobar Barfoot" | ||
@params[:bill_params][:debtor][:address][:line1] = "Via cantonale" | ||
@params[:bill_params][:debtor][:address][:line2] = "25" | ||
@params[:bill_params][:debtor][:address][:postal_code] = "3001" | ||
@params[:bill_params][:debtor][:address][:town] = "Comano" | ||
@params[:bill_params][:debtor][:address][:country] = "CH" | ||
@params[:bill_params][:reference] = "RF89MTR81UUWZYO48NY55NP3" | ||
@params[:bill_params][:reference_type] = "SCOR" | ||
@params[:bill_params][:additionally_information] = "pagamento riparazione monopattino" | ||
|
||
I18n.load_path << File.join(@params[:locales][:path], "qrbills.it.yml") | ||
I18n.load_path << File.join(@params[:locales][:path], "qrbills.en.yml") | ||
I18n.load_path << File.join(@params[:locales][:path], "qrbills.de.yml") | ||
I18n.load_path << File.join(@params[:locales][:path], "qrbills.fr.yml") | ||
I18n.default_locale = :it | ||
end | ||
end | ||
|
||
RSpec.describe "QRPRAWNLayout" do | ||
before do | ||
FileUtils.mkdir_p "#{Dir.pwd}/tmp/" | ||
File.delete filepath if File.exist?(filepath) | ||
end | ||
|
||
let(:filepath) { "#{Dir.pwd}/tmp/prawn-layout.pdf" } | ||
|
||
describe "layout generation" do | ||
before do | ||
@params[:qrcode_format] = 'svg' | ||
end | ||
|
||
it "successfully generates prawn/ruby layout + qr code" do | ||
expect{QRPRAWNLayout.create(@params, @pdf)}.not_to raise_error | ||
end | ||
|
||
it "generates svg qrcode" do | ||
expect(@params[:qrcode_filepath]).to_not include("data:image/svg+xml;") | ||
|
||
prawn_output = QRPRAWNLayout.create(@params, @pdf) | ||
IO.binwrite(filepath, prawn_output) | ||
expect(File.exist?(filepath)).to be_truthy | ||
|
||
expect(@params[:qrcode_filepath]).to include("data:image/svg+xml;") | ||
end | ||
|
||
it "does not overwrite locale" do | ||
@params[:bill_params][:language] = :de | ||
|
||
QRPRAWNLayout.create(@params, @pdf) | ||
|
||
expect(I18n.locale).to be :it | ||
end | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why was this removed?