Sha256: abe28561e9538202f2b6df07b014c7bb9164f829c7ff6579c0697f1ca949f42c

Contents?: true

Size: 1.88 KB

Versions: 17

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 Metrics/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 Metrics/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

17 entries across 17 versions & 1 rubygems

Version Path
renalware-core-2.0.130 app/models/concerns/renalware/pdf_compilation.rb
renalware-core-2.0.129 app/models/concerns/renalware/pdf_compilation.rb
renalware-core-2.0.128 app/models/concerns/renalware/pdf_compilation.rb
renalware-core-2.0.127 app/models/concerns/renalware/pdf_compilation.rb
renalware-core-2.0.126 app/models/concerns/renalware/pdf_compilation.rb
renalware-core-2.0.125 app/models/concerns/renalware/pdf_compilation.rb
renalware-core-2.0.124 app/models/concerns/renalware/pdf_compilation.rb
renalware-core-2.0.123 app/models/concerns/renalware/pdf_compilation.rb
renalware-core-2.0.121 app/models/concerns/renalware/pdf_compilation.rb
renalware-core-2.0.120 app/models/concerns/renalware/pdf_compilation.rb
renalware-core-2.0.119 app/models/concerns/renalware/pdf_compilation.rb
renalware-core-2.0.118 app/models/concerns/renalware/pdf_compilation.rb
renalware-core-2.0.117 app/models/concerns/renalware/pdf_compilation.rb
renalware-core-2.0.116 app/models/concerns/renalware/pdf_compilation.rb
renalware-core-2.0.115 app/models/concerns/renalware/pdf_compilation.rb
renalware-core-2.0.113 app/models/concerns/renalware/pdf_compilation.rb
renalware-core-2.0.112 app/models/concerns/renalware/pdf_compilation.rb