lib/pdf/merger/jruby.rb in pdf-merger-0.2.0 vs lib/pdf/merger/jruby.rb in pdf-merger-0.3.0

- old
+ new

@@ -12,22 +12,36 @@ module PDF class Merger # Saves the PDF into a file defined by path given. - # return the number of pages in the new file - def save_as(output_file_path) + # - 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| - copy.addDocument(PdfReader.new(f)) + if File.exists?(f) + begin + copy.addDocument(PdfReader.new(f)) + rescue => e + failure_list << f + Rails.logger.warn "PDF::Merger: Invalid PDF: #{f}" + end + else + failure_list << f + Rails.logger.warn "PDF::Merger: File does not exist: #{f}" + end end - - copy.addJavaScript(@js) if @js.present? - copy.close() - PdfReader.new(output_file_path).getNumberOfPages + if @files_to_merge.size - failure_list.size > 0 + copy.addJavaScript(@js) if @js.present? + copy.close() + PdfReader.new(output_file_path).getNumberOfPages + else + 0 + end end end end