Sha256: 8ef97222d0eb7a2112998ff04a04cae4b1c27d42695d1ae3233460e5f946e6dc

Contents?: true

Size: 1.62 KB

Versions: 2

Compression:

Stored size: 1.62 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.write_in_chunks(string)

      pdf_from_html_file(string_file.path, options)
    rescue Errno::EINVAL => e
      Rails.logger.error '[wicked_pdf] The HTML file is too large! Try reducing the size or using the return_file option instead.'
      raise e
    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])
      return_file = options.delete(:return_file)

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

      if return_file
        return generated_pdf_file
      end

      pdf = generated_pdf_file.read_in_chunks

      raise "PDF could not be generated!\n Command Error: #{err}" if pdf && pdf.rstrip.empty?

      pdf
    rescue Errno::EINVAL => e
      Rails.logger.error '[wicked_pdf] The PDF file is too large! Try reducing the size or using the return_file option instead.'
      raise e
    ensure
      generated_pdf_file.close! if generated_pdf_file && !return_file
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
adzap-wicked_pdf-2.0.0.beta5 lib/wicked_pdf/document.rb
adzap-wicked_pdf-2.0.0.beta4 lib/wicked_pdf/document.rb