Module: ActsAsFlyingSaucer::Controller::InstanceMethods

Defined in:
lib/acts_as_flying_saucer/acts_as_flying_saucer_controller.rb

Overview

InstanceMethods

Instance Method Summary collapse

Instance Method Details

#render_pdf(options = {}) ⇒ Object

render_pdf



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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/acts_as_flying_saucer/acts_as_flying_saucer_controller.rb', line 31

def render_pdf(options = {})
    tidy_clean = options[:clean] || false
  self.pdf_mode = :create
     if options[:url]
       tidy_clean = true
       if options[:url].match(/\Ahttp/)
         html = Net::HTTP.get_response(URI.parse(options[:url])).body rescue options[:url]
       elsif File.exist?(options[:url])
         html =  File.read(options[:url]) rescue  ""
       else
         html = options[:url]
       end
     elsif defined?(Rails)
       host = ActionController::Base.asset_host
      ActionController::Base.asset_host = request.protocol + request.host_with_port if host.blank?
    html = render_to_string options
    if options[:debug_html]
      #    ActionController::Base.asset_host = host
      response.header["Content-Type"] = "text/html; charset=utf-8"
      render :text => html and return
    end
    #sinatra
  elsif defined?(Sinatra)
    html = options[:template]
    if options[:debug_html]
      response.header["Content-Type"] = "text/html; charset=utf-8"
      response.body << html and return
    end
  end
  # saving the file
  tmp_dir = ActsAsFlyingSaucer::Config.options[:tmp_path]
     html = TidyFFI::Tidy.new(html,:output_xhtml=>true,:numeric_entities=>true).clean if tidy_clean
  html_digest = Digest::MD5.hexdigest(html)
  input_file =File.join(File.expand_path("#{tmp_dir}"),"#{html_digest}.html")

  #logger.debug("html file: #{input_file}")

  output_file = (options.has_key?(:pdf_file)) ? options[:pdf_file] : File.join(File.expand_path("#{tmp_dir}"),"#{html_digest}.pdf")
  password = (options.has_key?(:password)) ? options[:password] : ""


  generate_options = ActsAsFlyingSaucer::Config.options.merge({
                                                                      :input_file => input_file,
                                                                      :output_file => output_file,
                                                                      :html => html
                                                              })

  ActsAsFlyingSaucer::Xhtml2Pdf.write_pdf(generate_options)
  if  password != ""
    op=output_file.split(".")
    op.pop
    op  << "a"
    op=op.to_s+".pdf"
    output_file_name =  op
    ActsAsFlyingSaucer::Xhtml2Pdf.encrypt_pdf(generate_options,output_file_name,password)
    output_file = op
  end
  # restoring the host
  if defined?(Rails)
   ActionController::Base.asset_host = host
  end

  # sending the file to the client
     if options[:send_to_client] == false
       output_file
     else
    if options[:send_file]

      send_file_options = {
              :filename => File.basename(output_file)
              #:x_sendfile => true,
      }
      send_file_options.merge!(options[:send_file]) if options.respond_to?(:merge)
      send_file(output_file, send_file_options)
    end
     end
end