Sha256: 6e50e4129dbf1a714a9777cae5e2d0d71aa12178ca1bef8944a9090af37c5340

Contents?: true

Size: 1.56 KB

Versions: 1

Compression:

Stored size: 1.56 KB

Contents

module GBDev
  
  module PDF
  
    # A book that represents collection of PDF page.
    class Book
      
      def initialize()
        @pages = []
      end
      
      # Add a page to the book
      def add_page(new_page)
        @pages << new_page
      end
      
      # Renders the book with the given pages and saves to the given filename path.
      #
      # String filename : A path to the file to be created.      
      def save_to(filename)
        dir = File.dirname(filename)             
        temp_dir = [dir, "collection_temp_#{build_random_string}"].join('/')
        Dir.mkdir(temp_dir)
        @pages.each_with_index do |page, indx| 
          page.save_to([temp_dir, "#{indx}_#{build_random_file_name}"].join('/'))
        end
        temp_files = Dir[[temp_dir,'*'].join('/')].sort                 
            
        document = Document.new
        copier = PdfCopy.new(document, FileOutputStream.new(filename))
      
        document.open        
        temp_files.each do |read_target|
          reader = PdfReader.new(read_target)
          n_pages = reader.getNumberOfPages
          n_pages.times do |i|
            copier.addPage( copier.getImportedPage(reader, i+1)) if copier
          end
        end        
        document.close
              
        FileUtils.rm_rf(temp_dir, {:secure => true})        
      end
      
      def display
      end
      
      def save_and_display(filename)
        save(filename)
        display
      end
      
      include Gbdev::Utils::PrivateMethods
      
    end # End Filler

  end # End PDF

end # End GBDev

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
gbdev-pdf_filler-0.2.0 lib/pdf_filler/book.rb