Class: GBDev::PDF::Book

Inherits:
Object
  • Object
show all
Includes:
Gbdev::Utils::PrivateMethods
Defined in:
lib/pdf_filler/book.rb

Overview

A book that represents collection of PDF page.

Instance Method Summary collapse

Methods included from Gbdev::Utils::PrivateMethods

#build_random_file_name, #build_random_string

Constructor Details

#initializeBook

Returns a new instance of Book.



8
9
10
# File 'lib/pdf_filler/book.rb', line 8

def initialize()
  @pages = []
end

Instance Method Details

#add_page(new_page) ⇒ Object

Add a page to the book

  • new_page - The page object of type GBDev::PDF::Page



15
16
17
# File 'lib/pdf_filler/book.rb', line 15

def add_page(new_page)
  @pages << new_page
end

#displayObject

Renders the PDF Book without saving it to disk and displays it in the browser.



48
49
# File 'lib/pdf_filler/book.rb', line 48

def display
end

#save_and_display(filename) ⇒ Object

Renders the PDF Book, saves it to the specified file and then displays the PDF in the browser.

  • filename - A path to the file to be created.



54
55
56
57
# File 'lib/pdf_filler/book.rb', line 54

def save_and_display(filename)
  save(filename)
  display
end

#save_to(filename) ⇒ Object

Renders the PDF Book and saves it to the specified file.

  • filename - A path to the file to be created.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/pdf_filler/book.rb', line 22

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