Class: WickedPdf
- Inherits:
-
Object
show all
- Includes:
- Progress
- Defined in:
- lib/wicked_pdf.rb,
lib/wicked_pdf/binary.rb,
lib/wicked_pdf/railtie.rb,
lib/wicked_pdf/version.rb,
lib/wicked_pdf/progress.rb,
lib/wicked_pdf/tempfile.rb,
lib/wicked_pdf/middleware.rb,
lib/wicked_pdf/pdf_helper.rb,
lib/wicked_pdf/option_parser.rb,
lib/wicked_pdf/wicked_pdf_helper.rb,
lib/wicked_pdf/wicked_pdf_helper/assets.rb
Defined Under Namespace
Modules: PdfHelper, Progress, WickedPdfHelper
Classes: Binary, Middleware, OptionParser, Tempfile, WickedRailtie
Constant Summary
collapse
- DEFAULT_BINARY_VERSION =
Gem::Version.new('0.9.9')
- VERSION =
'2.8.2'.freeze
- @@config =
{}
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Progress
#invoke_with_progress, #track_progress?
Constructor Details
#initialize(wkhtmltopdf_binary_path = nil) ⇒ WickedPdf
Returns a new instance of WickedPdf.
45
46
47
|
# File 'lib/wicked_pdf.rb', line 45
def initialize(wkhtmltopdf_binary_path = nil)
@binary = Binary.new(wkhtmltopdf_binary_path, DEFAULT_BINARY_VERSION)
end
|
Class Method Details
.clear_config ⇒ Object
41
42
43
|
# File 'lib/wicked_pdf.rb', line 41
def self.clear_config
@@config = {}
end
|
.config=(config) ⇒ Object
28
29
30
31
32
|
# File 'lib/wicked_pdf.rb', line 28
def self.config=(config)
::Kernel.warn 'WickedPdf.config= is deprecated and will be removed in future versions. Use WickedPdf.configure instead.' unless @@silence_deprecations
@@config = config
end
|
34
35
36
37
38
39
|
# File 'lib/wicked_pdf.rb', line 34
def self.configure
config = OpenStruct.new(@@config)
yield config
@@config.merge! config.to_h
end
|
Instance Method Details
#binary_version ⇒ Object
49
50
51
|
# File 'lib/wicked_pdf.rb', line 49
def binary_version
@binary.version
end
|
#pdf_from_html_file(filepath, options = {}) ⇒ Object
53
54
55
|
# File 'lib/wicked_pdf.rb', line 53
def pdf_from_html_file(filepath, options = {})
pdf_from_url("file:///#{filepath}", options)
end
|
#pdf_from_string(string, options = {}) ⇒ Object
57
58
59
60
61
62
63
64
65
66
67
68
69
|
# File 'lib/wicked_pdf.rb', line 57
def pdf_from_string(string, options = {})
options = options.dup
options.merge!(WickedPdf.config) { |_key, option, _config| option }
string_file = WickedPdf::Tempfile.new('wicked_pdf.html', options[:temp_path])
string_file.write_in_chunks(string)
pdf_from_html_file(string_file.path, options)
ensure
if options[:delete_temporary_files] && string_file
string_file.close!
elsif string_file
string_file.close
end
end
|
#pdf_from_url(url, options = {}) ⇒ Object
rubocop:disable Metrics/CyclomaticComplexity
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
# File 'lib/wicked_pdf.rb', line 71
def pdf_from_url(url, options = {}) options.merge!(WickedPdf.config) { |_key, option, _config| option }
generated_pdf_file = WickedPdf::Tempfile.new('wicked_pdf_generated_file.pdf', options[:temp_path])
command = [@binary.path]
command.unshift(@binary.xvfb_run_path) if options[:use_xvfb]
command += option_parser.parse(options)
command << url
command << generated_pdf_file.path.to_s
print_command(command.inspect) if in_development_mode?
if track_progress?(options)
invoke_with_progress(command, options)
else
_out, err, status = Open3.capture3(*command)
err = [status.to_s, err].join("\n") if !err.empty? || !status.success?
end
if options[:return_file]
return_file = options.delete(:return_file)
return generated_pdf_file
end
pdf = generated_pdf_file.read_in_chunks
raise "Error generating PDF\n Command Error: #{err}" if options[:raise_on_all_errors] && !err.empty?
raise "PDF could not be generated!\n Command Error: #{err}" if pdf && pdf.rstrip.empty?
pdf
rescue StandardError => e
raise "Failed to execute:\n#{command}\nError: #{e}"
ensure
clean_temp_files
generated_pdf_file.close! if generated_pdf_file && !return_file
end
|