Class: Renalware::Events::EventPdf

Inherits:
Object
  • Object
show all
Includes:
Prawn::View
Defined in:
app/pdfs/renalware/events/event_pdf.rb

Overview

We have used wicked_pdf (which shells out to wkhtmltopdf) up to know, but using prawn here as it Event PDFs are somewhat simpler, and there is no extant html equivalent we can render. Its also a useful test to see if this is a better approach all round. It is certainly much quicker and less resource intensive to create the PDFs.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(event) ⇒ EventPdf

Returns a new instance of EventPdf.



18
19
20
21
# File 'app/pdfs/renalware/events/event_pdf.rb', line 18

def initialize(event)
  @event = event
  build
end

Instance Attribute Details

#eventObject (readonly)

Returns the value of attribute event.



14
15
16
# File 'app/pdfs/renalware/events/event_pdf.rb', line 14

def event
  @event
end

Instance Method Details

#buildObject



23
24
25
26
27
28
29
30
# File 'app/pdfs/renalware/events/event_pdf.rb', line 23

def build
  render_hospital_centre_info
  render_patient_identifiers
  render_common_event_fields
  render_specific_event_fields(event.document)
  render_notes
  self
end

#interpret_new_lines_in(text) ⇒ Object

Ensure any new line characters in a string are interpreted as actual new lines. This lets use /n in seed data and in the database.



100
101
102
# File 'app/pdfs/renalware/events/event_pdf.rb', line 100

def interpret_new_lines_in(text)
  text&.gsub('\n', "\n")
end

#output_html_as_text_but_preserving_paragraph_breaks(text:, strip_tags: true, **args) ⇒ Object

Create new line if there is a
. If there is an empty line (caused by
) move down to indicate a new paragraph.



106
107
108
109
110
111
112
113
114
115
116
117
# File 'app/pdfs/renalware/events/event_pdf.rb', line 106

def output_html_as_text_but_preserving_paragraph_breaks(text:, strip_tags: true, **args)
  return if text.blank?

  text.split("<br>").each do |paragraph|
    if strip_tags
      text ActionView::Base.full_sanitizer.sanitize(paragraph), **args
    else
      text paragraph, **args
    end
    move_down(10) if ActionView::Base.full_sanitizer.sanitize(paragraph).blank?
  end
end

#render_common_event_fieldsObject



55
56
57
58
59
60
61
62
63
64
# File 'app/pdfs/renalware/events/event_pdf.rb', line 55

def render_common_event_fields
  text "Date: #{I18n.l(event.date_time)}"
  move_down 10
  if event.respond_to?(:description)
    text "<u>Description</u>", inline_format: true
    move_down 5
    text event.description
    move_down 10
  end
end

#render_headerObject



32
33
34
35
36
37
38
# File 'app/pdfs/renalware/events/event_pdf.rb', line 32

def render_header
  font_size 12
  text title
  move_down 10
  text event.patient.to_s, style: :bold
  move_down 10
end

#render_hospital_centre_infoObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'app/pdfs/renalware/events/event_pdf.rb', line 80

def render_hospital_centre_info
  bounding_box([340, cursor], width: 200) do
    image_path = "app/assets/images/renalware/nhs_a4_letter_logo_black.png"
    image Renalware::Engine.root.join(image_path), height: 30, position: :right
    move_down 10
    text event.hospital_centre_trust_name, align: :right
    text event.hospital_centre_trust_caption, align: :right
    font_size 8
    output_html_as_text_but_preserving_paragraph_breaks(
      text: interpret_new_lines_in(event.hospital_centre_info),
      strip_tags: false,
      align: :right,
      inline_format: true
    )
    font_size 10
  end
end

#render_notesObject



74
75
76
77
78
# File 'app/pdfs/renalware/events/event_pdf.rb', line 74

def render_notes
  text "<u>Notes</u>", inline_format: true
  move_down 5
  output_html_as_text_but_preserving_paragraph_breaks(text: event.notes)
end

#render_patient_identifiersObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/pdfs/renalware/events/event_pdf.rb', line 40

def render_patient_identifiers
  move_cursor_to 675
  font_size 10
  bounding_box([0, cursor], width: 340) do
    render_header
    font_size 10
    text "DOB: #{I18n.l(patient.born_on)}"
    text "NHS: #{patient.nhs_number}"
    patient.hospital_identifiers.all.each do |key, value|
      text "#{key}: #{value}"
    end
    move_down 10
  end
end

#render_specific_event_fields(document) ⇒ Object



66
67
68
69
70
71
72
# File 'app/pdfs/renalware/events/event_pdf.rb', line 66

def render_specific_event_fields(document)
  document.attributes.each do |name, value|
    # could handle recursive documents here but none exists ATM even in HEROIC
    text "#{document.class.human_attribute_name(name)}:    #{value}"
  end
  move_down 10
end

#titleObject



119
120
121
# File 'app/pdfs/renalware/events/event_pdf.rb', line 119

def title
  event_type.title || event_type.name
end