Module: Renalware::Letters::Printing::PdfCombining
- Extended by:
- ActiveSupport::Concern
- Included in:
- CreatePdfByInterleavingAddressSheetAndLetterForEachRecipient::UsingOnePdfForAllLetterSections, CreatePdfByInterleavingAddressSheetAndLetterForEachRecipient::UsingSeparatePdfForEachLetterSection
- Defined in:
- app/models/renalware/letters/printing/pdf_combining.rb
Overview
Mixin for PDF combination
Instance Method Summary collapse
- #combine_multiple_pdfs_into_one(dir, output_filepath) ⇒ Object
-
#copy_tempfile_to_output_file(tmp_outfile, output_file) ⇒ Object
rubocop:enable Metrics/LineLength.
- #files ⇒ Object
- #in_a_temporary_folder ⇒ Object
- #rails_tmp_folder ⇒ Object
-
#shell_to_ghostscript_to_combine_files_into(outfile, dir) ⇒ Object
rubocop:disable Metrics/LineLength.
-
#using_a_temporary_output_file ⇒ Object
Create a tempfile outside the temp dir as dir will be destroyed when outside block closes.
Instance Method Details
#combine_multiple_pdfs_into_one(dir, output_filepath) ⇒ Object
14 15 16 17 18 19 |
# File 'app/models/renalware/letters/printing/pdf_combining.rb', line 14 def combine_multiple_pdfs_into_one(dir, output_filepath) using_a_temporary_output_file do |tmp_outfile| shell_to_ghostscript_to_combine_files_into(tmp_outfile, dir) copy_tempfile_to_output_file(tmp_outfile, output_filepath) end end |
#copy_tempfile_to_output_file(tmp_outfile, output_file) ⇒ Object
rubocop:enable Metrics/LineLength
31 32 33 34 |
# File 'app/models/renalware/letters/printing/pdf_combining.rb', line 31 def copy_tempfile_to_output_file(tmp_outfile, output_file) FileUtils.cp tmp_outfile.path, output_file output_file end |
#files ⇒ Object
36 37 38 |
# File 'app/models/renalware/letters/printing/pdf_combining.rb', line 36 def files @files ||= [] end |
#in_a_temporary_folder ⇒ Object
44 45 46 47 48 49 |
# File 'app/models/renalware/letters/printing/pdf_combining.rb', line 44 def in_a_temporary_folder Dir.mktmpdir(nil, rails_tmp_folder) do |dir| yield Pathname(dir) # temp dir removed here! end end |
#rails_tmp_folder ⇒ Object
40 41 42 |
# File 'app/models/renalware/letters/printing/pdf_combining.rb', line 40 def rails_tmp_folder Rails.root.join("tmp").to_s end |
#shell_to_ghostscript_to_combine_files_into(outfile, dir) ⇒ Object
rubocop:disable Metrics/LineLength
22 23 24 25 26 27 28 |
# File 'app/models/renalware/letters/printing/pdf_combining.rb', line 22 def shell_to_ghostscript_to_combine_files_into(outfile, dir) cmd = "gs -dNOPAUSE -sDEVICE=pdfwrite -sOUTPUTFILE=#{outfile.path} -dBATCH #{files.join(' ')}" err = Open3.popen3(cmd, chdir: dir.to_s) do |_stdin, _stdout, stderr| stderr.read end raise "Error combining PDFs: #{err}" unless err.empty? end |
#using_a_temporary_output_file ⇒ Object
Create a tempfile outside the temp dir as dir will be destroyed when outside block closes.
52 53 54 55 56 57 58 59 60 |
# File 'app/models/renalware/letters/printing/pdf_combining.rb', line 52 def using_a_temporary_output_file file = Tempfile.new("pdf_combined", rails_tmp_folder) begin yield file ensure file.close file.unlink # deletes the temp file end end |