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()
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]
html_string.gsub!( /src=["']+([^:]+?)["']/i ) { |m| %{src="#{RAILS_ROOT}/public/#{$1}"} }
end
html_string.gsub!( /src=["'](\S+\?\d*)["']/i ) { |m| %{src="#{$1.split('?').first}"} }
if options[:file]
prince.html_string_to_pdf_file(html_string, options[:file])
else
prince.html_string_to_pdf_string(html_string)
end
end
|