Sha256: a328c116872f123607d15b5861a3e5e694e3abaa2f20d6046c33a36d7e5c58f7

Contents?: true

Size: 1.82 KB

Versions: 58

Compression:

Stored size: 1.82 KB

Contents

# frozen_string_literal: true

require_dependency "renalware/letters"

require "attr_extras"

module Renalware
  module Letters
    module Printing
      # Mixin for PDF combination
      module PdfCombining
        extend ActiveSupport::Concern

        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

        # rubocop:disable Metrics/LineLength
        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
        # rubocop:enable Metrics/LineLength

        def copy_tempfile_to_output_file(tmp_outfile, output_file)
          FileUtils.cp tmp_outfile.path, output_file
          output_file
        end

        def files
          @files ||= []
        end

        def rails_tmp_folder
          Rails.root.join("tmp").to_s
        end

        def in_a_temporary_folder
          Dir.mktmpdir(nil, rails_tmp_folder) do |dir|
            yield Pathname(dir)
            # temp dir removed here!
          end
        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
  end
end

Version data entries

58 entries across 58 versions & 1 rubygems

Version Path
renalware-core-2.0.88 app/models/renalware/letters/printing/pdf_combining.rb
renalware-core-2.0.87 app/models/renalware/letters/printing/pdf_combining.rb
renalware-core-2.0.86 app/models/renalware/letters/printing/pdf_combining.rb
renalware-core-2.0.85 app/models/renalware/letters/printing/pdf_combining.rb
renalware-core-2.0.84 app/models/renalware/letters/printing/pdf_combining.rb
renalware-core-2.0.83 app/models/renalware/letters/printing/pdf_combining.rb
renalware-core-2.0.82 app/models/renalware/letters/printing/pdf_combining.rb
renalware-core-2.0.81 app/models/renalware/letters/printing/pdf_combining.rb
renalware-core-2.0.80 app/models/renalware/letters/printing/pdf_combining.rb
renalware-core-2.0.79 app/models/renalware/letters/printing/pdf_combining.rb
renalware-core-2.0.78 app/models/renalware/letters/printing/pdf_combining.rb
renalware-core-2.0.77 app/models/renalware/letters/printing/pdf_combining.rb
renalware-core-2.0.76 app/models/renalware/letters/printing/pdf_combining.rb
renalware-core-2.0.75 app/models/renalware/letters/printing/pdf_combining.rb
renalware-core-2.0.74 app/models/renalware/letters/printing/pdf_combining.rb
renalware-core-2.0.73 app/models/renalware/letters/printing/pdf_combining.rb
renalware-core-2.0.72 app/models/renalware/letters/printing/pdf_combining.rb
renalware-core-2.0.71 app/models/renalware/letters/printing/pdf_combining.rb
renalware-core-2.0.70 app/models/renalware/letters/printing/pdf_combining.rb
renalware-core-2.0.69 app/models/renalware/letters/printing/pdf_combining.rb