Module: RTeX::ControllerMethods

Defined in:
lib/rtex/template_handler.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

[View source]

11
12
13
# File 'lib/rtex/template_handler.rb', line 11

def self.included(base)
  base.alias_method_chain :render, :rtex 
end

Instance Method Details

#render_with_rtex(options = nil, *args, &block) ⇒ Object

[View source]

15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/rtex/template_handler.rb', line 15

def render_with_rtex(options = nil, *args, &block)
  # pp args
  orig = render_without_rtex(options, *args, &block)
  # CF HACK
  
  File.open(Rails.root.join("tmp","last_document.latex"), "w"){|f| f.puts orig.to_s.force_encoding('UTF-8')} if Rails.env.development?
  
  # old:
  # File.open(Rails.root.join("tmp","last_document.latex"), "w"){|f| f.puts orig} if Rails.env.development?

  # END CF HACK
  if Thread.current[:_rendering_rtex] == true
    Thread.current[:_rendering_rtex] = false
    options = {} if options.class != Hash        
    Document.new(orig, options.merge(:processed => true)).to_pdf do |f|
      serve_file = Tempfile.new('rtex-pdf')

      if options[:user_pw].present? && options[:owner_pw].present?
        pdf_path = File.dirname(f)
        pdf_name = File.basename(f, '.pdf')
        encrypted_filename = File.join(pdf_path, "#{pdf_name}_encrypted.pdf")
        owner_pw = options[:owner_pw].is_a?(Proc)? options[:owner_pw].call : options[:owner_pw]
        user_pw = options[:user_pw].is_a?(Proc)? options[:user_pw].call : options[:user_pw]

        pdftk_commands = [
          f,
          'output', encrypted_filename,
          'owner_pw', owner_pw,
          'user_pw', user_pw
        ]

        unless options[:disallow_printing]
          pdftk_commands << 'allow'
          pdftk_commands << 'printing'
        end

        PDF::Toolkit.pdftk(*pdftk_commands)
        f = encrypted_filename
      end

      FileUtils.mv f, serve_file.path
      send_file serve_file.path,
        :disposition => (options[:disposition] rescue nil) || 'inline',
        :url_based_filename => true,
        :filename => (options[:filename] rescue nil),
        :type => "application/pdf",
        :length => File.size(serve_file.path),
        :stream => false
      serve_file.close
    end   
  end
  orig
end