Sha256: ed8d7691d8e2206a6e7828032dbaafd1d72b125a94506d57215d48115fc51409

Contents?: true

Size: 1.25 KB

Versions: 1

Compression:

Stored size: 1.25 KB

Contents

# = pdf/merger/rjb.rb -- PDF template stamping.
#
#  Copyright 2010-14 Paul Schreiber

$:.unshift(File.join(File.dirname(__FILE__), '..', '..', '..', 'ext'))
require 'java'
require 'iText-4.2.0.jar'

java_import 'java.io.FileOutputStream'
java_import 'com.lowagie.text.pdf.PdfReader'
java_import 'com.lowagie.text.pdf.PdfCopyFields'

module PDF
  class Merger

    # Saves the PDF into a file defined by path given.
    # - return the number of pages in the new file
    # - populate failure_list with paths of missing or invalid PDFs
    def save_as(output_file_path, failure_list=[])
      filestream = FileOutputStream.new(output_file_path)
      copy = PdfCopyFields.new(filestream)

      @files_to_merge.each do |f|
        if File.exists?(f)
          begin
            copy.addDocument(PdfReader.new(f))
          rescue => e
            failure_list << f
            log "PDF::Merger: Invalid PDF: #{f}"
          end
        else
          failure_list << f
          log "PDF::Merger: File does not exist: #{f}"
        end
      end

      if @files_to_merge.size - failure_list.size > 0
        copy.addJavaScript(@js) if @js && !@js.empty?
        copy.close()
        PdfReader.new(output_file_path).getNumberOfPages
      else
        0
      end
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pdf-merger-0.3.2 lib/pdf/merger/jruby.rb