Class: WickedPdf
- Inherits:
-
Object
- Object
- WickedPdf
- Defined in:
- lib/wicked_pdf.rb,
lib/wicked_pdf_version.rb,
lib/wicked_pdf_middleware.rb
Defined Under Namespace
Classes: Middleware
Constant Summary collapse
- DEFAULT_BINARY_VERSION =
Gem::Version.new('0.9.9')
- EXE_NAME =
"wkhtmltopdf"
- VERSION =
'0.9.7'
- @@config =
{}
Instance Method Summary collapse
-
#initialize(wkhtmltopdf_binary_path = nil) ⇒ WickedPdf
constructor
A new instance of WickedPdf.
- #pdf_from_string(string, options = {}) ⇒ Object
- #retreive_binary_version ⇒ Object
Constructor Details
#initialize(wkhtmltopdf_binary_path = nil) ⇒ WickedPdf
Returns a new instance of WickedPdf.
27 28 29 30 31 32 33 34 |
# File 'lib/wicked_pdf.rb', line 27 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" unless File.exists?(@exe_path) raise "#{EXE_NAME} is not executable" unless File.executable?(@exe_path) @binary_version = DEFAULT_BINARY_VERSION end |
Instance Method Details
#pdf_from_string(string, options = {}) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/wicked_pdf.rb', line 44 def pdf_from_string(string, ={}) if WickedPdf.config[:retreive_version] retreive_binary_version end temp_path = .delete(:temp_path) string_file = WickedPdfTempfile.new("wicked_pdf.html", temp_path) string_file.binmode string_file.write(string) string_file.close generated_pdf_file = WickedPdfTempfile.new("wicked_pdf_generated_file.pdf", temp_path) command = "\"#{@exe_path}\" #{'-q ' unless on_windows?}#{()} \"file:///#{string_file.path}\" \"#{generated_pdf_file.path}\" " # -q for no errors on stdout print_command(command) if in_development_mode? err = Open3.popen3(command) do |stdin, stdout, stderr| stderr.read end if return_file = .delete(:return_file) return generated_pdf_file end generated_pdf_file.rewind generated_pdf_file.binmode pdf = generated_pdf_file.read raise "PDF could not be generated!\n Command Error: #{err}" if pdf and pdf.rstrip.length == 0 pdf rescue Exception => e raise "Failed to execute:\n#{command}\nError: #{e}" ensure string_file.close! if string_file generated_pdf_file.close! if generated_pdf_file && !return_file end |
#retreive_binary_version ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/wicked_pdf.rb', line 36 def retreive_binary_version begin stdin, stdout, stderr = Open3.popen3(@exe_path + ' -V') @binary_version = parse_version(stdout.gets(nil)) rescue StandardError end end |