Module: Renalware::PdfCompilation
- Extended by:
- ActiveSupport::Concern
- Included in:
- HD::SessionForms::BatchCompilePdfs
- Defined in:
- app/models/concerns/renalware/pdf_compilation.rb
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 Layout/LineLength.
- #rails_tmp_folder ⇒ Object
-
#shell_to_ghostscript_to_combine_files(filenames, dir, outputfile) ⇒ Object
rubocop:disable Layout/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
9 10 11 12 13 14 |
# File 'app/models/concerns/renalware/pdf_compilation.rb', line 9 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
16 17 18 19 20 21 22 23 |
# File 'app/models/concerns/renalware/pdf_compilation.rb', line 16 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 Layout/LineLength
40 41 42 43 |
# File 'app/models/concerns/renalware/pdf_compilation.rb', line 40 def move_tempfile_to_output_file(tmp_outfile, output_file) FileUtils.mv tmp_outfile.path, output_file output_file end |
#rails_tmp_folder ⇒ Object
45 46 47 |
# File 'app/models/concerns/renalware/pdf_compilation.rb', line 45 def rails_tmp_folder Rails.root.join("tmp").to_s end |
#shell_to_ghostscript_to_combine_files(filenames, dir, outputfile) ⇒ Object
rubocop:disable Layout/LineLength
26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'app/models/concerns/renalware/pdf_compilation.rb', line 26 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.
50 51 52 53 54 55 56 57 58 |
# File 'app/models/concerns/renalware/pdf_compilation.rb', line 50 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 |