class Corpshort::VerticalPdf
Public Class Methods
new(url:, base_url:, name:, flex: false)
click to toggle source
# File lib/corpshort/vertical_pdf.rb, line 8 def initialize(url:, base_url:, name:, flex: false) @url = url @base_url = base_url @name = name @flex = flex if @flex @width = code_size + required_width_for_url_box + padding + padding end end
Public Instance Methods
code_size()
click to toggle source
# File lib/corpshort/vertical_pdf.rb, line 28 def code_size cm2pt(3) end
document()
click to toggle source
# File lib/corpshort/vertical_pdf.rb, line 97 def document render end
h()
click to toggle source
# File lib/corpshort/vertical_pdf.rb, line 24 def h cm2pt(4) end
padding()
click to toggle source
# File lib/corpshort/vertical_pdf.rb, line 32 def padding cm2pt(0.2) end
pdf()
click to toggle source
# File lib/corpshort/vertical_pdf.rb, line 101 def pdf @pdf ||= Prawn::Document.new(page_size: [w,h], margin: 0).tap do |pdf| pdf.font_size = 12 end end
render()
click to toggle source
# File lib/corpshort/vertical_pdf.rb, line 68 def render @pdf = nil code_pos = [(w-code_size)/2.0, h] pdf.fill_color 'FFFFFF' pdf.fill { pdf.rounded_rectangle code_pos, code_size, code_size, 5 } pdf.print_qr_code(@url, level: :m, pos: code_pos, extent: code_size, stroke: false) [true, false].each do |dry_run| box = url_box() box.render(dry_run: dry_run) if dry_run pdf.fill_color 'FFFFFF' pdf.fill do pdf.rounded_rectangle( [box.at[0] - padding, box.at[1] + padding], box.available_width + padding + padding, box.height + padding + padding, 5, ) end pdf.fill_color '000000' end end pdf end
required_width_for_url_box()
click to toggle source
# File lib/corpshort/vertical_pdf.rb, line 44 def required_width_for_url_box doc = Prawn::Document.new(page_size: [cm2pt(5),cm2pt(5)], margin: 0) doc.font_size = 12 text.inject(0) do |r,t| r + doc.width_of(t.fetch(:text), style: t[:styles]&.first) end end
text()
click to toggle source
# File lib/corpshort/vertical_pdf.rb, line 36 def text [ {link: @url, text: @base_url}, {link: @url, text: '/'}, {link: @url, styles: [:bold], text: @name}, ] end
url_box()
click to toggle source
# File lib/corpshort/vertical_pdf.rb, line 52 def url_box Prawn::Text::Formatted::Box.new( text, document: pdf, at: [padding, (h-code_size)-padding], width: w - padding - padding, height: (h-code_size) - padding - padding, overflow: :shrink_to_fit, min_font_size: nil, disable_wrap_by_char: true, align: :center, valign: :center, kerning: true, ) end
w()
click to toggle source
# File lib/corpshort/vertical_pdf.rb, line 20 def w @width || cm2pt(6) end