tv-pdf-stamper 0.3.7

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.
@@ -0,0 +1,36 @@
1
+ == 0.3.6 2012-10-25
2
+ * use java_import instead of include_class (include_class deprecated in jruby 1.7.0)
3
+
4
+ == 0.3.5 2012-06-27
5
+ * Checkboxes now work in JRuby
6
+
7
+ == 0.3.4 2010-09-24
8
+ * Circle/ellipse/rectangle now take a page parameter, and can be applied to a specific page.
9
+ This is useful for multi-page PDFs.
10
+
11
+ == 0.3.3 2010-09-24
12
+ * iText downgraded to 4.2.0 due to license incompatibility
13
+
14
+ == 0.3.2 2010-09-24
15
+ * Added support for drawing circles, rectangles and ellipses
16
+
17
+ == 0.3.1 2010-09-15
18
+ * checkbox support added
19
+ * iText updated to 5.0.4
20
+ * documentation updated and typos fixed
21
+
22
+ == 0.3.0 2009-02-08
23
+
24
+ * Refactored the code.
25
+ * Added JRuby support
26
+ * Fixed some documentation
27
+
28
+ == 0.2.0 2007-11-08
29
+
30
+ * Use bytearray for output
31
+ * Added support for images
32
+
33
+ == 0.1.0 2007-07-13
34
+
35
+ * 1 major enhancement:
36
+ * Initial release
@@ -0,0 +1,11 @@
1
+ History.txt
2
+ Manifest.txt
3
+ README.txt
4
+ Rakefile
5
+ ext/iText-4.2.0.jar
6
+ lib/pdf/stamper.rb
7
+ lib/pdf/stamper/jruby.rb
8
+ lib/pdf/stamper/rjb.rb
9
+ spec/logo.gif
10
+ spec/pdf_stamper_spec.rb
11
+ spec/test_template.pdf
@@ -0,0 +1,62 @@
1
+ = pdf/stamper - PDF Templates
2
+ http://github.com/jaywhy/pdf-stamper/
3
+ by Jason Yates
4
+
5
+ 15 September 2010 -- fork by Paul Schreiber
6
+ http://github.com/paulschreiber/pdf-stamper/
7
+
8
+ 21 January 2013 -- fork by Wes Morgan (TurboVote)
9
+ http://github.com/turbovote/pdf-stamper
10
+
11
+ == DESCRIPTION:
12
+
13
+ Fill out PDF forms (templates) using iText's PdfStamper.
14
+
15
+ == CAVEAT:
16
+
17
+ You have to use JRuby or RJB. You need Adobe LiveCycle Designer
18
+ or Acrobat Professional to create the templates.
19
+
20
+ == EXAMPLE:
21
+ pdf = PDF::Stamper.new("my_template.pdf")
22
+ pdf.text :first_name, "Jason"
23
+ pdf.text :last_name, "Yates"
24
+ pdf.image :photo, "photo.jpg"
25
+ pdf.checkbox :hungry
26
+ pdf.ellipse(140, 380, 50, 13)
27
+ pdf.rectangle(140, 380, 50, 13, 2)
28
+ pdf.circle(140, 380)
29
+ pdf.save_as "my_output.pdf"
30
+
31
+ == INSTALL:
32
+
33
+ $ sudo gem install tv-pdf-stamper
34
+
35
+ == CODE:
36
+
37
+ $ git clone http://github.com/turbovote/pdf-stamper/
38
+
39
+ == LICENSE:
40
+
41
+ (The MIT License)
42
+
43
+ Copyright 2007-2013 Jason Yates, Paul Schreiber, Wes Morgan
44
+
45
+ Permission is hereby granted, free of charge, to any person obtaining
46
+ a copy of this software and associated documentation files (the
47
+ 'Software'), to deal in the Software without restriction, including
48
+ without limitation the rights to use, copy, modify, merge, publish,
49
+ distribute, sublicense, and/or sell copies of the Software, and to
50
+ permit persons to whom the Software is furnished to do so, subject to
51
+ the following conditions:
52
+
53
+ The above copyright notice and this permission notice shall be
54
+ included in all copies or substantial portions of the Software.
55
+
56
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
57
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
58
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
59
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
60
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
61
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
62
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,13 @@
1
+ $:.unshift File.join(File.dirname(__FILE__), "lib")
2
+ require 'pdf/stamper'
3
+ require 'spec/rake/spectask'
4
+
5
+ %w[rubygems rake rake/clean fileutils rubigen].each { |f| require f }
6
+
7
+ Dir['tasks/**/*.rake'].each { |t| load t }
8
+
9
+ Spec::Rake::SpecTask.new do |t|
10
+ t.spec_files = FileList['spec/*_spec.rb']
11
+ t.warning = true
12
+ t.rcov = false
13
+ end
Binary file
@@ -0,0 +1,99 @@
1
+ # = pdf/stamper.rb -- PDF template stamping.
2
+ #
3
+ # Copyright (c) 2007-2009 Jason Yates
4
+
5
+ require 'rbconfig'
6
+ require 'fileutils'
7
+ require 'tmpdir'
8
+
9
+ include FileUtils
10
+
11
+ module PDF
12
+ class Stamper
13
+ VERSION = "0.3.6"
14
+
15
+ if RUBY_PLATFORM =~ /java/ # ifdef to check if your using JRuby
16
+ require 'pdf/stamper/jruby'
17
+ else
18
+ require 'pdf/stamper/rjb'
19
+ end
20
+ # PDF::Stamper provides an interface into iText's PdfStamper allowing for the
21
+ # editing of existing PDFs as templates. PDF::Stamper is not a PDF generator,
22
+ # it allows you to edit existing PDFs and use them as templates.
23
+ #
24
+ # == Creation of templates
25
+ #
26
+ # Templates currently can be created using Adobe LiveCycle Designer
27
+ # or Adobe Acrobat Professional. Using Acrobat Professional, you can create
28
+ # a form and add textfields, checkboxes, radio buttons and buttons for images.
29
+ #
30
+ # == Example
31
+ #
32
+ # pdf = PDF::Stamper.new("my_template.pdf")
33
+ # pdf.text :first_name, "Jason"
34
+ # pdf.text :last_name, "Yates"
35
+ # pdf.image :photo, "photo.jpg"
36
+ # pdf.checkbox :hungry
37
+ # pdf.save_as "my_output"
38
+
39
+ # Set a textfield defined by key and text to value.
40
+ def text(key, value)
41
+ @form.setField(key.to_s, value.to_s) # Value must be a string or itext will error.
42
+ end
43
+
44
+ # Set a checkbox to checked
45
+ def checkbox(key)
46
+ field_type = @form.getFieldType(key.to_s)
47
+ return unless is_checkbox(field_type)
48
+
49
+ all_states = @form.getAppearanceStates(key.to_s)
50
+ yes_state = all_states.reject{|x| x == "Off"}
51
+
52
+
53
+ @form.setField(key.to_s, yes_state.first) unless (yes_state.size == 0)
54
+ end
55
+
56
+ # Get checkbox values
57
+ def get_checkbox_values(key)
58
+ field_type = @form.getFieldType(key.to_s)
59
+ return unless is_checkbox(field_type)
60
+
61
+ @form.getAppearanceStates(key.to_s)
62
+ end
63
+
64
+ def circle(x, y, r, page=1)
65
+ update_canvas_list(page)
66
+ @canvas_list[page].circle(x, y, r)
67
+ end
68
+
69
+ def ellipse(x, y, width, height, page=1)
70
+ update_canvas_list(page)
71
+ @canvas_list[page].ellipse(x, y, x + width, y + height)
72
+ end
73
+
74
+ def rectangle(x, y, width, height, page=1)
75
+ update_canvas_list(page)
76
+ @canvas_list[page].rectangle(x, y, width, height)
77
+ end
78
+
79
+ # Saves the PDF into a file defined by path given.
80
+ def save_as(file)
81
+ File.open(file, "wb") { |f| f.write to_s }
82
+ end
83
+
84
+ private
85
+
86
+ def update_canvas_list(page)
87
+ @canvas_list[page] = @stamp.getOverContent(page) unless @canvas_list.has_key?(page)
88
+ end
89
+
90
+ def fill
91
+ @canvas_list.values.each do |c|
92
+ c.stroke
93
+ end
94
+ @stamp.setFormFlattening(true)
95
+ @stamp.close
96
+ end
97
+ end
98
+ end
99
+
@@ -0,0 +1,68 @@
1
+ # = pdf/stamper/rjb.rb -- PDF template stamping.
2
+ #
3
+ # Copyright (c) 2007-2009 Jason Yates
4
+
5
+ $:.unshift(File.join(File.dirname(__FILE__), '..', '..', '..', 'ext'))
6
+ require 'java'
7
+
8
+ java.lang.System.setProperty('java.awt.headless', 'true')
9
+
10
+ require 'iText-4.2.0.jar'
11
+
12
+ java_import 'java.io.FileOutputStream'
13
+ java_import 'java.io.ByteArrayOutputStream'
14
+ java_import 'com.lowagie.text.pdf.AcroFields'
15
+ java_import 'com.lowagie.text.pdf.PdfReader'
16
+ java_import 'com.lowagie.text.pdf.PdfStamper'
17
+ java_import 'com.lowagie.text.Image'
18
+ java_import 'com.lowagie.text.Rectangle'
19
+ java_import 'com.lowagie.text.pdf.GrayColor'
20
+
21
+ module PDF
22
+ class Stamper
23
+ def initialize(pdf = nil)
24
+ template(pdf) if ! pdf.nil?
25
+ end
26
+
27
+ def template(template)
28
+ # NOTE I'd rather use a ByteArrayOutputStream. However I
29
+ # couldn't get it working. Patches welcome.
30
+ #@tmp_path = File.join(Dir::tmpdir, 'pdf-stamper-' + rand(10000).to_s + '.pdf')
31
+ reader = PdfReader.new(template)
32
+ @baos = ByteArrayOutputStream.new
33
+ @stamp = PdfStamper.new(reader, @baos)#FileOutputStream.new(@tmp_path))
34
+ @form = @stamp.getAcroFields()
35
+ @black = GrayColor.new(0.0)
36
+ @canvas_list = { 1 => @stamp.getOverContent(1) }
37
+ end
38
+
39
+ def is_checkbox(field_type)
40
+ field_type == AcroFields::FIELD_TYPE_CHECKBOX
41
+ end
42
+
43
+ # Set a button field defined by key and replaces with an image.
44
+ def image(key, image_path)
45
+ # Idea from here http://itext.ugent.be/library/question.php?id=31
46
+ # Thanks Bruno for letting me know about it.
47
+ img = Image.getInstance(image_path)
48
+ img_field = @form.getFieldPositions(key.to_s)
49
+
50
+ rect = Rectangle.new(img_field[1], img_field[2], img_field[3], img_field[4])
51
+ img.scaleToFit(rect.width, rect.height)
52
+ img.setAbsolutePosition(
53
+ img_field[1] + (rect.width - img.scaledWidth) / 2,
54
+ img_field[2] + (rect.height - img.scaledHeight) /2
55
+ )
56
+
57
+ cb = @stamp.getOverContent(img_field[0].to_i)
58
+ cb.addImage(img)
59
+ end
60
+
61
+ # Takes the PDF output and sends as a string. Basically it's sole
62
+ # purpose is to be used with send_data in rails.
63
+ def to_s
64
+ fill
65
+ String.from_java_bytes(@baos.toByteArray)
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,130 @@
1
+ # = pdf/stamper/rjb.rb -- PDF template stamping.
2
+ #
3
+ # Copyright (c) 2007-2009 Jason Yates
4
+
5
+ require 'rubygems'
6
+ require 'rjb'
7
+
8
+ Rjb::load(File.join(File.dirname(__FILE__), '..', '..', '..', 'ext', 'iText-4.2.0.jar'), ['-Djava.awt.headless=true'])
9
+
10
+ module PDF
11
+ # PDF::Stamper::RJB
12
+ #
13
+ # RJB needs the LD_LIBRARY_PATH and JAVA_HOME environment set for it
14
+ # to work correctly. For example on my system:
15
+ #
16
+ # export LD_LIBRARY_PATH=/usr/java/jdk1.6.0/jre/lib/i386/:/usr/java/jdk1.6.0/jre/lib/i386/client/:./
17
+ # export JAVA_HOME=/usr/java/jdk1.6.0/
18
+ #
19
+ # Check the RJB documentation if you are having issues with this.
20
+ class Stamper
21
+ def initialize(pdf = nil, options = {})
22
+ @bytearray = Rjb::import('java.io.ByteArrayOutputStream')
23
+ @filestream = Rjb::import('java.io.FileOutputStream')
24
+ @acrofields = Rjb::import('com.lowagie.text.pdf.AcroFields')
25
+ @pdfreader = Rjb::import('com.lowagie.text.pdf.PdfReader')
26
+ @pdfstamper = Rjb::import('com.lowagie.text.pdf.PdfStamper')
27
+ @pdfwriter = Rjb::import('com.lowagie.text.pdf.PdfWriter')
28
+ @image_class = Rjb::import('com.lowagie.text.Image')
29
+ @pdf_content_byte_class = Rjb::import('com.lowagie.text.pdf.PdfContentByte')
30
+ @basefont_class = Rjb::import('com.lowagie.text.pdf.BaseFont')
31
+ @rectangle = Rjb::import('com.lowagie.text.Rectangle')
32
+ @gray_color = Rjb::import('com.lowagie.text.pdf.GrayColor')
33
+
34
+ template(pdf) if ! pdf.nil?
35
+ end
36
+
37
+ def is_checkbox(field_type)
38
+ field_type == @acrofields.FIELD_TYPE_CHECKBOX
39
+ end
40
+
41
+ def template(template)
42
+ reader = @pdfreader.new(template)
43
+ @pagesize = reader.getPageSize(1)
44
+ @numpages = reader.getNumberOfPages()
45
+ @baos = @bytearray.new
46
+ @stamp = @pdfstamper.new(reader, @baos)
47
+ @form = @stamp.getAcroFields()
48
+ @black = @gray_color.new(0.0)
49
+ @canvas_list = { 1 => @stamp.getOverContent(1) }
50
+ end
51
+
52
+ # Set a button field defined by key and replaces with an image.
53
+ def image(key, image_path)
54
+ # Idea from here http://itext.ugent.be/library/question.php?id=31
55
+ # Thanks Bruno for letting me know about it.
56
+ img = @image_class.getInstance(image_path)
57
+ img_field = @form.getFieldPositions(key.to_s)
58
+
59
+
60
+ rect = @rectangle.new(img_field[1], img_field[2], img_field[3], img_field[4])
61
+ img.scaleToFit(rect.width, rect.height)
62
+ img.setAbsolutePosition(
63
+ img_field[1] + (rect.width - img.getScaledWidth) / 2,
64
+ img_field[2] + (rect.height - img.getScaledHeight) /2
65
+ )
66
+
67
+ cb = @stamp.getOverContent(img_field[0].to_i)
68
+ cb.addImage(img)
69
+ end
70
+
71
+ # Takes the PDF output and sends as a string. Basically its sole
72
+ # purpose is to be used with send_data in rails.
73
+ def to_s
74
+ fill
75
+ @baos.toByteArray
76
+ end
77
+
78
+ def add_images(images)
79
+ basefont = @basefont_class.createFont(@basefont_class.HELVETICA, @basefont_class.CP1252, @basefont_class.NOT_EMBEDDED)
80
+ image_size = []
81
+ half_page_width = @pagesize.width() / 2
82
+ half_page_height = @pagesize.height() / 2
83
+ image_size[0] = half_page_width - 80
84
+ image_size[1] = half_page_height - 80
85
+ pages = (images.length / 4.0).ceil
86
+ pages.times do |index|
87
+ page_number = index + @numpages + 1
88
+ image_index = index * 4
89
+ @stamp.insertPage(page_number, @pagesize)
90
+ over = @stamp.getOverContent(page_number)
91
+ over.setFontAndSize(basefont, 12.0)
92
+ 4.times do |n|
93
+ if pdf_image = images[image_index + n]
94
+ if image_path = pdf_image[0]
95
+ img = @image_class.getInstance(image_path)
96
+ img.scaleToFit(image_size[0] + 30, (image_size[1]))
97
+ img_x_offset = (half_page_width - image_size[0]) / 2
98
+ img_y_offset = (half_page_height - img.getScaledHeight()) / 2
99
+ case n
100
+ when 0
101
+ img.setAbsolutePosition(img_x_offset, (half_page_height + img_y_offset))
102
+ when 1
103
+ img.setAbsolutePosition((half_page_width + (img_x_offset - 30)), (half_page_height + img_y_offset))
104
+ when 2
105
+ img.setAbsolutePosition(img_x_offset, img_y_offset)
106
+ when 3
107
+ img.setAbsolutePosition((half_page_width + (img_x_offset - 30)), img_y_offset)
108
+ end
109
+ over.addImage(img)
110
+ end
111
+ if image_label = pdf_image[1]
112
+ over.beginText()
113
+ over.showTextAligned(@pdf_content_byte_class.ALIGN_CENTER, image_label, (img.getAbsoluteX() + ((image_size[0] + 30) / 2)), (img.getAbsoluteY() - 15), 0)
114
+ over.endText()
115
+ end
116
+ end
117
+ end
118
+ end
119
+ @stamp.setFullCompression()
120
+ end
121
+
122
+ def add_image_on_page(page, x, y, url)
123
+ over = @stamp.getOverContent(page)
124
+ img = @image_class.getInstance(url)
125
+ img.setAbsolutePosition(x,y)
126
+ img.scaleToFit(200,70)
127
+ over.addImage(img)
128
+ end
129
+ end
130
+ end
Binary file
@@ -0,0 +1,21 @@
1
+ $:.unshift File.join(File.dirname(__FILE__), "..", "lib")
2
+ require 'pdf/stamper'
3
+
4
+ describe PDF::Stamper do
5
+ before(:each) do
6
+ @pdf = PDF::Stamper.new(File.join(File.dirname(__FILE__), "test_template.pdf"))
7
+ @pdf.text :text_field01, "test"
8
+ @pdf.text :text_field02, "test2"
9
+ @pdf.image :button_field01, File.join(File.dirname(__FILE__), "logo.gif")
10
+ end
11
+
12
+ it "should create PDF document" do
13
+ @pdf.to_s.should_not be_nil
14
+ end
15
+
16
+ it "should save PDF document" do
17
+ @pdf.save_as "test_output.pdf"
18
+ File.exist?("test_output.pdf").should be_true
19
+ File.delete("test_output.pdf") # Comment this out to view the output
20
+ end
21
+ end
Binary file
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tv-pdf-stamper
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.7
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Paul Schreiber
9
+ - Wes Morgan
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2013-01-21 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: newgem
17
+ requirement: !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: 1.2.3
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ! '>='
29
+ - !ruby/object:Gem::Version
30
+ version: 1.2.3
31
+ - !ruby/object:Gem::Dependency
32
+ name: hoe
33
+ requirement: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: 1.8.0
39
+ type: :development
40
+ prerelease: false
41
+ version_requirements: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: 1.8.0
47
+ description: ! 'Fill out PDF forms (templates) using iText''s PdfStamper. == CAVEAT: You
48
+ have to use JRuby or RJB. You need Adobe LiveCycle Designer or Acrobat Professional
49
+ to create the templates. == EXAMPLE: pdf = PDF::Stamper.new("my_template.pdf")
50
+ pdf.text :first_name, "Jason" pdf.text :last_name, "Yates" pdf.image :photo, "photo.jpg"
51
+ pdf.checkbox :hungry pdf.save_as "my_output.pdf"'
52
+ email:
53
+ - paulschreiber@gmail.com
54
+ - wes@turbovote.org
55
+ executables: []
56
+ extensions: []
57
+ extra_rdoc_files:
58
+ - History.txt
59
+ - Manifest.txt
60
+ - README.txt
61
+ files:
62
+ - History.txt
63
+ - Manifest.txt
64
+ - README.txt
65
+ - Rakefile
66
+ - ext/iText-4.2.0.jar
67
+ - lib/pdf/stamper.rb
68
+ - lib/pdf/stamper/jruby.rb
69
+ - lib/pdf/stamper/rjb.rb
70
+ - spec/logo.gif
71
+ - spec/pdf_stamper_spec.rb
72
+ - spec/test_template.pdf
73
+ homepage: http://github.com/turbovote/pdf-stamper/
74
+ licenses: []
75
+ post_install_message:
76
+ rdoc_options:
77
+ - --main
78
+ - README.txt
79
+ require_paths:
80
+ - lib
81
+ - ext
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ! '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubyforge_project: pdf-stamper
96
+ rubygems_version: 1.8.23
97
+ signing_key:
98
+ specification_version: 2
99
+ summary: PDF templates using iText's PdfStamper.
100
+ test_files: []