Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move all modules and classes into WickedPdf namespace #538

Merged
merged 1 commit into from
May 24, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
180 changes: 91 additions & 89 deletions lib/wicked_pdf/pdf_helper.rb
Original file line number Diff line number Diff line change
@@ -1,112 +1,114 @@
module PdfHelper
def self.included(base)
# Protect from trying to augment modules that appear
# as the result of adding other gems.
return if base != ActionController::Base
class WickedPdf
module PdfHelper
def self.included(base)
# Protect from trying to augment modules that appear
# as the result of adding other gems.
return if base != ActionController::Base

base.class_eval do
alias_method_chain :render, :wicked_pdf
alias_method_chain :render_to_string, :wicked_pdf
after_filter :clean_temp_files
base.class_eval do
alias_method_chain :render, :wicked_pdf
alias_method_chain :render_to_string, :wicked_pdf
after_filter :clean_temp_files
end
end
end

def render_with_wicked_pdf(options = nil, *args, &block)
if options.is_a?(Hash) && options.key?(:pdf)
log_pdf_creation
options[:basic_auth] = set_basic_auth(options)
make_and_send_pdf(options.delete(:pdf), (WickedPdf.config || {}).merge(options))
else
render_without_wicked_pdf(options, *args, &block)
def render_with_wicked_pdf(options = nil, *args, &block)
if options.is_a?(Hash) && options.key?(:pdf)
log_pdf_creation
options[:basic_auth] = set_basic_auth(options)
make_and_send_pdf(options.delete(:pdf), (WickedPdf.config || {}).merge(options))
else
render_without_wicked_pdf(options, *args, &block)
end
end
end

def render_to_string_with_wicked_pdf(options = nil, *args, &block)
if options.is_a?(Hash) && options.key?(:pdf)
log_pdf_creation
options[:basic_auth] = set_basic_auth(options)
options.delete :pdf
make_pdf((WickedPdf.config || {}).merge(options))
else
render_to_string_without_wicked_pdf(options, *args, &block)
def render_to_string_with_wicked_pdf(options = nil, *args, &block)
if options.is_a?(Hash) && options.key?(:pdf)
log_pdf_creation
options[:basic_auth] = set_basic_auth(options)
options.delete :pdf
make_pdf((WickedPdf.config || {}).merge(options))
else
render_to_string_without_wicked_pdf(options, *args, &block)
end
end
end

private

def log_pdf_creation
logger.info '*' * 15 + 'WICKED' + '*' * 15 if logger && logger.respond_to?(:info)
end
private

def set_basic_auth(options = {})
options[:basic_auth] ||= WickedPdf.config.fetch(:basic_auth) { false }
return unless options[:basic_auth] && request.env['HTTP_AUTHORIZATION']
request.env['HTTP_AUTHORIZATION'].split(' ').last
end
def log_pdf_creation
logger.info '*' * 15 + 'WICKED' + '*' * 15 if logger && logger.respond_to?(:info)
end

def clean_temp_files
return unless defined?(@hf_tempfiles)
@hf_tempfiles.each(&:close!)
end
def set_basic_auth(options = {})
options[:basic_auth] ||= WickedPdf.config.fetch(:basic_auth) { false }
return unless options[:basic_auth] && request.env['HTTP_AUTHORIZATION']
request.env['HTTP_AUTHORIZATION'].split(' ').last
end

def make_pdf(options = {})
render_opts = {
:template => options[:template],
:layout => options[:layout],
:formats => options[:formats],
:handlers => options[:handlers]
}
render_opts[:locals] = options[:locals] if options[:locals]
render_opts[:file] = options[:file] if options[:file]
html_string = render_to_string(render_opts)
options = prerender_header_and_footer(options)
w = WickedPdf.new(options[:wkhtmltopdf])
w.pdf_from_string(html_string, options)
end
def clean_temp_files
return unless defined?(@hf_tempfiles)
@hf_tempfiles.each(&:close!)
end

def make_and_send_pdf(pdf_name, options = {})
options[:wkhtmltopdf] ||= nil
options[:layout] ||= false
options[:template] ||= File.join(controller_path, action_name)
options[:disposition] ||= 'inline'
if options[:show_as_html]
def make_pdf(options = {})
render_opts = {
:template => options[:template],
:layout => options[:layout],
:formats => options[:formats],
:handlers => options[:handlers],
:content_type => 'text/html'
:handlers => options[:handlers]
}
render_opts[:locals] = options[:locals] if options[:locals]
render_opts[:file] = options[:file] if options[:file]
render(render_opts)
else
pdf_content = make_pdf(options)
File.open(options[:save_to_file], 'wb') { |file| file << pdf_content } if options[:save_to_file]
send_data(pdf_content, :filename => pdf_name + '.pdf', :type => 'application/pdf', :disposition => options[:disposition]) unless options[:save_only]
html_string = render_to_string(render_opts)
options = prerender_header_and_footer(options)
w = WickedPdf.new(options[:wkhtmltopdf])
w.pdf_from_string(html_string, options)
end
end

# Given an options hash, prerenders content for the header and footer sections
# to temp files and return a new options hash including the URLs to these files.
def prerender_header_and_footer(options)
[:header, :footer].each do |hf|
next unless options[hf] && options[hf][:html] && options[hf][:html][:template]
@hf_tempfiles = [] unless defined?(@hf_tempfiles)
@hf_tempfiles.push(tf = WickedPdfTempfile.new("wicked_#{hf}_pdf.html"))
options[hf][:html][:layout] ||= options[:layout]
render_opts = {
:template => options[hf][:html][:template],
:layout => options[hf][:html][:layout],
:formats => options[hf][:html][:formats],
:handlers => options[hf][:html][:handlers]
}
render_opts[:locals] = options[hf][:html][:locals] if options[hf][:html][:locals]
render_opts[:file] = options[hf][:html][:file] if options[:file]
tf.write render_to_string(render_opts)
tf.flush
options[hf][:html][:url] = "file:///#{tf.path}"
def make_and_send_pdf(pdf_name, options = {})
options[:wkhtmltopdf] ||= nil
options[:layout] ||= false
options[:template] ||= File.join(controller_path, action_name)
options[:disposition] ||= 'inline'
if options[:show_as_html]
render_opts = {
:template => options[:template],
:layout => options[:layout],
:formats => options[:formats],
:handlers => options[:handlers],
:content_type => 'text/html'
}
render_opts[:locals] = options[:locals] if options[:locals]
render_opts[:file] = options[:file] if options[:file]
render(render_opts)
else
pdf_content = make_pdf(options)
File.open(options[:save_to_file], 'wb') { |file| file << pdf_content } if options[:save_to_file]
send_data(pdf_content, :filename => pdf_name + '.pdf', :type => 'application/pdf', :disposition => options[:disposition]) unless options[:save_only]
end
end

# Given an options hash, prerenders content for the header and footer sections
# to temp files and return a new options hash including the URLs to these files.
def prerender_header_and_footer(options)
[:header, :footer].each do |hf|
next unless options[hf] && options[hf][:html] && options[hf][:html][:template]
@hf_tempfiles = [] unless defined?(@hf_tempfiles)
@hf_tempfiles.push(tf = WickedPdfTempfile.new("wicked_#{hf}_pdf.html"))
options[hf][:html][:layout] ||= options[:layout]
render_opts = {
:template => options[hf][:html][:template],
:layout => options[hf][:html][:layout],
:formats => options[hf][:html][:formats],
:handlers => options[hf][:html][:handlers]
}
render_opts[:locals] = options[hf][:html][:locals] if options[hf][:html][:locals]
render_opts[:file] = options[hf][:html][:file] if options[:file]
tf.write render_to_string(render_opts)
tf.flush
options[hf][:html][:url] = "file:///#{tf.path}"
end
options
end
options
end
end
56 changes: 29 additions & 27 deletions lib/wicked_pdf/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,45 @@
require 'wicked_pdf/wicked_pdf_helper'
require 'wicked_pdf/wicked_pdf_helper/assets'

if defined?(Rails)
class WickedPdf
if defined?(Rails)

if Rails::VERSION::MAJOR == 3

class WickedRailtie < Rails::Railtie
initializer 'wicked_pdf.register' do |_app|
ActionController::Base.send :include, PdfHelper
if Rails::VERSION::MINOR > 0 && Rails.configuration.assets.enabled
ActionView::Base.send :include, WickedPdfHelper::Assets
else
ActionView::Base.send :include, WickedPdfHelper
end
end
end

if Rails::VERSION::MAJOR == 3
elsif Rails::VERSION::MAJOR == 2

class WickedRailtie < Rails::Railtie
initializer 'wicked_pdf.register' do |_app|
unless ActionController::Base.instance_methods.include? 'render_with_wicked_pdf'
ActionController::Base.send :include, PdfHelper
if Rails::VERSION::MINOR > 0 && Rails.configuration.assets.enabled
end
unless ActionView::Base.instance_methods.include? 'wicked_pdf_stylesheet_link_tag'
ActionView::Base.send :include, WickedPdfHelper
end

else

class WickedRailtie < Rails::Railtie
initializer 'wicked_pdf.register' do |_app|
ActionController::Base.send :include, PdfHelper
ActionView::Base.send :include, WickedPdfHelper::Assets
else
ActionView::Base.send :include, WickedPdfHelper
end
end
end

elsif Rails::VERSION::MAJOR == 2

unless ActionController::Base.instance_methods.include? 'render_with_wicked_pdf'
ActionController::Base.send :include, PdfHelper
end
unless ActionView::Base.instance_methods.include? 'wicked_pdf_stylesheet_link_tag'
ActionView::Base.send :include, WickedPdfHelper
end

else

class WickedRailtie < Rails::Railtie
initializer 'wicked_pdf.register' do |_app|
ActionController::Base.send :include, PdfHelper
ActionView::Base.send :include, WickedPdfHelper::Assets
end
if Mime::Type.lookup_by_extension(:pdf).nil?
Mime::Type.register('application/pdf', :pdf)
end

end

if Mime::Type.lookup_by_extension(:pdf).nil?
Mime::Type.register('application/pdf', :pdf)
end

end
16 changes: 9 additions & 7 deletions lib/wicked_pdf/tempfile.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
require 'tempfile'

class WickedPdfTempfile < Tempfile
# ensures the Tempfile's filename always keeps its extension
def initialize(filename, temp_dir = nil)
temp_dir ||= Dir.tmpdir
extension = File.extname(filename)
basename = File.basename(filename, extension)
super([basename, extension], temp_dir)
class WickedPdf
class WickedPdfTempfile < Tempfile
# ensures the Tempfile's filename always keeps its extension
def initialize(filename, temp_dir = nil)
temp_dir ||= Dir.tmpdir
extension = File.extname(filename)
basename = File.basename(filename, extension)
super([basename, extension], temp_dir)
end
end
end
54 changes: 28 additions & 26 deletions lib/wicked_pdf/wicked_pdf_helper.rb
Original file line number Diff line number Diff line change
@@ -1,33 +1,35 @@
module WickedPdfHelper
def self.root_path
String === Rails.root ? Pathname.new(Rails.root) : Rails.root
end
class WickedPdf
module WickedPdfHelper
def self.root_path
String === Rails.root ? Pathname.new(Rails.root) : Rails.root
end

def self.add_extension(filename, extension)
filename.to_s.split('.').include?(extension) ? filename : "#{filename}.#{extension}"
end
def self.add_extension(filename, extension)
filename.to_s.split('.').include?(extension) ? filename : "#{filename}.#{extension}"
end

def wicked_pdf_stylesheet_link_tag(*sources)
css_dir = WickedPdfHelper.root_path.join('public', 'stylesheets')
css_text = sources.collect do |source|
source = WickedPdfHelper.add_extension(source, 'css')
"<style type='text/css'>#{File.read(css_dir.join(source))}</style>"
end.join("\n")
css_text.respond_to?(:html_safe) ? css_text.html_safe : css_text
end
def wicked_pdf_stylesheet_link_tag(*sources)
css_dir = WickedPdfHelper.root_path.join('public', 'stylesheets')
css_text = sources.collect do |source|
source = WickedPdfHelper.add_extension(source, 'css')
"<style type='text/css'>#{File.read(css_dir.join(source))}</style>"
end.join("\n")
css_text.respond_to?(:html_safe) ? css_text.html_safe : css_text
end

def wicked_pdf_image_tag(img, options = {})
image_tag "file:///#{WickedPdfHelper.root_path.join('public', 'images', img)}", options
end
def wicked_pdf_image_tag(img, options = {})
image_tag "file:///#{WickedPdfHelper.root_path.join('public', 'images', img)}", options
end

def wicked_pdf_javascript_src_tag(jsfile, options = {})
jsfile = WickedPdfHelper.add_extension(jsfile, 'js')
src = "file:///#{WickedPdfHelper.root_path.join('public', 'javascripts', jsfile)}"
content_tag('script', '', { 'type' => Mime::JS, 'src' => path_to_javascript(src) }.merge(options))
end
def wicked_pdf_javascript_src_tag(jsfile, options = {})
jsfile = WickedPdfHelper.add_extension(jsfile, 'js')
src = "file:///#{WickedPdfHelper.root_path.join('public', 'javascripts', jsfile)}"
content_tag('script', '', { 'type' => Mime::JS, 'src' => path_to_javascript(src) }.merge(options))
end

def wicked_pdf_javascript_include_tag(*sources)
js_text = sources.collect { |source| wicked_pdf_javascript_src_tag(source, {}) }.join("\n")
js_text.respond_to?(:html_safe) ? js_text.html_safe : js_text
def wicked_pdf_javascript_include_tag(*sources)
js_text = sources.collect { |source| wicked_pdf_javascript_src_tag(source, {}) }.join("\n")
js_text.respond_to?(:html_safe) ? js_text.html_safe : js_text
end
end
end
Loading