Sha256: eab4c09677d8359cb00d4a4bcfcdb3cbf25581215499e40b21a0053f03a11645

Contents?: true

Size: 995 Bytes

Versions: 4

Compression:

Stored size: 995 Bytes

Contents

require 'tempfile'
require 'stringio'

class WickedPdf
  class Tempfile < ::Tempfile
    def initialize(filename, temp_dir = nil)
      temp_dir ||= Dir.tmpdir
      extension = File.extname(filename)
      basename = File.basename(filename, extension)
      super([basename, extension], temp_dir)
    end

    def write_in_chunks(input_string)
      binmode
      string_io = StringIO.new(input_string)
      write(string_io.read(chunk_size)) until string_io.eof?
      close
      self
    rescue Errno::EINVAL => e
      raise e, file_too_large_message
    end

    def read_in_chunks
      rewind
      binmode
      output_string = ''
      output_string << read(chunk_size) until eof?
      output_string
    rescue Errno::EINVAL => e
      raise e, file_too_large_message
    end

    private

    def chunk_size
      1024 * 1024
    end

    def file_too_large_message
      'The HTML file is too large! Try reducing the size or using the return_file option instead.'
    end
  end
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
wicked_pdf-2.8.1 lib/wicked_pdf/tempfile.rb
wicked_pdf-2.8.0 lib/wicked_pdf/tempfile.rb
adzap-wicked_pdf-2.0.0.beta4 lib/wicked_pdf/tempfile.rb
wicked_pdf-2.7.0 lib/wicked_pdf/tempfile.rb