gbdev-pdf_filler 0.3.2 → 0.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5,17 +5,19 @@ load_path = File.expand_path(File.dirname(__FILE__) + '/iText-2.1.7.jar')
5
5
  options = ['-Djava.awt.headless=true']
6
6
  Rjb::load load_path, options
7
7
 
8
- FileOutputStream = Rjb::import('java.io.FileOutputStream')
9
- PdfWriter = Rjb::import('com.lowagie.text.pdf.PdfWriter')
10
- PdfReader = Rjb::import('com.lowagie.text.pdf.PdfReader')
11
- PdfCopy = Rjb::import('com.lowagie.text.pdf.PdfCopy')
12
- PdfImportedPage = Rjb::import('com.lowagie.text.pdf.PdfImportedPage')
13
- Document = Rjb::import('com.lowagie.text.Document')
14
- Paragraph = Rjb::import('com.lowagie.text.Paragraph')
15
- AcroFields = Rjb::import('com.lowagie.text.pdf.AcroFields')
16
- PdfStamper = Rjb::import('com.lowagie.text.pdf.PdfStamper')
17
- HashMap = Rjb::import('java.util.HashMap')
18
- Iterator = Rjb::import('java.util.Iterator')
8
+ FileOutputStream = Rjb::import('java.io.FileOutputStream')
9
+ ByteArrayOutputStream = Rjb::import('java.io.ByteArrayOutputStream')
10
+ ByteArrayInputStream = Rjb::import('java.io.ByteArrayInputStream')
11
+ PdfWriter = Rjb::import('com.lowagie.text.pdf.PdfWriter')
12
+ PdfReader = Rjb::import('com.lowagie.text.pdf.PdfReader')
13
+ PdfCopy = Rjb::import('com.lowagie.text.pdf.PdfCopy')
14
+ PdfImportedPage = Rjb::import('com.lowagie.text.pdf.PdfImportedPage')
15
+ Document = Rjb::import('com.lowagie.text.Document')
16
+ Paragraph = Rjb::import('com.lowagie.text.Paragraph')
17
+ AcroFields = Rjb::import('com.lowagie.text.pdf.AcroFields')
18
+ PdfStamper = Rjb::import('com.lowagie.text.pdf.PdfStamper')
19
+ HashMap = Rjb::import('java.util.HashMap')
20
+ Iterator = Rjb::import('java.util.Iterator')
19
21
 
20
22
  require 'pdf_filler/util_methods'
21
23
  require 'pdf_filler/page'
@@ -42,21 +42,50 @@ module GBDev
42
42
  document.close
43
43
 
44
44
  FileUtils.rm_rf(temp_dir, {:secure => true})
45
- end
45
+ end
46
46
 
47
- # Renders the PDF Book without saving it to disk and displays it in the browser.
48
- def display
49
- end
47
+ # Renders the template with the given data and returns that buffer.
48
+ def to_buffer
49
+ dir = defined?(RAILS_ROOT) ? [RAILS_ROOT, 'tmp'].join('/') : File.expand_path(File.dirname(__FILE__) + '/../../spec/output')
50
+ temp_dir = [dir, "collection_temp_#{build_random_string}"].join('/')
51
+ Dir.mkdir(temp_dir)
52
+ @pages.each_with_index do |page, indx|
53
+ page.save_to([temp_dir, "#{indx}_#{build_random_file_name}"].join('/'))
54
+ end
55
+ temp_files = Dir[[temp_dir,'*'].join('/')].sort
56
+
57
+ document = Document.new
58
+ baos = ByteArrayOutputStream.new
59
+ copier = PdfCopy.new(document, baos)
50
60
 
51
- # Renders the PDF Book, saves it to the specified file and then displays the PDF in the browser.
52
- #
53
- # * filename - A path to the file to be created.
54
- def save_and_display(filename)
55
- save(filename)
56
- display
57
- end
61
+ document.open
62
+
63
+ # This did not work but try working on it again later.
64
+ # @pages.each do |page|
65
+ # bais = ByteArrayInputStream.new_with_sig(page.to_buffer)
66
+ # reader = PdfReader.new(bais)
67
+ # reader = page.get_pdf_reader
68
+ # n_pages = reader.getNumberOfPages
69
+ # n_pages.times do |i|
70
+ # copier.addPage( copier.getImportedPage(reader, i+1)) if copier
71
+ # end
72
+ # end
73
+
74
+ temp_files.each do |read_target|
75
+ reader = PdfReader.new(read_target)
76
+ n_pages = reader.getNumberOfPages
77
+ n_pages.times do |i|
78
+ copier.addPage( copier.getImportedPage(reader, i+1)) if copier
79
+ end
80
+ end
81
+ document.close
82
+
83
+ #FileUtils.rm_rf(temp_dir, {:secure => true})
84
+
85
+ return baos.toByteArray()
86
+ end
58
87
 
59
- include Gbdev::Utils::PrivateMethods
88
+ include GBDev::Utils::PrivateMethods
60
89
 
61
90
  end # End Filler
62
91
 
@@ -26,6 +26,8 @@ module GBDev
26
26
  @radio_buttons = {}
27
27
  @signature_fields = {}
28
28
  @text_fields = {}
29
+
30
+ @pdf_reader = nil
29
31
  end
30
32
 
31
33
  # Returns a hash where the keys are the field names in the template PDF and the values are the field types.
@@ -206,19 +208,36 @@ module GBDev
206
208
  stamper.close
207
209
  end
208
210
 
209
- # Renders the PDF Book without saving it to disk and displays it in the browser.
210
- def display
211
- end
212
211
 
213
- # Renders the PDF Page, saves it to the specified file and then displays the PDF in the browser.
214
- #
215
- # * filename - A path to the file to be created.
216
- def save_and_display(filename)
217
- save(filename)
218
- display
212
+ # Renders the template with the given data and returns that buffer.
213
+ def to_buffer
214
+ field_cache = HashMap.new
215
+ reader = PdfReader.new(@template)
216
+ baos = ByteArrayOutputStream.new
217
+ stamper = PdfStamper.new( reader, baos )
218
+ form = stamper.getAcroFields()
219
+ form.setFieldCache(field_cache)
220
+
221
+ all_fields = {}
222
+ all_fields.merge!(@check_boxes)
223
+ all_fields.merge!(@radio_buttons)
224
+ all_fields.merge!(@signature_fields)
225
+ all_fields.merge!(@text_fields)
226
+
227
+ all_fields.each do |field, value|
228
+ form.setField(field.to_s, value.to_s)
229
+ end
230
+
231
+ # TODO: do something with @images
232
+
233
+ stamper.setFormFlattening(true)
234
+ stamper.close
235
+
236
+ return baos.toByteArray()
219
237
  end
238
+
220
239
 
221
- include Gbdev::Utils::PrivateMethods
240
+ include GBDev::Utils::PrivateMethods
222
241
 
223
242
  end # End Page
224
243
 
@@ -1,4 +1,4 @@
1
- module Gbdev #:nodoc: all
1
+ module GBDev #:nodoc: all
2
2
 
3
3
  module Utils
4
4
 
@@ -14,5 +14,37 @@ module Gbdev #:nodoc: all
14
14
  end
15
15
 
16
16
  end
17
+
18
+
19
+ # Include these controller methods to prompt the user to download the PDF.
20
+ #
21
+ # * include GBDev::Utils::ControllerMethods
22
+ module ControllerMethods
23
+
24
+ # Used to display the PDF without saving it to disk.
25
+ #
26
+ # * buffer - A Page buffer or Book buffer.
27
+ # * filename - The filename to apply to the buffer when prompted to download.
28
+ def display_pdf(buffer, filename)
29
+ send_data(buffer, {:type => 'application/pdf',
30
+ :disposition => 'attachment',
31
+ :filename => filename} )
32
+ end
33
+
34
+
35
+ # From here: http://railspdfplugin.rubyforge.org/wiki/wiki.pl
36
+ # Suggested code so errors will always show in browser
37
+ def rescue_action_in_public(exception) # :nodoc:
38
+ headers.delete("Content-Disposition")
39
+ super
40
+ end
41
+
42
+ def rescue_action_locally(exception) # :nodoc:
43
+ headers.delete("Content-Disposition")
44
+ super
45
+ end
46
+
47
+ end
48
+
17
49
  end
18
50
  end
@@ -50,4 +50,25 @@ describe 'Book' do
50
50
  File.exist?(pdf_book).should be_true
51
51
  end
52
52
 
53
+ it 'should return the buffer without writing to disk' do
54
+ book = PDFBook()
55
+
56
+ template_file = File.expand_path(File.dirname(__FILE__) + '/../templates/certificate_template.pdf')
57
+
58
+ page1 = GBDev::PDF::Page.new(template_file)
59
+ page1.set_text_field(:full_name, 'Wes Hays')
60
+
61
+ page2 = GBDev::PDF::Page.new(template_file)
62
+ page2.set_text_field(:full_name, 'Darren Johnson')
63
+
64
+ page3 = GBDev::PDF::Page.new(template_file)
65
+ page3.set_text_field(:full_name, 'John Dell')
66
+
67
+ book.add_page(page1)
68
+ book.add_page(page2)
69
+ book.add_page(page3)
70
+
71
+ book.to_buffer.should_not be_blank
72
+ end
73
+
53
74
  end
@@ -80,4 +80,10 @@ describe 'Page' do
80
80
  File.exist?(File.dirname(__FILE__) + '/../output/form_test.pdf').should be_true
81
81
  end
82
82
 
83
+ it 'should return the buffer without writing to disk' do
84
+ page = PDFPage(File.expand_path(File.dirname(__FILE__) + '/../templates/certificate_template.pdf'))
85
+ page.set_text_field(:full_name, 'Wes Hays')
86
+ pdf_data = page.to_buffer
87
+ pdf_data.should_not be_blank
88
+ end
83
89
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gbdev-pdf_filler
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 0.3.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Wes Hays
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2009-08-04 00:00:00 -07:00
13
+ date: 2009-08-08 00:00:00 -07:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency