Sha256: 881b5960a8727777268763464fd18cdeb25958515c9b80be75ce3c2131d98c86

Contents?: true

Size: 1.29 KB

Versions: 47

Compression:

Stored size: 1.29 KB

Contents

# frozen_string_literal: true

require_dependency "renalware/letters"
require "attr_extras"

module Renalware
  module Letters
    # Use pandoc to convert the html letter to RTF
    class RTFRenderer
      pattr_initialize :letter
      REGEX_TO_STRIP_IMAGES = %r{(?m)<img\s*.*?"\s*\/>}.freeze

      def render
        using_temp_html_file do |temp_file|
          rtf_content_converted_from(temp_file)
        end
      end

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

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

      private

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

47 entries across 47 versions & 1 rubygems

Version Path
renalware-core-2.0.159 app/models/renalware/letters/rtf_renderer.rb
renalware-core-2.0.158 app/models/renalware/letters/rtf_renderer.rb
renalware-core-2.0.157 app/models/renalware/letters/rtf_renderer.rb
renalware-core-2.0.156 app/models/renalware/letters/rtf_renderer.rb
renalware-core-2.0.155 app/models/renalware/letters/rtf_renderer.rb
renalware-core-2.0.153 app/models/renalware/letters/rtf_renderer.rb
renalware-core-2.0.152 app/models/renalware/letters/rtf_renderer.rb
renalware-core-2.0.151 app/models/renalware/letters/rtf_renderer.rb
renalware-core-2.0.149 app/models/renalware/letters/rtf_renderer.rb
renalware-core-2.0.148 app/models/renalware/letters/rtf_renderer.rb
renalware-core-2.0.147 app/models/renalware/letters/rtf_renderer.rb
renalware-core-2.0.146 app/models/renalware/letters/rtf_renderer.rb
renalware-core-2.0.145 app/models/renalware/letters/rtf_renderer.rb
renalware-core-2.0.144 app/models/renalware/letters/rtf_renderer.rb
renalware-core-2.0.143 app/models/renalware/letters/rtf_renderer.rb
renalware-core-2.0.142 app/models/renalware/letters/rtf_renderer.rb
renalware-core-2.0.141 app/models/renalware/letters/rtf_renderer.rb
renalware-core-2.0.140 app/models/renalware/letters/rtf_renderer.rb
renalware-core-2.0.139 app/models/renalware/letters/rtf_renderer.rb
renalware-core-2.0.138 app/models/renalware/letters/rtf_renderer.rb