Sha256: e56a06117a1d7c687ecf0ee211cbc5c4e8fd75215a34995c37228658cf5ff8d4
Contents?: true
Size: 1.88 KB
Versions: 37
Compression:
Stored size: 1.88 KB
Contents
# frozen_string_literal: true require "active_support/concern" module Renalware module PdfCompilation extend ActiveSupport::Concern 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 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 # rubocop:disable Layout/LineLength 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 # rubocop:enable Layout/LineLength def move_tempfile_to_output_file(tmp_outfile, output_file) FileUtils.mv tmp_outfile.path, output_file output_file end def rails_tmp_folder Rails.root.join("tmp").to_s end # Create a tempfile outside the temp dir as dir will be destroyed when outside block closes. 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 end end
Version data entries
37 entries across 37 versions & 1 rubygems