Sha256: 8e036e634b0c790ba420aea6fa6b9a4a561fe6445a38dd6c96ce180f5103577a

Contents?: true

Size: 1.18 KB

Versions: 4

Compression:

Stored size: 1.18 KB

Contents

# encoding: utf-8
require 'pathname'

class Itext
  require 'itext/attachments'

  include Attachments

  # Create new Itext document instance
  # Required params:
  # path: Pass absolute path to pdf file
  def initialize(*args, &block)
    super

    opts   = args[0] if args.is_a?(Array)
    @path  = opts[:path]

    raise ArgumentError.new('Missing :path') if @path.nil?
    raise ArgumentError.new('Please provide absolute path') unless Pathname.new(@path).absolute?
  end

  def save(save_to = nil)
    save_to ||= @path

    output_file = if File.exists?(@path)
      Tempfile.new(['temp_pdf', '.pdf'])
    else
      File.open(save_to, "r+") 
    end

    @reader   = Java::ComLowagieTextPdf::PdfReader.new(@path.to_java(:string))
    @buffer   = Java::JavaIo::FileOutputStream.new output_file.path
    @stamper  = Java::ComLowagieTextPdf::PdfStamper.new @reader, @buffer

    # Run all attached hooks
    @hooks.each { |hook| hook.call }

    @stamper.close

    if output_file.is_a?(Tempfile)
      FileUtils.rm(save_to) if File.exists?(save_to)
      FileUtils.cp(output_file.path, save_to) 
    end
    
    # Return output path
    save_to
  end

  protected

  def hooks
    @hooks || []
  end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
itext-jruby-0.0.6 lib/itext.rb
itext-jruby-0.0.5 lib/itext.rb
itext-jruby-0.0.4 lib/itext.rb
itext-jruby-0.0.3 lib/itext.rb