Sha256: 21d823b80bf0f5aa40faa49a8bf1ff2992475326cb54d20eb2667729d32048bd

Contents?: true

Size: 1.34 KB

Versions: 3

Compression:

Stored size: 1.34 KB

Contents

require 'pdf_ravager/strategies/xfa'
require 'pdf_ravager/strategies/acro_form'
require 'pdf_ravager/strategies/smart'

unless RUBY_PLATFORM =~ /java/
  raise "You can only ravage PDFs using JRuby, not #{RUBY_PLATFORM}!"
end

require 'java'
require 'nokogiri'
require File.dirname(__FILE__)  + '/../../vendor/iText-4.2.0'

module PDFRavager
  class Ravager
    def self.ravage(*args, &blk)
      warn "[DEPRECATION] Please use PDFRavager::Ravager's instance " +
           "methods instead of the `::ravage` method"
      new(*args, &blk).ravage
    end

    def initialize(template, opts={})
      @opts = opts
      opts = {:in_file => opts} if opts.is_a? String
      @out = if opts[:out_file]
        java.io.FileOutputStream.new(opts[:out_file])
      else
        java.io.ByteArrayOutputStream.new
      end
      @template = template
      reader = com.lowagie.text.pdf.PdfReader.new(opts[:in_file])
      @stamper = com.lowagie.text.pdf.PdfStamper.new(reader, @out)
      @strategy = case template.strategy
      when :acro_forms
        Strategies::AcroForm.new(@stamper)
      when :xfa
        Strategies::XFA.new(@stamper)
      when :smart
        Strategies::Smart.new(@stamper)
      end
    end

    def ravage
      @strategy.set_field_values(@template)
      @strategy.set_read_only if @opts[:read_only]
      @stamper.close
      @out
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
pdf_ravager-0.2.2 lib/pdf_ravager/ravager.rb
pdf_ravager-0.2.1 lib/pdf_ravager/ravager.rb
pdf_ravager-0.2.0 lib/pdf_ravager/ravager.rb