Sha256: e928146ac872ffd269631202b2a1896321edf901e3b8fb4bea5ab4a70c608ca8

Contents?: true

Size: 1.49 KB

Versions: 68

Compression:

Stored size: 1.49 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*\/>}

      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

68 entries across 68 versions & 1 rubygems

Version Path
renalware-core-2.0.83 app/models/renalware/letters/rtf_renderer.rb
renalware-core-2.0.82 app/models/renalware/letters/rtf_renderer.rb
renalware-core-2.0.81 app/models/renalware/letters/rtf_renderer.rb
renalware-core-2.0.80 app/models/renalware/letters/rtf_renderer.rb
renalware-core-2.0.79 app/models/renalware/letters/rtf_renderer.rb
renalware-core-2.0.78 app/models/renalware/letters/rtf_renderer.rb
renalware-core-2.0.77 app/models/renalware/letters/rtf_renderer.rb
renalware-core-2.0.76 app/models/renalware/letters/rtf_renderer.rb
renalware-core-2.0.75 app/models/renalware/letters/rtf_renderer.rb
renalware-core-2.0.74 app/models/renalware/letters/rtf_renderer.rb
renalware-core-2.0.73 app/models/renalware/letters/rtf_renderer.rb
renalware-core-2.0.72 app/models/renalware/letters/rtf_renderer.rb
renalware-core-2.0.71 app/models/renalware/letters/rtf_renderer.rb
renalware-core-2.0.70 app/models/renalware/letters/rtf_renderer.rb
renalware-core-2.0.69 app/models/renalware/letters/rtf_renderer.rb
renalware-core-2.0.68 app/models/renalware/letters/rtf_renderer.rb
renalware-core-2.0.67 app/models/renalware/letters/rtf_renderer.rb
renalware-core-2.0.64 app/models/renalware/letters/rtf_renderer.rb
renalware-core-2.0.63 app/models/renalware/letters/rtf_renderer.rb
renalware-core-2.0.62 app/models/renalware/letters/rtf_renderer.rb