lib/ezprint/pdf_helper.rb in ezprint-0.2.2 vs lib/ezprint/pdf_helper.rb in ezprint-0.3.1
- old
+ new
@@ -1,15 +1,13 @@
-module EzPrint
+module Ezprint
module PdfHelper
- require 'pdfkit'
-
def self.included(base)
base.class_eval do
alias_method_chain :render, :ezprint
end
end
-
+
def render_with_ezprint(options = nil, *args, &block)
if options.is_a?(Symbol) or options.nil? or options[:pdf].nil?
render_without_ezprint(options, *args, &block)
else
options[:name] ||= options.delete(:pdf)
@@ -18,22 +16,18 @@
end
private
def make_pdf(options = {})
- stylesheets = options.delete(:stylesheets) || []
- layout = options.delete(:layout) || false
- template = options.delete(:template) || File.join(controller_path,action_name)
+ template = options.delete(:template) || File.join(controller_path,action_name)
+ layout = options.delete(:layout) || false
+ stylesheets = options[:stylesheets] || []
+ options[:stylesheets] = stylesheets.map { |s| stylesheet_file_path(s) }
- # Stop Rails from appending timestamps to assets.
- ENV["RAILS_ASSET_ID"] = ''
+ ENV["RAILS_ASSET_ID"] = '' # Stop Rails from appending timestamps to assets
html_string = render_to_string(:template => template, :layout => layout)
-
- kit = PDFKit.new(process_html_string(html_string), options)
- kit.stylesheets = stylesheets.collect{ |style| stylesheet_file_path(style) }
-
- kit.to_pdf
+ Ezprint.get_processor.send(:process, html_string, options)
end
def make_and_send_pdf(pdf_name, options = {})
filename = "#{pdf_name}.pdf"
if request.headers['User-Agent'] =~ /MSIE ([0-9]{1,}[\.0-9]{0,})/
@@ -51,17 +45,15 @@
)
end
def stylesheet_file_path(stylesheet)
stylesheet = stylesheet.to_s.gsub(".css","")
- File.join(ActionView::Helpers::AssetTagHelper::STYLESHEETS_DIR,"#{stylesheet}.css")
- end
- def process_html_string(html)
- # reroute absolute paths
- html.gsub!("src=\"/", "src=\"#{RAILS_ROOT}/public/")
- html.gsub!("href=\"/", "src=\"#{RAILS_ROOT}/public/")
- html.gsub!("url(/", "url(#{RAILS_ROOT}/public/")
- html
+ stylesheets_dir = if defined? ActionView::Helpers::AssetTagHelper::STYLESHEETS_DIR
+ ActionView::Helpers::AssetTagHelper::STYLESHEETS_DIR
+ else
+ config.stylesheets_dir
+ end
+ File.join(stylesheets_dir, "#{stylesheet}.css")
end
end
end