Class: Renalware::Letters::Printing::CreatePdfByInterleavingAddressSheetAndLetterForEachRecipient::UsingOnePdfForAllLetterSections

Inherits:
Object
  • Object
show all
Includes:
PdfCombining
Defined in:
app/models/renalware/letters/printing/create_pdf_by_interleaving_address_sheet_and_letter_for_each_recipient.rb

Overview

For each letter, uses whkhtmltopdf to build the entire print content, ie all address sheets and required instance of the letter are created as one fresh pdf (no cache will be used).

Pros:

  • Quicker: For each letter only one PDF is generated, and although the generation is slow as it does not use the previously cached letter PDF, and it has to render all the letter sections, including the letter itself, for every recipient, its stil faster then rendering separate PDFs fo each section.

Cons:

- wastes some resources because it does not use the cache and renders same letter
  multiple times in the views
- cannot be 100% sure the number of pages in the final PDF will be extactly what we
  predict, so requires a check to load the pdf into a reader and validate the page_count

Instance Method Summary collapse

Methods included from PdfCombining

#combine_multiple_pdfs_into_one, #copy_tempfile_to_output_file, #files, #in_a_temporary_folder, #rails_tmp_folder, #shell_to_ghostscript_to_combine_files_into, #using_a_temporary_output_file

Instance Method Details

#callObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'app/models/renalware/letters/printing/create_pdf_by_interleaving_address_sheet_and_letter_for_each_recipient.rb', line 61

def call
  in_a_temporary_folder do |dir|
    Array(letters).each do |letter|
      if letter.page_count.to_i < 1
        raise(ArgumentError, "letter.page_count not set on letter.id=#{letter.id}")
      end
      letter_filename = create_letter_pdf_in(dir, letter)
      files << letter_filename
    end
    combine_multiple_pdfs_into_one(dir, output_file)
    # For debuging:
    # FileUtils.cp output_file, "/Users/tim/Desktop/x.pdf"
    # `open /Users/tim/Desktop/x.pdf`
  end
end