4
4
5
5
require 'rbconfig'
6
6
require 'fileutils'
7
- require 'tmpdir'
7
+ #require 'tmpdir'
8
+ #require 'active_support/inflector/methods'
8
9
9
10
include FileUtils
10
11
11
12
if RUBY_PLATFORM =~ /java/ # ifdef to check if your using JRuby
12
- $:. unshift ( File . join ( File . dirname ( __FILE__ ) , '..' , '..' , '..' , 'ext' ) )
13
- require 'java'
14
- require 'iText-4.2.0.jar'
15
-
16
- java_import 'java.io.FileOutputStream'
17
- java_import 'java.io.ByteArrayOutputStream'
18
- java_import 'com.lowagie.text.pdf.AcroFields'
19
- java_import 'com.lowagie.text.pdf.PdfReader'
20
- java_import 'com.lowagie.text.pdf.PdfStamper'
21
- java_import 'com.lowagie.text.Image'
22
- java_import 'com.lowagie.text.Rectangle'
23
- java_import 'com.lowagie.text.pdf.GrayColor'
13
+ require 'pdf/stamper/jruby'
24
14
else
25
15
require 'pdf/stamper/rjb'
26
16
end
27
17
28
18
module PDF
29
19
class Stamper
30
- VERSION = "0.5"
20
+ VERSION = "0.6.0"
21
+
22
+
31
23
# PDF::Stamper provides an interface into iText's PdfStamper allowing for the
32
24
# editing of existing PDFs as templates. PDF::Stamper is not a PDF generator,
33
25
# it allows you to edit existing PDFs and use them as templates.
@@ -77,6 +69,32 @@ def image(key, image_path)
77
69
cb . addImage ( img )
78
70
end
79
71
72
+ # PDF::Stamper allows setting metadata on the created PDF by passing
73
+ # the parameters to the set_more_info function. Our implementation here
74
+ # is slightly different from iText, in that we only receive a single key/value
75
+ # pair at a time, instead of a Map<string,string> since that is slightly
76
+ # more complex to bridge properly from ruby to java.
77
+ #
78
+ # Possible keys include "Creator". All values here are strings.
79
+ #
80
+ def set_metadata ( key , value )
81
+ params = java . util . HashMap . new ( )
82
+ params . put ( key . to_s , value )
83
+ @stamp . setMoreInfo ( params )
84
+ end
85
+
86
+ # If you want to have iText reset some of the metadata, this function will
87
+ # cause iText to use its default xml metadata.
88
+ def reset_xmp_metadata ( )
89
+ @stamp . setXmpMetadata ( "" . to_java_bytes )
90
+ end
91
+
92
+ # Set a textfield defined by key and text to value
93
+ def text ( key , value )
94
+ @form . setField ( key . to_s , value . to_s ) # Value must be a string or itext will error.
95
+ end
96
+
97
+
80
98
# Takes the PDF output and sends as a string.
81
99
#
82
100
# Here is how to use it in rails:
@@ -92,15 +110,11 @@ def to_s
92
110
String . from_java_bytes ( @baos . toByteArray )
93
111
end
94
112
95
- # Set a textfield defined by key and text to value.
96
- def text ( key , value )
97
- @form . setField ( key . to_s , value . to_s ) # Value must be a string or itext will error.
98
- end
99
113
100
114
# Set a checkbox to checked
101
115
def checkbox ( key )
102
116
field_type = @form . getFieldType ( key . to_s )
103
- return unless field_type == @acrofields . FIELD_TYPE_CHECKBOX
117
+ return unless is_checkbox ( field_type )
104
118
105
119
all_states = @form . getAppearanceStates ( key . to_s )
106
120
yes_state = all_states . reject { |x | x == "Off" }
@@ -112,7 +126,7 @@ def checkbox(key)
112
126
# Get checkbox values
113
127
def get_checkbox_values ( key )
114
128
field_type = @form . getFieldType ( key . to_s )
115
- return unless field_type == @acrofields . FIELD_TYPE_CHECKBOX
129
+ return unless is_checkbox ( field_type )
116
130
117
131
@form . getAppearanceStates ( key . to_s )
118
132
end
@@ -128,8 +142,42 @@ def ellipse(x, y, width, height)
128
142
def rectangle ( x , y , width , height )
129
143
@canvas . rectangle ( x , y , width , height )
130
144
end
145
+
146
+ # Example
147
+ # barcode("PDF417", "2d_barcode", "Barcode data...", AspectRatio: 0.5)
148
+ def barcode ( format , key , value , opts = { } )
149
+ bar = create_barcode ( format )
150
+ bar . setText ( value )
151
+ opts . each do |name , opt |
152
+ #bar.send("set#{name.to_s.camelize}", opt) #Camelize is not present outside of Rails by default
153
+ bar . send ( "set#{ name . to_s } " , opt )
154
+ end
155
+
156
+ coords = @form . getFieldPositions ( key . to_s )
157
+ rect = create_rectangle ( coords )
158
+
159
+ barcode_img = bar . getImage
160
+ barcode_img . scalePercent ( 100 , 100 * bar . getYHeight )
161
+ barcode_img . setAbsolutePosition (
162
+ coords [ 1 ] + ( rect . getWidth - barcode_img . getScaledWidth ) / 2 ,
163
+ coords [ 2 ] + ( rect . getHeight - barcode_img . getScaledHeight ) / 2
164
+ )
165
+
166
+ cb = @stamp . getOverContent ( coords [ 0 ] . to_i )
167
+ cb . addImage ( barcode_img )
168
+ end
169
+
170
+ # this has to be called *before* setting field values
171
+ def set_font ( font_name )
172
+ itr = @form . getFields . keySet . iterator
173
+ while itr . hasNext
174
+ field = itr . next
175
+ @form . setFieldProperty ( field , 'textfont' , create_font ( font_name ) , nil )
176
+ end
177
+ end
131
178
132
- # Saves the PDF into a file defined by path given.
179
+ # Saves the PDF into a file defined by path given. If you want to save
180
+ # to a string/buffer, just use .to_s directly.
133
181
def save_as ( file )
134
182
File . open ( file , "wb" ) { |f | f . write to_s }
135
183
end
0 commit comments