class Corpshort::HorizontalPdf

Public Class Methods

new(url:, base_url:, name:, flex: false) click to toggle source
# File lib/corpshort/horizontal_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 + padding
  end
end

Public Instance Methods

code_size() click to toggle source
# File lib/corpshort/horizontal_pdf.rb, line 28
def code_size
  cm2pt(3)
end
document() click to toggle source
# File lib/corpshort/horizontal_pdf.rb, line 95
def document
  render
end
h() click to toggle source
# File lib/corpshort/horizontal_pdf.rb, line 24
def h
  cm2pt(3)
end
padding() click to toggle source
# File lib/corpshort/horizontal_pdf.rb, line 32
def padding
  cm2pt(0.2)
end
pdf() click to toggle source
# File lib/corpshort/horizontal_pdf.rb, line 99
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/horizontal_pdf.rb, line 68
def render
  @pdf = nil

  pdf.fill_color 'FFFFFF'
  pdf.fill { pdf.rounded_rectangle [0, code_size], code_size, code_size, 10 }
  pdf.print_qr_code(@url, level: :m, 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/horizontal_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/horizontal_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/horizontal_pdf.rb, line 52
def url_box
  Prawn::Text::Formatted::Box.new(
    text,
    document: pdf,
    at: [code_size, code_size],
    width: w - code_size - padding - padding,
    height: h,
    overflow: :shrink_to_fit,
    min_font_size: nil,
    disable_wrap_by_char: true,
    align: :left,
    valign: :center,
    kerning: true,
  )
end
w() click to toggle source
# File lib/corpshort/horizontal_pdf.rb, line 20
def w
  @width || cm2pt(7)
end