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