Sha256: d9227a0acca3cc00638d3c10aaad415a8501d35cea2099bb10d8fd153a9ebeba

Contents?: true

Size: 1.46 KB

Versions: 28

Compression:

Stored size: 1.46 KB

Contents

require_dependency "renalware/letters"

module Renalware
  module Letters
    class RTFRenderer
      include ActionController::Rendering
      REGEX_TO_STRIP_IMAGES = %r{(?m)<img\s*.*?"\s*\/>}

      def initialize(letter, controller)
        @letter = letter
        @controller = controller
      end

      def render
        using_temp_html_file do |temp_file|
          controller.send_data rtf_content_converted_from(temp_file),
                               type: "text/richtext",
                               filename: filename
        end
      end

      private

      attr_accessor :letter, :controller

      def using_temp_html_file
        temp_html_file = Tempfile.new("html_to_be_converted_to_rtf")
        temp_html_file << html_with_images_stripped
        temp_html_file.close

        yield(temp_html_file) if block_given?
      ensure
        temp_html_file.unlink # allows garbage collection and temp file removal
      end

      def filename
        "#{letter.pdf_filename}.rtf"
      end

      def disposition
        "attachment; filename=\"#{filename}\""
      end

      def rtf_content_converted_from(html_temp_file)
        rtf_template = File.join(Engine.root, "lib", "pandoc", "templates", "default.rtf")
        options = { template: rtf_template }
        PandocRuby.html([html_temp_file.path], options, :standalone).to_rtf
      end

      def html_with_images_stripped
        letter.to_html.gsub(REGEX_TO_STRIP_IMAGES, "")
      end
    end
  end
end

Version data entries

28 entries across 28 versions & 1 rubygems

Version Path
renalware-core-2.0.8 app/models/renalware/letters/rtf_renderer.rb
renalware-core-2.0.7 app/models/renalware/letters/rtf_renderer.rb
renalware-core-2.0.5 app/models/renalware/letters/rtf_renderer.rb
renalware-core-2.0.4 app/models/renalware/letters/rtf_renderer.rb
renalware-core-2.0.3 app/models/renalware/letters/rtf_renderer.rb
renalware-core-2.0.2 app/models/renalware/letters/rtf_renderer.rb
renalware-core-2.0.1 app/models/renalware/letters/rtf_renderer.rb
renalware-core-2.0.0 app/models/renalware/letters/rtf_renderer.rb
renalware-core-2.0.0.pre.rc13 app/models/renalware/letters/rtf_renderer.rb
renalware-core-2.0.0.pre.rc11 app/models/renalware/letters/rtf_renderer.rb
renalware-core-2.0.0.pre.rc10 app/models/renalware/letters/rtf_renderer.rb
renalware-core-2.0.0.pre.rc9 app/models/renalware/letters/rtf_renderer.rb
renalware-core-2.0.0.pre.rc8 app/models/renalware/letters/rtf_renderer.rb
renalware-core-2.0.0.pre.rc7 app/models/renalware/letters/rtf_renderer.rb
renalware-core-2.0.0.pre.rc6 app/models/renalware/letters/rtf_renderer.rb
renalware-core-2.0.0.pre.rc5 app/models/renalware/letters/rtf_renderer.rb
renalware-core-2.0.0.pre.rc4 app/models/renalware/letters/rtf_renderer.rb
renalware-core-2.0.0.pre.rc3 app/models/renalware/letters/rtf_renderer.rb
renalware-core-2.0.0.pre.rc1 app/models/renalware/letters/rtf_renderer.rb
renalware-core-2.0.0.pre.beta12 app/models/renalware/letters/rtf_renderer.rb