spatialnetworks-pdf-stamper 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,15 @@
1
+ == 0.3.0 2009-02-08
2
+
3
+ * Refactored the code.
4
+ * Added JRuby support
5
+ * Fixed some documentation
6
+
7
+ == 0.2.0 2007-11-08
8
+
9
+ * Use bytearray for output
10
+ * Added support for images
11
+
12
+ == 0.1.0 2007-07-13
13
+
14
+ * 1 major enhancement:
15
+ * Initial release
@@ -0,0 +1,11 @@
1
+ History.txt
2
+ Manifest.txt
3
+ README.txt
4
+ Rakefile
5
+ ext/iText-2.1.4.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,52 @@
1
+ = pdf/stamper - PDF Templates, Wow!
2
+ http://github.com/jaywhy/pdf-stamper/
3
+ by Jason Yates
4
+
5
+ == DESCRIPTION:
6
+
7
+ Super cool PDF templates using iText's PdfStamper.
8
+
9
+ == CAVEAT:
10
+
11
+ Anything super cool must have a caveat. You have to use JRuby or RJB. Plus you
12
+ can only use Adobe LiveCycle Designer to create the templates.
13
+
14
+ == EXAMPLE:
15
+ pdf = PDF::Stamper.new("my_template.pdf")
16
+ pdf.text :first_name, "Jason"
17
+ pdf.text :last_name, "Yates"
18
+ pdf.image :photo, "photo.jpg"
19
+ pdf.save_as "my_output.pdf"
20
+
21
+ == INSTALL:
22
+
23
+ $ sudo gem install pdf-stamper
24
+
25
+ == CODE:
26
+
27
+ $ git clone http://github.com/jaywhy/pdf-stamper/
28
+
29
+ == LICENSE:
30
+
31
+ (The MIT License)
32
+
33
+ Copyright (c) 2007-2009 Jason Yates
34
+
35
+ Permission is hereby granted, free of charge, to any person obtaining
36
+ a copy of this software and associated documentation files (the
37
+ 'Software'), to deal in the Software without restriction, including
38
+ without limitation the rights to use, copy, modify, merge, publish,
39
+ distribute, sublicense, and/or sell copies of the Software, and to
40
+ permit persons to whom the Software is furnished to do so, subject to
41
+ the following conditions:
42
+
43
+ The above copyright notice and this permission notice shall be
44
+ included in all copies or substantial portions of the Software.
45
+
46
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
47
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
48
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
49
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
50
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
51
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
52
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,33 @@
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 newgem rubigen].each { |f| require f }
6
+
7
+ $hoe = Hoe.new('pdf-stamper', PDF::Stamper::VERSION) do |p|
8
+ p.name = 'pdf-stamper'
9
+ p.author = 'Jason Yates'
10
+ p.email = 'jaywhy@gmail.com'
11
+ p.summary = "Super cool PDF templates using iText's PdfStamper."
12
+ p.description = p.paragraphs_of('README.txt', 2..5).join("\n\n")
13
+ p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
14
+ p.extra_dev_deps = [
15
+ ['newgem', ">= #{::Newgem::VERSION}"]
16
+ ]
17
+
18
+ # p.spec_extras['platform'] = 'jruby' # JRuby gem created, e.g. pdf-stamper-X.Y.Z-jruby.gem
19
+
20
+ p.clean_globs |= %w[**/.DS_Store tmp *.log]
21
+ path = (p.rubyforge_name == p.name) ? p.rubyforge_name : "\#{p.rubyforge_name}/\#{p.name}"
22
+ p.remote_rdoc_dir = File.join(path.gsub(/^#{p.rubyforge_name}\/?/,''), 'rdoc')
23
+ p.rsync_args = '-av --delete --ignore-errors'
24
+ end
25
+
26
+ require 'newgem/tasks' # load /tasks/*.rake
27
+ Dir['tasks/**/*.rake'].each { |t| load t }
28
+
29
+ Spec::Rake::SpecTask.new do |t|
30
+ t.spec_files = FileList['spec/*_spec.rb']
31
+ t.warning = true
32
+ t.rcov = false
33
+ end
Binary file
@@ -0,0 +1,58 @@
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.0"
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 PDF's as templates. PDF::Stamper is not a PDF generator,
22
+ # it allows you to edit existing PDF's and use them as templates.
23
+ #
24
+ # == Creation of templates
25
+ #
26
+ # Templates currently can only be created using Adobe LiveCycle
27
+ # Designer which comes with the lastest versions of Adobe Acrobat
28
+ # Professional. Using LiveCycle Designer you can create a form and
29
+ # add textfield's for text and button's for images.
30
+ #
31
+ # == Example
32
+ #
33
+ # pdf = PDF::Stamper.new("my_template.pdf")
34
+ # pdf.text :first_name, "Jason"
35
+ # pdf.text :last_name, "Yates"
36
+ # pdf.image :photo, "photo.jpg"
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
+ # Saves the PDF into a file defined by path given.
45
+ def save_as(file)
46
+ File.open(file, "wb") { |f| f.write to_s }
47
+ end
48
+
49
+ private
50
+
51
+ def fill
52
+ @stamp.setFormFlattening(true)
53
+ @stamp.close
54
+ end
55
+ end
56
+ end
57
+
58
+
@@ -0,0 +1,58 @@
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
+ require 'iText-2.1.4.jar'
8
+
9
+ include_class 'java.io.FileOutputStream'
10
+ include_class 'java.io.ByteArrayOutputStream'
11
+ include_class 'com.lowagie.text.pdf.AcroFields'
12
+ include_class 'com.lowagie.text.pdf.PdfReader'
13
+ include_class 'com.lowagie.text.pdf.PdfStamper'
14
+ include_class 'com.lowagie.text.Image'
15
+ include_class 'com.lowagie.text.Rectangle'
16
+
17
+ module PDF
18
+ class Stamper
19
+ def initialize(pdf = nil)
20
+ template(pdf) if ! pdf.nil?
21
+ end
22
+
23
+ def template(template)
24
+ # NOTE I'd rather use a ByteArrayOutputStream. However I
25
+ # couldn't get it working. Patches welcome.
26
+ #@tmp_path = File.join(Dir::tmpdir, 'pdf-stamper-' + rand(10000).to_s + '.pdf')
27
+ reader = PdfReader.new(template)
28
+ @baos = ByteArrayOutputStream.new
29
+ @stamp = PdfStamper.new(reader, @baos)#FileOutputStream.new(@tmp_path))
30
+ @form = @stamp.getAcroFields()
31
+ end
32
+
33
+ # Set a button field defined by key and replaces with an image.
34
+ def image(key, image_path)
35
+ # Idea from here http://itext.ugent.be/library/question.php?id=31
36
+ # Thanks Bruno for letting me know about it.
37
+ img = Image.getInstance(image_path)
38
+ img_field = @form.getFieldPositions(key.to_s)
39
+
40
+ rect = Rectangle.new(img_field[1], img_field[2], img_field[3], img_field[4])
41
+ img.scaleToFit(rect.width, rect.height)
42
+ img.setAbsolutePosition(
43
+ img_field[1] + (rect.width - img.scaledWidth) / 2,
44
+ img_field[2] + (rect.height - img.scaledHeight) /2
45
+ )
46
+
47
+ cb = @stamp.getOverContent(img_field[0].to_i)
48
+ cb.addImage(img)
49
+ end
50
+
51
+ # Takes the PDF output and sends as a string. Basically it's sole
52
+ # purpose is to be used with send_data in rails.
53
+ def to_s
54
+ fill
55
+ String.from_java_bytes(@baos.toByteArray)
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,118 @@
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-2.1.4.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
+
33
+ template(pdf) if ! pdf.nil?
34
+ end
35
+
36
+ def template(template)
37
+ reader = @pdfreader.new(template)
38
+ @pagesize = reader.getPageSize(1)
39
+ @numpages = reader.getNumberOfPages()
40
+ @baos = @bytearray.new
41
+ @stamp = @pdfstamper.new(reader, @baos)
42
+ @form = @stamp.getAcroFields()
43
+ end
44
+
45
+ # Set a button field defined by key and replaces with an image.
46
+ def image(key, image_path)
47
+ # Idea from here http://itext.ugent.be/library/question.php?id=31
48
+ # Thanks Bruno for letting me know about it.
49
+ img = @image_class.getInstance(image_path)
50
+ img_field = @form.getFieldPositions(key.to_s)
51
+
52
+
53
+ rect = @rectangle.new(img_field[1], img_field[2], img_field[3], img_field[4])
54
+ img.scaleToFit(rect.width, rect.height)
55
+ img.setAbsolutePosition(
56
+ img_field[1] + (rect.width - img.getScaledWidth) / 2,
57
+ img_field[2] + (rect.height - img.getScaledHeight) /2
58
+ )
59
+
60
+ cb = @stamp.getOverContent(img_field[0].to_i)
61
+ cb.addImage(img)
62
+ end
63
+
64
+ # Takes the PDF output and sends as a string. Basically it's sole
65
+ # purpose is to be used with send_data in rails.
66
+ def to_s
67
+ fill
68
+ @baos.toByteArray
69
+ end
70
+
71
+ def add_images(images)
72
+ basefont = @basefont_class.createFont(@basefont_class.HELVETICA, @basefont_class.CP1252, @basefont_class.NOT_EMBEDDED)
73
+ image_size = []
74
+ image_size[0] = @pagesize.width() / 2
75
+ image_size[1] = (@pagesize.height() / 2) - 25
76
+ pages = (images.length / 4.0).ceil
77
+ pages.times do |index|
78
+ page_number = index + @numpages + 1
79
+ image_index = index * 4
80
+ @stamp.insertPage(page_number, @pagesize)
81
+ over = @stamp.getOverContent(page_number)
82
+ over.setFontAndSize(basefont, 12.0)
83
+ 4.times do |n|
84
+ if pdf_image = images[image_index + n]
85
+ if image_path = pdf_image[0]
86
+ img = @image_class.getInstance(image_path)
87
+ img.scaleToFit(image_size[0], (image_size[1] + 25))
88
+ case n
89
+ when 0
90
+ img.setAbsolutePosition(0, (image_size[1] + 25))
91
+ when 1
92
+ img.setAbsolutePosition(image_size[0], (image_size[1] + 25))
93
+ when 2
94
+ img.setAbsolutePosition(0, 25)
95
+ when 3
96
+ img.setAbsolutePosition(image_size[0], 25)
97
+ end
98
+ over.addImage(img)
99
+ end
100
+ if image_label = pdf_image[1]
101
+ over.beginText()
102
+ over.showTextAligned(@pdf_content_byte_class.ALIGN_CENTER, image_label, (img.getAbsoluteX() + (image_size[0] / 2)), (img.getAbsoluteY() - 20), 0)
103
+ over.endText()
104
+ end
105
+ end
106
+ end
107
+ end
108
+ end
109
+
110
+ def add_image_on_page(page, x, y, url)
111
+ over = @stamp.getOverContent(page)
112
+ img = @image_class.getInstance(url)
113
+ img.setAbsolutePosition(x,y)
114
+ img.scaleToFit(200,70)
115
+ over.addImage(img)
116
+ end
117
+ end
118
+ 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,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: spatialnetworks-pdf-stamper
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 3
8
+ - 0
9
+ version: 0.3.0
10
+ platform: ruby
11
+ authors:
12
+ - Joe Steele
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-08-19 00:00:00 -04:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: newgem
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 1
29
+ - 2
30
+ - 3
31
+ version: 1.2.3
32
+ type: :development
33
+ version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: hoe
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ segments:
42
+ - 1
43
+ - 8
44
+ - 0
45
+ version: 1.8.0
46
+ type: :development
47
+ version_requirements: *id002
48
+ description: "Super cool PDF templates using iText's PdfStamper. == CAVEAT: Anything super cool must have a caveat. You have to use JRuby or RJB. Plus you can only use Adobe LiveCycle Designer to create the templates. == EXAMPLE: pdf = PDF::Stamper.new(\"my_template.pdf\") pdf.text :first_name, \"Jason\" pdf.text :last_name, \"Yates\" pdf.image :photo, \"photo.jpg\" pdf.save_as \"my_output.pdf\""
49
+ email: joe@spatialnetworks.com
50
+ executables: []
51
+
52
+ extensions: []
53
+
54
+ extra_rdoc_files:
55
+ - History.txt
56
+ - Manifest.txt
57
+ - README.txt
58
+ files:
59
+ - History.txt
60
+ - Manifest.txt
61
+ - README.txt
62
+ - Rakefile
63
+ - ext/iText-2.1.4.jar
64
+ - lib/pdf/stamper.rb
65
+ - lib/pdf/stamper/jruby.rb
66
+ - lib/pdf/stamper/rjb.rb
67
+ - spec/logo.gif
68
+ - spec/pdf_stamper_spec.rb
69
+ - spec/test_template.pdf
70
+ has_rdoc: true
71
+ homepage: http://github.com/spatialnetworks/pdf-stamper/
72
+ licenses: []
73
+
74
+ post_install_message:
75
+ rdoc_options:
76
+ - --main
77
+ - README.txt
78
+ require_paths:
79
+ - lib
80
+ - ext
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ segments:
86
+ - 0
87
+ version: "0"
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ segments:
93
+ - 0
94
+ version: "0"
95
+ requirements: []
96
+
97
+ rubyforge_project: pdf-stamper
98
+ rubygems_version: 1.3.6
99
+ signing_key:
100
+ specification_version: 2
101
+ summary: Super cool PDF templates using iText's PdfStamper.
102
+ test_files: []
103
+