Module: Renalware::Letters::Printing::PdfCombining
- Extended by:
- ActiveSupport::Concern
- Included in:
- BatchCompilePdfs
- Defined in:
- app/models/renalware/letters/printing/pdf_combining.rb
Overview
Mixin for PDF combination
Instance Method Summary collapse
- #combine_multiple_pdfs_into_file(filepath:, glob:) ⇒ Object
- #combine_multiple_pdfs_using_filenames(filenames, dir, output_filepath) ⇒ Object
-
#move_tempfile_to_output_file(tmp_outfile, output_file) ⇒ Object
rubocop:enable Metrics/LineLength.
- #rails_tmp_folder ⇒ Object
-
#shell_to_ghostscript_to_combine_files(filenames, dir, outputfile) ⇒ 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_file(filepath:, glob:) ⇒ Object
14 15 16 17 18 19 |
# File 'app/models/renalware/letters/printing/pdf_combining.rb', line 14 def combine_multiple_pdfs_into_file(filepath:, glob:) filepath = Pathname(filepath) Rails.logger.info " Compiling PDFs #{glob.join(',')} into #{filepath}" shell_to_ghostscript_to_combine_files(glob, dir, filepath) filepath end |
#combine_multiple_pdfs_using_filenames(filenames, dir, output_filepath) ⇒ Object
21 22 23 24 25 26 27 28 |
# File 'app/models/renalware/letters/printing/pdf_combining.rb', line 21 def combine_multiple_pdfs_using_filenames(filenames, dir, output_filepath) filenames = Array(filenames) Rails.logger.info " Compiling PDFs #{filenames.join(',')} into #{output_filepath}" using_a_temporary_output_file do |tmp_outfile| shell_to_ghostscript_to_combine_files(filenames, dir, tmp_outfile) move_tempfile_to_output_file(tmp_outfile, output_filepath) end end |
#move_tempfile_to_output_file(tmp_outfile, output_file) ⇒ Object
rubocop:enable Metrics/LineLength
45 46 47 48 |
# File 'app/models/renalware/letters/printing/pdf_combining.rb', line 45 def move_tempfile_to_output_file(tmp_outfile, output_file) FileUtils.mv tmp_outfile.path, output_file output_file end |
#rails_tmp_folder ⇒ Object
50 51 52 |
# File 'app/models/renalware/letters/printing/pdf_combining.rb', line 50 def rails_tmp_folder Rails.root.join("tmp").to_s end |
#shell_to_ghostscript_to_combine_files(filenames, dir, outputfile) ⇒ Object
rubocop:disable Metrics/LineLength
31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'app/models/renalware/letters/printing/pdf_combining.rb', line 31 def shell_to_ghostscript_to_combine_files(filenames, dir, outputfile) outputfile = Pathname(outputfile) cmd = "gs -dNOPAUSE -sDEVICE=pdfwrite -sOUTPUTFILE=#{outputfile} -dBATCH #{filenames.join(' ')}" err = msg = nil Open3.popen3(cmd, chdir: dir.to_s) do |_stdin, stdout, stderr| err = stderr.read msg = stdout.read end if err.present? raise "Error combining PDFs: #{[err, msg].join(' ')} command: #{cmd}" end end |
#using_a_temporary_output_file ⇒ Object
Create a tempfile outside the temp dir as dir will be destroyed when outside block closes.
55 56 57 58 59 60 61 62 63 |
# File 'app/models/renalware/letters/printing/pdf_combining.rb', line 55 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 |