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,17 @@
1
+ == 0.3.0 2009-02-08
2
+
3
+ * Refactored the code.
4
+ * Added JRuby support
5
+ * Fixed some documentation
6
+ * Fixed issue with Leopard
7
+ * Upgraded iText version
8
+
9
+ == 0.2.0 2007-11-08
10
+
11
+ * Use bytearray for output
12
+ * Added support for images
13
+
14
+ == 0.1.0 2007-07-13
15
+
16
+ * 1 major enhancement:
17
+ * 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,59 @@
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
+ f = File.new(file, "w")
47
+ f.syswrite to_s
48
+ end
49
+
50
+ private
51
+
52
+ def fill
53
+ @stamp.setFormFlattening(true)
54
+ @stamp.close
55
+ end
56
+ end
57
+ end
58
+
59
+
@@ -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,65 @@
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
+
28
+ template(pdf) if ! pdf.nil?
29
+ end
30
+
31
+ def template(template)
32
+ reader = @pdfreader.new(template)
33
+ @baos = @bytearray.new
34
+ @stamp = @pdfstamper.new(reader, @baos)
35
+ @form = @stamp.getAcroFields()
36
+ end
37
+
38
+ # Set a button field defined by key and replaces with an image.
39
+ def image(key, image_path)
40
+ # Idea from here http://itext.ugent.be/library/question.php?id=31
41
+ # Thanks Bruno for letting me know about it.
42
+ image = Rjb::import('com.lowagie.text.Image')
43
+ img = image.getInstance(image_path)
44
+ img_field = @form.getFieldPositions(key.to_s)
45
+
46
+ rectangle = Rjb::import('com.lowagie.text.Rectangle')
47
+ rect = rectangle.new(img_field[1], img_field[2], img_field[3], img_field[4])
48
+ img.scaleToFit(rect.width, rect.height)
49
+ img.setAbsolutePosition(
50
+ img_field[1] + (rect.width - img.getScaledWidth) / 2,
51
+ img_field[2] + (rect.height - img.getScaledHeight) /2
52
+ )
53
+
54
+ cb = @stamp.getOverContent(img_field[0].to_i)
55
+ cb.addImage(img)
56
+ end
57
+
58
+ # Takes the PDF output and sends as a string. Basically it's sole
59
+ # purpose is to be used with send_data in rails.
60
+ def to_s
61
+ fill
62
+ @baos.toByteArray
63
+ end
64
+ end
65
+ 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,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pdf-stamper
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.0
5
+ platform: ruby
6
+ authors:
7
+ - Jason Yates
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-02-08 00:00:00 -05:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: newgem
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.2.3
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: hoe
27
+ type: :development
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 1.8.0
34
+ version:
35
+ 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\""
36
+ email: jaywhy@gmail.com
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - History.txt
43
+ - Manifest.txt
44
+ - README.txt
45
+ files:
46
+ - History.txt
47
+ - Manifest.txt
48
+ - README.txt
49
+ - Rakefile
50
+ - ext/iText-2.1.4.jar
51
+ - lib/pdf/stamper.rb
52
+ - lib/pdf/stamper/jruby.rb
53
+ - lib/pdf/stamper/rjb.rb
54
+ - spec/logo.gif
55
+ - spec/pdf_stamper_spec.rb
56
+ - spec/test_template.pdf
57
+ has_rdoc: true
58
+ homepage: " http://github.com/jaywhy/pdf-stamper/"
59
+ post_install_message:
60
+ rdoc_options:
61
+ - --main
62
+ - README.txt
63
+ require_paths:
64
+ - lib
65
+ - ext
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: "0"
71
+ version:
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: "0"
77
+ version:
78
+ requirements: []
79
+
80
+ rubyforge_project: pdf-stamper
81
+ rubygems_version: 1.3.1
82
+ signing_key:
83
+ specification_version: 2
84
+ summary: Super cool PDF templates using iText's PdfStamper.
85
+ test_files: []
86
+