Class: Itext

Inherits:
Object
  • Object
show all
Includes:
Attachments, Signing
Defined in:
lib/itext.rb,
lib/itext-jruby.rb

Defined Under Namespace

Modules: Attachments, Signing

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Signing

#enable_signing!, included, #sign_file?

Methods included from Attachments

#add_attachments, included, #remove_attachments

Constructor Details

#initialize(*args, &block) ⇒ Itext

Create new Itext document instance Required params: path: Pass absolute path to pdf file

Raises:

  • (ArgumentError)


15
16
17
18
19
20
21
22
23
24
# File 'lib/itext.rb', line 15

def initialize(*args, &block)
  self.class.import_java     # Import required deps
  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

Class Method Details

.import_java(notify = false) ⇒ Object

Import all required jars and packages



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/itext-jruby.rb', line 7

def self.import_java(notify = false)
  Dir[File.expand_path(File.join(File.dirname(__FILE__), "../libexec/jar/*.jar"))].each { |jar| require jar }

  libs = [
    java.io.ByteArrayOutputStream,
    com.lowagie.text.html.WebColors,
    com.lowagie.text.Document,
    com.lowagie.text.Paragraph,
    com.lowagie.text.Phrase,
    com.lowagie.text.PageSize,
    com.lowagie.text.Chunk,
    com.lowagie.text.pdf.PdfPCell,
    com.lowagie.text.pdf.PdfPTable,
    com.lowagie.text.BadElementException,
    com.lowagie.text.DocumentException,
    com.lowagie.text.Image,
    com.lowagie.text.pdf.PdfFileSpecification,
    com.lowagie.text.pdf.PdfReader,
    com.lowagie.text.pdf.PdfSignatureAppearance,
    com.lowagie.text.pdf.PdfStamper,
    com.lowagie.text.pdf.PdfWriter,
    java.io.FileOutputStream
  ]

  # Require lib only if package not found
  libs.each do |lib|
    unless defined?(lib)
      java_import(lib)
    else
      puts "Java Import: Allready defined: #{lib}" if notify
    end
  end
end

Instance Method Details

#save(save_to = nil, opts = {}) ⇒ Object

Saves file to given destination Usage: save(path, options = {}) Allowed options: { sign_document: true }



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/itext.rb', line 29

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