Module: HtmldocRails::Controller

Defined in:
lib/htmldoc_rails/controller.rb

Instance Method Summary

Instance Method Details

- (Object) render_pdf(options = {})

Runs the given view through HTMLDoc and sends the generated PDF data back to the browser.

Examples:

Rendering default view of action as PDF

  render_pdf

Rendering a specific view as PDF

  render_pdf :action => 'bar'

Set a top-margin of 50px in the PDF and force a download box when the page loads

  render_pdf :action => "bar", :as => :attachment, :htmldoc => { :top => 50 }

Parameters:

  • (Hash) options (defaults to: {}) — Various options

Options Hash (options):

  • (Hash, String) :url — default: nil — The hash/url that represents which view you want to render. Passed straight to render().
  • (Symbol) :as — default: :inline — The disposition; :inline renders the PDF in the browser, :attachment pops up a download box when the page loads. You can also set the disposition at runtime by appending ?as=attachment|inline to the URL.
  • (String) :filename — default: nil — The default filename for the file being downloaded, assuming :as => :attachment.
  • (Hash) :htmldoc — default: {} — Options that will be passed to HTMLDoc when the PDF is rendered.


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(options={})
  filename = options.delete(:filename)
  disposition = (options.delete(:as) || params[:as] || :inline).to_sym
  htmldoc_options = options.delete(:htmldoc) || {}
  render_options = options.merge(options.delete(:url) || {})
  if !render_options.include?(:layout)
    render_options[:layout] = false
  end

  send_data_options = { :type => content_type }
  send_data_options[:filename] = filename if filename
  send_data_options[: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(render_options)
  pdf_data = run_through_htmldoc(html_content, htmldoc_options)
  unless pdf_data.blank?
    send_data(pdf_data, send_data_options) 
  else
    render :text => "HTMLDoc had trouble parsing the HTML to create the PDF."
  end
end

- (Object) render_pdf_to_file(filename, render_options = {}, htmldoc_options = {})

Runs the given view through HTMLDoc and writes the generated PDF data to the file of your choice.

Examples:

Render the ‘blah’ view to ‘foo.pdf’

  render_pdf_to_file 'foo.pdf', :action => 'blah'

Same thing, but set a top-margin of 50px in the PDF

  render_pdf_to_file 'foo.pdf', { :action => 'blah' }, { :top => 50 }

Parameters:

  • (String) filename — The outfile
  • (Hash, String) render_options (defaults to: {}) — The hash/url that represents which view you want to render. Passed straight to render().
  • (Hash) htmldoc_options (defaults to: {}) — Options that will be passed to HTMLDoc when the PDF is rendered.


71
72
73
74
75
76
77
78
79
# File 'lib/htmldoc_rails/controller.rb', line 71

def render_pdf_to_file(filename, render_options={}, htmldoc_options={})
  if !render_options.include?(:layout)
    render_options[:layout] = false
  end
  headers["Content-Disposition"] = "inline"
  html_content = render_to_string(render_options)
  pdf_data = run_through_htmldoc(html_content, htmldoc_options)
  File.open(filename, 'w') {|f| f.write(pdf_data) }
end