Module: HtmldocRails::Controller
- Defined in:
- lib/htmldoc_rails/controller.rb
Instance Method Summary collapse
-
#render_pdf(options = {}) ⇒ Object
Runs the given view through HTMLDoc and sends the generated PDF data back to the browser.
-
#render_pdf_to_file(filename, render_options = {}, htmldoc_options = {}) ⇒ Object
Runs the given view through HTMLDoc and writes the generated PDF data to the file of your choice.
Instance Method Details
#render_pdf(options = {}) ⇒ Object
Runs the given view through HTMLDoc and sends the generated PDF data back to the browser.
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 |
# File 'lib/htmldoc_rails/controller.rb', line 25 def render_pdf(={}) filename = .delete(:filename) disposition = (.delete(:as) || params[:as] || :inline).to_sym = .delete(:htmldoc) || {} = .merge(.delete(:url) || {}) if !.include?(:layout) [:layout] = false end = { :type => content_type } [:filename] = filename if filename [:disposition] = (disposition == :attachment) ? 'attachment' : 'inline' # Make sure that the rendered PDF isn't cached if request.env['HTTP_USER_AGENT'] =~ /msie/i headers['Pragma'] = '' headers['Cache-Control'] = '' else headers['Pragma'] = 'no-cache' headers['Cache-Control'] = 'no-cache, must-revalidate' end # Run view through PDF::HTMLDoc::View html_content = render_to_string() pdf_data = run_through_htmldoc(html_content, ) unless pdf_data.blank? send_data(pdf_data, ) else render :text => "HTMLDoc had trouble parsing the HTML to create the PDF." end end |
#render_pdf_to_file(filename, render_options = {}, htmldoc_options = {}) ⇒ Object
Runs the given view through HTMLDoc and writes the generated PDF data to the file of your choice.
71 72 73 74 75 76 77 78 79 |
# File 'lib/htmldoc_rails/controller.rb', line 71 def render_pdf_to_file(filename, ={}, ={}) if !.include?(:layout) [:layout] = false end headers["Content-Disposition"] = "inline" html_content = render_to_string() pdf_data = run_through_htmldoc(html_content, ) File.open(filename, 'w') {|f| f.write(pdf_data) } end |