jpdfer 0.4.0-java → 0.5.0-java
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.
- data/lib/jpdfer/page_range_utilities.rb +35 -0
- data/lib/jpdfer/pdf.rb +11 -2
- data/lib/jpdfer/version.rb +1 -1
- data/spec/data/simple_form_2.pdf +0 -0
- data/spec/page_range_utilites_spec.rb +40 -0
- data/spec/pdf_spec.rb +5 -0
- metadata +7 -2
@@ -0,0 +1,35 @@
|
|
1
|
+
module Jpdfer
|
2
|
+
module PageRangeUtilities
|
3
|
+
module_function
|
4
|
+
|
5
|
+
def normalize_page_range page_range
|
6
|
+
pages = expand_page_range page_range
|
7
|
+
split_list pages
|
8
|
+
end
|
9
|
+
|
10
|
+
def expand_page_range page_range
|
11
|
+
page_range.split(',').reduce([]) do |result, sub_range|
|
12
|
+
if sub_range.include? '-'
|
13
|
+
raise "Invalid Range: #{sub_range}" unless sub_range =~ /^\d+-\d+$/
|
14
|
+
first, last = sub_range.split '-'
|
15
|
+
result.concat (first.to_i..last.to_i).to_a
|
16
|
+
else
|
17
|
+
raise "Invalid Range: #{sub_range}" unless sub_range.to_i.to_s == sub_range
|
18
|
+
result << sub_range.to_i
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def split_list list
|
24
|
+
list.reduce([[]]) do |result, number|
|
25
|
+
current = result.last
|
26
|
+
if current.include? number
|
27
|
+
result << [number]
|
28
|
+
else
|
29
|
+
current << number
|
30
|
+
result
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
data/lib/jpdfer/pdf.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'jpdfer/page_sizes'
|
2
|
+
require 'jpdfer/page_range_utilities'
|
2
3
|
# High-level/convenience wrapper class for a PDF document.
|
3
4
|
|
4
5
|
module Jpdfer
|
@@ -45,8 +46,16 @@ module Jpdfer
|
|
45
46
|
flatten = options.delete(:flatten)
|
46
47
|
concatenator = PdfCopyFields.new output_buffer.to_outputstream
|
47
48
|
|
48
|
-
pdfs.each do |pdf|
|
49
|
-
|
49
|
+
pdfs.each do |pdf, page_range|
|
50
|
+
if page_range
|
51
|
+
PageRangeUtilities::normalize_page_range(page_range).each do |pages|
|
52
|
+
# We need to help jruby convert Fixnum to java.lang.Integer. It defaults to java.lang.Long
|
53
|
+
pages = pages.map {|page| Java::JavaLang::Integer.new page}
|
54
|
+
concatenator.addDocument pdf.reader, pages
|
55
|
+
end
|
56
|
+
else
|
57
|
+
concatenator.addDocument pdf.reader
|
58
|
+
end
|
50
59
|
end
|
51
60
|
concatenator.close
|
52
61
|
|
data/lib/jpdfer/version.rb
CHANGED
Binary file
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module Jpdfer
|
4
|
+
describe PageRangeUtilities do
|
5
|
+
describe '.normalize_page_range' do
|
6
|
+
it "should convert '' to []" do
|
7
|
+
Jpdfer::PageRangeUtilities.normalize_page_range('').should == [[]]
|
8
|
+
end
|
9
|
+
|
10
|
+
it "should convert '1' to [1]" do
|
11
|
+
Jpdfer::PageRangeUtilities.normalize_page_range('1').should == [[1]]
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should convert '1-2' to [1,2]" do
|
15
|
+
Jpdfer::PageRangeUtilities.normalize_page_range('1-2').should == [[1,2]]
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should convert '1-2,3-4,5-6,7' to [1,2,3,4,5,6,7]" do
|
19
|
+
Jpdfer::PageRangeUtilities.normalize_page_range('1-2,3-4,5-6,7').should == [[1,2,3,4,5,6,7]]
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should convert '1-2,3,4,6' to [1,2,3,4,6]" do
|
23
|
+
Jpdfer::PageRangeUtilities.normalize_page_range('1-2,3,4,6').should == [[1,2,3,4,6]]
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should convert '1-2,3,2,4-5,6' to [[1,2,3],[2,4,6]]" do
|
27
|
+
Jpdfer::PageRangeUtilities.normalize_page_range('1-2,3,2,4-5,6').should == [[1,2,3],[2,4,5,6]]
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should convert '1-2,3,2,4-5,6' to [[1,2,3],[2,4,5,6]]" do
|
31
|
+
Jpdfer::PageRangeUtilities.normalize_page_range('1-2,3,2,4-5,6').should == [[1,2,3],[2,4,5,6]]
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should convert '1,1,1,2,3,4,5,2,3,5,6' to [[1],[1],[1,2,3,4,5],[2,3,5,6]]" do
|
35
|
+
Jpdfer::PageRangeUtilities.normalize_page_range('1,1,1,2,3,4,5,2,3,5,6').should == [[1],[1],[1,2,3,4,5],[2,3,5,6]]
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
data/spec/pdf_spec.rb
CHANGED
@@ -151,6 +151,11 @@ describe Pdf do
|
|
151
151
|
new_pdf = Pdf.new(@save_path)
|
152
152
|
new_pdf.number_of_pages.should == @pdf_1.number_of_pages + @pdf_2.number_of_pages
|
153
153
|
end
|
154
|
+
|
155
|
+
it 'should allow the specification of a page range for a pdf' do
|
156
|
+
pdf = Pdf.concatenate([[@pdf_1, '1,1,1'], @pdf_2], @save_path)
|
157
|
+
pdf.number_of_pages.should == 3 + @pdf_2.number_of_pages
|
158
|
+
end
|
154
159
|
end
|
155
160
|
|
156
161
|
describe 'forwards any potential messages to the reader if it will respond' do
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: jpdfer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.
|
5
|
+
version: 0.5.0
|
6
6
|
platform: java
|
7
7
|
authors:
|
8
8
|
- Scott Nielsen
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date:
|
13
|
+
date: 2013-01-22 00:00:00.000000000Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: nokogiri
|
@@ -58,14 +58,17 @@ files:
|
|
58
58
|
- jars/itextpdf-5.1.1.jar
|
59
59
|
- lib/jpdfer.rb
|
60
60
|
- lib/jpdfer/key_store.rb
|
61
|
+
- lib/jpdfer/page_range_utilities.rb
|
61
62
|
- lib/jpdfer/page_sizes.rb
|
62
63
|
- lib/jpdfer/pdf.rb
|
63
64
|
- lib/jpdfer/version.rb
|
65
|
+
- spec/page_range_utilites_spec.rb
|
64
66
|
- spec/pdf_spec.rb
|
65
67
|
- spec/spec_helper.rb
|
66
68
|
- spec/data/keystore.ks
|
67
69
|
- spec/data/flattened.pdf
|
68
70
|
- spec/data/simple_form.pdf
|
71
|
+
- spec/data/simple_form_2.pdf
|
69
72
|
- spec/data/simple_form_flattened.pdf
|
70
73
|
- spec/data/simple_form_flattened_signed.pdf
|
71
74
|
- spec/data/simple_form_signed_by_someone_else.pdf
|
@@ -96,11 +99,13 @@ signing_key:
|
|
96
99
|
specification_version: 3
|
97
100
|
summary: Read and write PDF forms in JRuby
|
98
101
|
test_files:
|
102
|
+
- spec/page_range_utilites_spec.rb
|
99
103
|
- spec/pdf_spec.rb
|
100
104
|
- spec/spec_helper.rb
|
101
105
|
- spec/data/keystore.ks
|
102
106
|
- spec/data/flattened.pdf
|
103
107
|
- spec/data/simple_form.pdf
|
108
|
+
- spec/data/simple_form_2.pdf
|
104
109
|
- spec/data/simple_form_flattened.pdf
|
105
110
|
- spec/data/simple_form_flattened_signed.pdf
|
106
111
|
- spec/data/simple_form_signed_by_someone_else.pdf
|