Sha256: 67104a65b7e1874d7b72a5e8c1f09bd0828ced89338de13f496cda0aa4deb0f9

Contents?: true

Size: 1.39 KB

Versions: 3

Compression:

Stored size: 1.39 KB

Contents

module WickedPdf
  class Document
    def initialize(command = Command.new)
      @command = command
    end

    def pdf_from_html_file(filepath, options = {})
      pdf_from_url("file:///#{filepath}", options)
    end

    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.binmode
      string_file.write(string)
      string_file.close

      pdf_from_html_file(string_file.path, options)
    ensure
      string_file.close! if string_file
    end

    def pdf_from_url(url, options = {})
      # merge in global config options
      options.merge!(WickedPdf.config) { |_key, option, _config| option }
      generated_pdf_file = WickedPdf::Tempfile.new('wicked_pdf_generated_file.pdf', options[:temp_path])

      result = @command.execute(options, url, generated_pdf_file.path.to_s)

      if options[:return_file]
        return_file = options.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: #{result}" if pdf && pdf.rstrip.empty?

      pdf
    ensure
      generated_pdf_file.close! if generated_pdf_file && !return_file
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
adzap-wicked_pdf-2.0.0.beta3 lib/wicked_pdf/document.rb
adzap-wicked_pdf-2.0.0.beta2 lib/wicked_pdf/document.rb
adzap-wicked_pdf-2.0.0.beta1 lib/wicked_pdf/document.rb