Sha256: 017aae5dfe3a24d13189b60d5b1b47b80cf260d5f839092c0f75d28cb7a49264

Contents?: true

Size: 1.56 KB

Versions: 2

Compression:

Stored size: 1.56 KB

Contents

# encoding: utf-8
require 'pathname'
require 'tempfile'

class Itext
  require 'itext/attachments'
  require 'itext/signing'

  include Attachments
  include Signing

  # 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

  # Saves file to given destination
  # Usage: save(path, options = {})
  # Allowed options: { sign_document: true }
  def save(save_to = nil, opts = {})
    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  = initialize_stamper

    # 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

  # Creates default stamper
  def initialize_stamper
    stamper = super rescue nil
    if stamper
      stamper
    else
      # Initialize default stamper
      Java::ComLowagieTextPdf::PdfStamper.new @reader, @buffer
    end
  end

  def hooks
    @hooks || []
  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
itext-jruby-0.0.8 lib/itext.rb
itext-jruby-0.0.7 lib/itext.rb