Module: ActsAsFlyingSaucer::Controller::InstanceMethods
- Defined in:
- lib/acts_as_flying_saucer/acts_as_flying_saucer_controller.rb
Overview
InstanceMethods
Instance Method Summary collapse
-
#render_pdf(options = {}) ⇒ Object
render_pdf.
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( = {}) tidy_clean = [:clean] || false self.pdf_mode = :create if [:url] tidy_clean = true if [:url].match(/\Ahttp/) html = Net::HTTP.get_response(URI.parse([:url])).body rescue [:url] elsif File.exist?([:url]) html = File.read([:url]) rescue "" else html = [: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 if [: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 = [:template] if [:debug_html] response.header["Content-Type"] = "text/html; charset=utf-8" response.body << html and return end end # saving the file tmp_dir = ::Config.[: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.("#{tmp_dir}"),"#{html_digest}.html") #logger.debug("html file: #{input_file}") output_file = (.has_key?(:pdf_file)) ? [:pdf_file] : File.join(File.("#{tmp_dir}"),"#{html_digest}.pdf") password = (.has_key?(:password)) ? [:password] : "" = ::Config..merge({ :input_file => input_file, :output_file => output_file, :html => html }) ::Xhtml2Pdf.write_pdf() if password != "" op=output_file.split(".") op.pop op << "a" op=op.to_s+".pdf" output_file_name = op ::Xhtml2Pdf.encrypt_pdf(,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 [:send_to_client] == false output_file else if [:send_file] = { :filename => File.basename(output_file) #:x_sendfile => true, } .merge!([:send_file]) if .respond_to?(:merge) send_file(output_file, ) end end end |