Class: PdfFill::ExtrasGenerator
- Inherits:
-
Object
- Object
- PdfFill::ExtrasGenerator
- Defined in:
- lib/pdf_fill/extras_generator.rb
Instance Method Summary collapse
- #add_text(value, metadata) ⇒ Object
- #create_block(value, metadata) ⇒ Object
- #generate ⇒ Object
-
#initialize ⇒ ExtrasGenerator
constructor
A new instance of ExtrasGenerator.
- #set_font(pdf) ⇒ Object
- #sort_generate_blocks ⇒ Object
- #text? ⇒ Boolean
Constructor Details
#initialize ⇒ ExtrasGenerator
Returns a new instance of ExtrasGenerator.
5 6 7 |
# File 'lib/pdf_fill/extras_generator.rb', line 5 def initialize @generate_blocks = [] end |
Instance Method Details
#add_text(value, metadata) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/pdf_fill/extras_generator.rb', line 23 def add_text(value, ) unless text? @generate_blocks << { metadata: {}, block: lambda do |pdf| pdf.text('Additional Information', size: 16, style: :bold) end } end @generate_blocks << { metadata:, block: create_block(value, ) } end |
#create_block(value, metadata) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/pdf_fill/extras_generator.rb', line 9 def create_block(value, ) lambda do |pdf| pdf.move_down(10) prefix = [:question_num].to_s prefix += [:question_suffix] if [:question_suffix].present? prefix = "#{prefix}. #{[:question_text].humanize}" i = [:i] prefix += " Line #{i + 1}" if i.present? pdf.text("#{prefix}:", style: :bold) pdf.text(value.to_s, style: :normal) end end |
#generate ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/pdf_fill/extras_generator.rb', line 65 def generate folder = 'tmp/pdfs' FileUtils.mkdir_p(folder) file_path = "#{folder}/extras_#{SecureRandom.uuid}.pdf" generate_blocks = sort_generate_blocks Prawn::Document.generate(file_path) do |pdf| set_font(pdf) box_height = 25 pdf.bounding_box( [pdf.bounds.left, pdf.bounds.top - box_height], width: pdf.bounds.width, height: pdf.bounds.height - box_height ) do generate_blocks.each do |block| block[:block].call(pdf) end end end file_path end |
#set_font(pdf) ⇒ Object
55 56 57 58 59 60 61 62 63 |
# File 'lib/pdf_fill/extras_generator.rb', line 55 def set_font(pdf) pdf.font_families.update( 'Roboto' => { normal: Rails.root.join('lib', 'pdf_fill', 'fonts', 'Roboto-Regular.ttf'), bold: Rails.root.join('lib', 'pdf_fill', 'fonts', 'Roboto-Bold.ttf') } ) pdf.font('Roboto') end |
#sort_generate_blocks ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/pdf_fill/extras_generator.rb', line 43 def sort_generate_blocks @generate_blocks.sort_by do |generate_block| = generate_block[:metadata] [ [:question_num] || -1, [:i] || 99_999, [:question_suffix] || '', [:question_text] || '' ] end end |
#text? ⇒ Boolean
39 40 41 |
# File 'lib/pdf_fill/extras_generator.rb', line 39 def text? @generate_blocks.size.positive? end |