Module: PdfHelper

Defined in:
lib/rprince/pdf_helper.rb

Instance Method Summary collapse

Instance Method Details

#rprince_pdf(options = {}) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/rprince/pdf_helper.rb', line 4

def rprince_pdf(options = {})
  options[:stylesheets] ||= []
  options[:layout]      ||= false
  options[:template]    ||= File.join(controller_path, action_name)

  unless options.has_key?(:normalize_sources)
    options[:normalize_sources] = true
  end

  prince = RPrince.new()

  # Sets style sheets on PDF renderer
  prince.style_sheets = options[:stylesheets].map{|style| stylesheet_file_path(style) }
  html_string = render_to_string(:template => options[:template], :layout => options[:layout])

  if options[:normalize_sources]
    # Make all paths relative, on disk paths...
    html_string.gsub!( /src=["']+([^:]+?)["']/i ) { |m| %{src="#{RAILS_ROOT}/public/#{$1}"} }
  end
  
  # Remove asset ids
  html_string.gsub!( /src=["'](\S+\?\d*)["']/i ) { |m| %{src="#{$1.split('?').first}"} }


  # Send the generated PDF file from our html string.
  if options[:file]
    prince.html_string_to_pdf_file(html_string, options[:file])
  else
    prince.html_string_to_pdf_string(html_string)
  end
end

#send_rprince_pdf(pdf_name, options = {}) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/rprince/pdf_helper.rb', line 36

def send_rprince_pdf(pdf_name, options = {})
  options.delete(:file)
  send_data(rprince_pdf(options),
    :filename => "#{pdf_name}.pdf",
    :type => 'application/pdf'
  )
end