Skip to content

Commit 8afed6d

Browse files
committed
work in progress for certs support
1 parent de433e6 commit 8afed6d

File tree

7 files changed

+80
-27
lines changed

7 files changed

+80
-27
lines changed

Gemfile

+1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ group :development do
77
gem "rdoc"
88
gem "bundler"
99
gem "jeweler"
10+
gem "pry"
1011
end

Gemfile.lock

+17
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@ GEM
88
thread_safe (~> 0.1)
99
tzinfo (~> 0.3.37)
1010
addressable (2.3.5)
11+
atomic (1.1.13)
1112
atomic (1.1.13-java)
1213
builder (3.2.2)
14+
coderay (1.0.9)
1315
faraday (0.8.8)
1416
multipart-post (~> 1.2.0)
17+
ffi (1.9.0-java)
1518
git (1.2.6)
1619
github_api (0.10.1)
1720
addressable
@@ -33,13 +36,16 @@ GEM
3336
nokogiri (= 1.5.10)
3437
rake
3538
rdoc
39+
json (1.8.0)
3640
json (1.8.0-java)
3741
jwt (0.1.8)
3842
multi_json (>= 1.5)
43+
method_source (0.8.2)
3944
minitest (4.7.5)
4045
multi_json (1.7.9)
4146
multi_xml (0.5.5)
4247
multipart-post (1.2.0)
48+
nokogiri (1.5.10)
4349
nokogiri (1.5.10-java)
4450
oauth2 (0.9.2)
4551
faraday (~> 0.8)
@@ -48,6 +54,11 @@ GEM
4854
multi_json (~> 1.0)
4955
multi_xml (~> 0.5)
5056
rack (~> 1.2)
57+
pry (0.9.12.2-java)
58+
coderay (~> 1.0.5)
59+
method_source (~> 0.8)
60+
slop (~> 3.4)
61+
spoon (~> 0.0)
5162
rack (1.5.2)
5263
rake (10.1.0)
5364
rdoc (4.0.1)
@@ -58,6 +69,11 @@ GEM
5869
shoulda-context (1.1.5)
5970
shoulda-matchers (2.3.0)
6071
activesupport (>= 3.0.0)
72+
slop (3.4.6)
73+
spoon (0.0.4)
74+
ffi
75+
thread_safe (0.1.2)
76+
atomic
6177
thread_safe (0.1.2-java)
6278
atomic
6379
tzinfo (0.3.37)
@@ -68,5 +84,6 @@ PLATFORMS
6884
DEPENDENCIES
6985
bundler
7086
jeweler
87+
pry
7188
rdoc
7289
shoulda

README.rdoc

+21-7
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,35 @@ IText Pdf generation static java build packed to gem with some additional Ruby m
55
== Usage
66

77
=== Instalation
8-
require 'itext-jruby'
8+
9+
gem install itext-jruby
910

1011
== Example
12+
13+
require 'pry'
14+
require 'itext-jruby'
1115

12-
@source_pdf = File.expand_path('pdf_demo.pdf')
13-
@sample_image = File.expand_path('ja.jpg')
14-
@output_pdf = File.expand_path('out.pdf')
16+
@source_pdf = File.expand_path('test/assets/sample.pdf')
17+
@sample_image = File.expand_path('test/assets/pug.jpg')
18+
@output_pdf = File.expand_path('output.pdf')
19+
@cert_file = File.expand_path('test/assets/cert/ia.p12')
1520

16-
@pdf_document = Itext.new(path: @path)
17-
@pdf_document.add_attachment @pathb
18-
@pdf_document.save(@out)
21+
@pdf_document = Itext.new(path: @source_pdf)
22+
@pdf_document.add_attachment @sample_image
23+
@pdf_document.enable_signing!(private_key_path: @cert_file)
24+
@pdf_document.save(@output_pdf)
1925

2026
Find more examples in test directory.
2127

2228

29+
== Changelog
30+
* 0.0.6 - First stable version allowing adding attachments to PdfFile
31+
* 0.0.7 - Add option to sign documents usign pkcs12 certificates
32+
33+
== Read more
34+
* Creating pkcs12 certs: http://www.flatmtn.com/article/creating-pkcs12-certificates
35+
* Converting .pem to .pcks: http://www.advogato.org/person/johnnyb/diary.html?start=143
36+
2337
== Contributing to itext-jruby
2438

2539
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.

VERSION

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.0.6
1+
0.0.7

itext-jruby.gemspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
Gem::Specification.new do |s|
77
s.name = "itext-jruby"
8-
s.version = "0.0.5"
8+
s.version = "0.0.6"
99

1010
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
1111
s.authors = ["Ernest Bursa"]

lib/itext.rb

+19-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
# encoding: utf-8
22
require 'pathname'
3+
require 'tempfile'
34

45
class Itext
56
require 'itext/attachments'
7+
require 'itext/signing'
68

79
include Attachments
10+
include Signing
811

912
# Create new Itext document instance
1013
# Required params:
@@ -19,7 +22,10 @@ def initialize(*args, &block)
1922
raise ArgumentError.new('Please provide absolute path') unless Pathname.new(@path).absolute?
2023
end
2124

22-
def save(save_to = nil)
25+
# Saves file to given destination
26+
# Usage: save(path, options = {})
27+
# Allowed options: { sign_document: true }
28+
def save(save_to = nil, opts = {})
2329
save_to ||= @path
2430

2531
output_file = if File.exists?(@path)
@@ -30,7 +36,7 @@ def save(save_to = nil)
3036

3137
@reader = Java::ComLowagieTextPdf::PdfReader.new(@path.to_java(:string))
3238
@buffer = Java::JavaIo::FileOutputStream.new output_file.path
33-
@stamper = Java::ComLowagieTextPdf::PdfStamper.new @reader, @buffer
39+
@stamper = initialize_stamper
3440

3541
# Run all attached hooks
3642
@hooks.each { |hook| hook.call }
@@ -48,6 +54,17 @@ def save(save_to = nil)
4854

4955
protected
5056

57+
# Creates default stamper
58+
def initialize_stamper
59+
stamper = super rescue nil
60+
if stamper
61+
stamper
62+
else
63+
# Initialize default stamper
64+
Java::ComLowagieTextPdf::PdfStamper.new @reader, @buffer
65+
end
66+
end
67+
5168
def hooks
5269
@hooks || []
5370
end

lib/itext/attachments.rb

+20-16
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ module Itext::Attachments
44
def initialize(*args, &block)
55
@hooks ||= []
66
@hooks.push ->{ process_attachments }
7+
8+
super rescue nil
79
end
810

911
def self.included(base)
@@ -44,23 +46,25 @@ def remove_attachments(attachments)
4446
def process_attachments
4547
@writer = @stamper.getWriter.to_java(Java::ComLowagieTextPdf::PdfWriter)
4648

47-
@attachments.each do |attachment|
48-
if attachment.is_a?(Hash)
49-
attachment_path = attachment[:path]
50-
attachment_name = attachment[:file_name]
51-
elsif attachment.is_a?(String)
52-
attachment_path = attachment
53-
attachment_name = Pathname.new(attachment_path).basename
54-
else
55-
raise ArgumentError.new("Specify hash with :path and :file_name or string with path")
56-
end
49+
if @attachments && @attachments.size > 0
50+
@attachments.each do |attachment|
51+
if attachment.is_a?(Hash)
52+
attachment_path = attachment[:path]
53+
attachment_name = attachment[:file_name]
54+
elsif attachment.is_a?(String)
55+
attachment_path = attachment.to_s
56+
attachment_name = Pathname.new(attachment_path).basename.to_s
57+
else
58+
raise ArgumentError.new("Specify hash with :path and :file_name or string with path")
59+
end
5760

58-
attachment_spec = Java::ComLowagieTextPdf::PdfFileSpecification.fileEmbedded @writer,
59-
attachment_path.to_s.to_java_string,
60-
attachment_name.to_s.to_java_string,
61-
nil
62-
attachment_spec.addDescription attachment_name, false.to_java(:boolean)
63-
@stamper.addFileAttachment attachment_name, attachment_spec
61+
attachment_spec = Java::ComLowagieTextPdf::PdfFileSpecification.fileEmbedded @writer,
62+
attachment_path.to_java_string,
63+
attachment_name.to_java_string,
64+
nil
65+
attachment_spec.addDescription attachment_name, false.to_java(:boolean)
66+
@stamper.addFileAttachment attachment_name, attachment_spec
67+
end
6468
end
6569

6670
# @writer.close

0 commit comments

Comments
 (0)