Class: Corpshort::VerticalPdf

Inherits:
Object
  • Object
show all
Includes:
Prawn::Measurements
Defined in:
lib/corpshort/vertical_pdf.rb

Instance Method Summary collapse

Constructor Details

#initialize(url:, base_url:, name:, flex: false) ⇒ VerticalPdf

Returns a new instance of VerticalPdf.



8
9
10
11
12
13
14
15
16
17
18
# 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

Instance Method Details

#code_sizeObject



28
29
30
# File 'lib/corpshort/vertical_pdf.rb', line 28

def code_size
  cm2pt(3)
end

#documentObject



97
98
99
# File 'lib/corpshort/vertical_pdf.rb', line 97

def document
  render
end

#hObject



24
25
26
# File 'lib/corpshort/vertical_pdf.rb', line 24

def h
  cm2pt(4)
end

#paddingObject



32
33
34
# File 'lib/corpshort/vertical_pdf.rb', line 32

def padding
  cm2pt(0.2)
end

#pdfObject



101
102
103
104
105
# 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

#renderObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# 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_boxObject



44
45
46
47
48
49
50
# 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

#textObject



36
37
38
39
40
41
42
# 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_boxObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# 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

#wObject



20
21
22
# File 'lib/corpshort/vertical_pdf.rb', line 20

def w
  @width || cm2pt(6)
end