Sha256: 051cdd0c53e58b5229ab09605a28fe546623d34c93e120a7e7468a48ef31ce87

Contents?: true

Size: 1.5 KB

Versions: 24

Compression:

Stored size: 1.5 KB

Contents

# frozen_string_literal: true

require_dependency "renalware/letters"

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

      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

24 entries across 24 versions & 1 rubygems

Version Path
renalware-core-2.0.108 app/models/renalware/letters/rtf_renderer.rb
renalware-core-2.0.106 app/models/renalware/letters/rtf_renderer.rb
renalware-core-2.0.105 app/models/renalware/letters/rtf_renderer.rb
renalware-core-2.0.104 app/models/renalware/letters/rtf_renderer.rb
renalware-core-2.0.103 app/models/renalware/letters/rtf_renderer.rb
renalware-core-2.0.102 app/models/renalware/letters/rtf_renderer.rb
renalware-core-2.0.101 app/models/renalware/letters/rtf_renderer.rb
renalware-core-2.0.100 app/models/renalware/letters/rtf_renderer.rb
renalware-core-2.0.99 app/models/renalware/letters/rtf_renderer.rb
renalware-core-2.0.98 app/models/renalware/letters/rtf_renderer.rb
renalware-core-2.0.97 app/models/renalware/letters/rtf_renderer.rb
renalware-core-2.0.96 app/models/renalware/letters/rtf_renderer.rb
renalware-core-2.0.95 app/models/renalware/letters/rtf_renderer.rb
renalware-core-2.0.94 app/models/renalware/letters/rtf_renderer.rb
renalware-core-2.0.93 app/models/renalware/letters/rtf_renderer.rb
renalware-core-2.0.92 app/models/renalware/letters/rtf_renderer.rb
renalware-core-2.0.91 app/models/renalware/letters/rtf_renderer.rb
renalware-core-2.0.90 app/models/renalware/letters/rtf_renderer.rb
renalware-core-2.0.89 app/models/renalware/letters/rtf_renderer.rb
renalware-core-2.0.88 app/models/renalware/letters/rtf_renderer.rb