Sha256: b3564d5cf43c6f09fa966f04540e62756bd1d1606f86fb3c10323778f1171274

Contents?: true

Size: 1.06 KB

Versions: 1

Compression:

Stored size: 1.06 KB

Contents

# frozen_string_literal: true

require 'html2pdf/rails/client'

module Html2Pdf
  module Rails
    module Rendering
      def render_to_pdf(options)
        make_and_send_pdf(options.delete(:pdf), options)
      end

      private

      def make_and_send_pdf(pdf_name, options = {})
        options[:layout] ||= false
        options[:template] ||= File.join(controller_path, action_name)
        options[:disposition] ||= 'inline'
        options[:pdf_options] ||= {}

        if options[:show_as_html]
          render_opts = options.slice(:template, :layout, :formats, :handlers)
          render(render_opts.merge({ content_type: 'text/html' }))
        else
          pdf_content = make_pdf(options)
          send_data(pdf_content, filename: pdf_name + '.pdf', type: 'application/pdf', disposition: options[:disposition])
        end
      end

      def make_pdf(options = {})
        render_opts = options.slice(:template, :layout, :formats, :handlers)
        html = render_to_string(render_opts)
        Client.post(html, options[:pdf_options]).body
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
html2pdf-rails-0.1.0 lib/html2pdf/rails/rendering.rb