Class: GBDev::PDF::Book

Inherits:
Object
  • Object
show all
Includes:
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 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

#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

#to_bufferObject

Renders the template with the given data and returns that buffer.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/pdf_filler/book.rb', line 48

def to_buffer
  dir = defined?(RAILS_ROOT) ? [RAILS_ROOT, 'tmp'].join('/') : File.expand_path(File.dirname(__FILE__) + '/../../spec/output')         
  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        
  baos = ByteArrayOutputStream.new
  copier = PdfCopy.new(document, baos)

  document.open        
  
  # This did not work but try working on it again later.
  # @pages.each do |page|
  #   bais = ByteArrayInputStream.new_with_sig(page.to_buffer)
  #   reader = PdfReader.new(bais)
  #   reader = page.get_pdf_reader
  #   n_pages = reader.getNumberOfPages
  #   n_pages.times do |i|
  #     copier.addPage( copier.getImportedPage(reader, i+1)) if copier
  #   end
  # end        
  
  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})       
        
  return baos.toByteArray()
end