Sha256: 06173902cf4848b00861a00d2e9c4300b42fd3bd92f723f50a373b67492482a3

Contents?: true

Size: 1.41 KB

Versions: 10

Compression:

Stored size: 1.41 KB

Contents

# frozen_string_literal: true

require_dependency "renalware/letters"

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

      # Use windows line endings (CRLF) rather than linux (LF).
      # This solves a problem at Barts exporting RTFs to send to GPs
      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("--eol=crlf")
      end

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

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
renalware-core-2.1.1 app/models/renalware/letters/rtf_renderer.rb
renalware-core-2.1.0 app/models/renalware/letters/rtf_renderer.rb
renalware-core-2.0.167 app/models/renalware/letters/rtf_renderer.rb
renalware-core-2.0.166 app/models/renalware/letters/rtf_renderer.rb
renalware-core-2.0.165 app/models/renalware/letters/rtf_renderer.rb
renalware-core-2.0.164 app/models/renalware/letters/rtf_renderer.rb
renalware-core-2.0.163 app/models/renalware/letters/rtf_renderer.rb
renalware-core-2.0.162 app/models/renalware/letters/rtf_renderer.rb
renalware-core-2.0.161 app/models/renalware/letters/rtf_renderer.rb
renalware-core-2.0.160 app/models/renalware/letters/rtf_renderer.rb