Class: WickedPdf
- Inherits:
-
Object
- Object
- WickedPdf
- Includes:
- Progress
- Defined in:
- lib/wicked_pdf.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/wicked_pdf_helper.rb,
lib/wicked_pdf/wicked_pdf_helper/assets.rb
Defined Under Namespace
Modules: PdfHelper, Progress, WickedPdfHelper Classes: Middleware, WickedPdfTempfile, WickedRailtie
Constant Summary collapse
- DEFAULT_BINARY_VERSION =
Gem::Version.new('0.9.9')
- BINARY_VERSION_WITHOUT_DASHES =
Gem::Version.new('0.12.0')
- EXE_NAME =
'wkhtmltopdf'.freeze
- VERSION =
'1.2.0'.freeze
- @@config =
{}
Instance Attribute Summary collapse
-
#binary_version ⇒ Object
Returns the value of attribute binary_version.
Instance Method Summary collapse
-
#initialize(wkhtmltopdf_binary_path = nil) ⇒ WickedPdf
constructor
A new instance of WickedPdf.
- #pdf_from_html_file(filepath, options = {}) ⇒ Object
- #pdf_from_string(string, options = {}) ⇒ Object
- #pdf_from_url(url, options = {}) ⇒ Object
Methods included from Progress
#invoke_with_progress, #track_progress?
Constructor Details
#initialize(wkhtmltopdf_binary_path = nil) ⇒ WickedPdf
Returns a new instance of WickedPdf.
42 43 44 45 46 47 48 49 |
# File 'lib/wicked_pdf.rb', line 42 def initialize(wkhtmltopdf_binary_path = nil) @exe_path = wkhtmltopdf_binary_path || find_wkhtmltopdf_binary_path raise "Location of #{EXE_NAME} unknown" if @exe_path.empty? raise "Bad #{EXE_NAME}'s path: #{@exe_path}" unless File.exist?(@exe_path) raise "#{EXE_NAME} is not executable" unless File.executable?(@exe_path) retrieve_binary_version end |
Instance Attribute Details
#binary_version ⇒ Object
Returns the value of attribute binary_version.
38 39 40 |
# File 'lib/wicked_pdf.rb', line 38 def binary_version @binary_version end |
Instance Method Details
#pdf_from_html_file(filepath, options = {}) ⇒ Object
51 52 53 |
# File 'lib/wicked_pdf.rb', line 51 def pdf_from_html_file(filepath, = {}) pdf_from_url("file:///#{filepath}", ) end |
#pdf_from_string(string, options = {}) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/wicked_pdf.rb', line 55 def pdf_from_string(string, = {}) = .dup .merge!(WickedPdf.config) { |_key, option, _config| option } string_file = WickedPdfTempfile.new('wicked_pdf.html', [:temp_path]) string_file.binmode string_file.write(string) string_file.close pdf = pdf_from_html_file(string_file.path, ) pdf ensure string_file.close! if string_file end |
#pdf_from_url(url, options = {}) ⇒ Object
69 70 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 |
# File 'lib/wicked_pdf.rb', line 69 def pdf_from_url(url, = {}) # merge in global config options .merge!(WickedPdf.config) { |_key, option, _config| option } generated_pdf_file = WickedPdfTempfile.new('wicked_pdf_generated_file.pdf', [:temp_path]) command = [@exe_path] command += () command << url command << generated_pdf_file.path.to_s print_command(command.inspect) if in_development_mode? if track_progress?() invoke_with_progress(command, ) else err = Open3.popen3(*command) do |_stdin, _stdout, stderr| stderr.read end end if [:return_file] return_file = .delete(:return_file) return generated_pdf_file end generated_pdf_file.rewind generated_pdf_file.binmode pdf = generated_pdf_file.read raise "Error generating PDF\n Command Error: #{err}" if [: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 generated_pdf_file.close! if generated_pdf_file && !return_file end |