combine_pdf 0.2.5 → 0.2.37
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +2 -0
- data/CHANGELOG.md +273 -27
- data/LICENSE.txt +2 -1
- data/README.md +69 -4
- data/lib/combine_pdf/api.rb +156 -153
- data/lib/combine_pdf/basic_writer.rb +41 -53
- data/lib/combine_pdf/decrypt.rb +238 -228
- data/lib/combine_pdf/exceptions.rb +4 -0
- data/lib/combine_pdf/filter.rb +79 -85
- data/lib/combine_pdf/fonts.rb +451 -462
- data/lib/combine_pdf/page_methods.rb +891 -946
- data/lib/combine_pdf/parser.rb +663 -531
- data/lib/combine_pdf/pdf_protected.rb +341 -126
- data/lib/combine_pdf/pdf_public.rb +492 -454
- data/lib/combine_pdf/renderer.rb +146 -141
- data/lib/combine_pdf/version.rb +1 -2
- data/lib/combine_pdf.rb +14 -18
- data/test/automated +132 -0
- data/test/console +4 -4
- data/test/named_dest +84 -0
- metadata +8 -5
- data/lib/combine_pdf/operations.rb +0 -416
@@ -5,459 +5,497 @@
|
|
5
5
|
## is subject to the same license.
|
6
6
|
########################################################
|
7
7
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
8
|
module CombinePDF
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
|
370
|
-
|
371
|
-
|
372
|
-
|
373
|
-
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
|
382
|
-
|
383
|
-
|
384
|
-
|
385
|
-
|
386
|
-
|
387
|
-
|
388
|
-
|
389
|
-
|
390
|
-
|
391
|
-
|
392
|
-
|
393
|
-
|
394
|
-
|
395
|
-
|
396
|
-
|
397
|
-
|
398
|
-
|
399
|
-
|
400
|
-
|
401
|
-
|
402
|
-
|
403
|
-
|
404
|
-
|
405
|
-
|
406
|
-
|
407
|
-
|
408
|
-
|
409
|
-
|
410
|
-
|
411
|
-
|
412
|
-
|
413
|
-
|
414
|
-
|
415
|
-
|
416
|
-
|
417
|
-
|
418
|
-
|
419
|
-
|
420
|
-
|
421
|
-
|
422
|
-
|
423
|
-
|
424
|
-
|
425
|
-
|
426
|
-
|
427
|
-
|
428
|
-
|
429
|
-
|
430
|
-
|
431
|
-
|
432
|
-
|
433
|
-
|
434
|
-
|
435
|
-
|
436
|
-
|
437
|
-
|
438
|
-
|
439
|
-
|
440
|
-
|
441
|
-
|
442
|
-
|
443
|
-
|
444
|
-
|
445
|
-
|
446
|
-
|
447
|
-
|
448
|
-
|
449
|
-
|
450
|
-
|
451
|
-
|
452
|
-
|
453
|
-
|
454
|
-
|
455
|
-
|
456
|
-
|
457
|
-
|
458
|
-
|
459
|
-
|
460
|
-
|
461
|
-
|
9
|
+
# PDF class is the PDF object that can save itself to
|
10
|
+
# a file and that can be used as a container for a full
|
11
|
+
# PDF file data, including version, information etc'.
|
12
|
+
#
|
13
|
+
# PDF objects can be used to combine or to inject data.
|
14
|
+
# == Combine/Merge PDF files or Pages
|
15
|
+
# To combine PDF files (or data):
|
16
|
+
# pdf = CombinePDF.new
|
17
|
+
# pdf << CombinePDF.load("file1.pdf") # one way to combine, very fast.
|
18
|
+
# pdf << CombinePDF.load("file2.pdf")
|
19
|
+
# pdf.save "combined.pdf"
|
20
|
+
# or even a one liner:
|
21
|
+
# (CombinePDF.load("file1.pdf") << CombinePDF.load("file2.pdf") << CombinePDF.load("file3.pdf")).save("combined.pdf")
|
22
|
+
# you can also add just odd or even pages:
|
23
|
+
# pdf = CombinePDF.new
|
24
|
+
# i = 0
|
25
|
+
# CombinePDF.load("file.pdf").pages.each do |page|
|
26
|
+
# i += 1
|
27
|
+
# pdf << page if i.even?
|
28
|
+
# end
|
29
|
+
# pdf.save "even_pages.pdf"
|
30
|
+
# notice that adding all the pages one by one is slower then adding the whole file.
|
31
|
+
# == Add content to existing pages (Stamp / Watermark)
|
32
|
+
# To add content to existing PDF pages, first import the new content from an existing PDF file.
|
33
|
+
# after that, add the content to each of the pages in your existing PDF.
|
34
|
+
#
|
35
|
+
# in this example, we will add a company logo to each page:
|
36
|
+
# company_logo = CombinePDF.load("company_logo.pdf").pages[0]
|
37
|
+
# pdf = CombinePDF.load "content_file.pdf"
|
38
|
+
# pdf.pages.each {|page| page << company_logo} # notice the << operator is on a page and not a PDF object.
|
39
|
+
# pdf.save "content_with_logo.pdf"
|
40
|
+
# Notice the << operator is on a page and not a PDF object. The << operator acts differently on PDF objects and on Pages.
|
41
|
+
#
|
42
|
+
# The << operator defaults to secure injection by renaming references to avoid conflics. For overlaying pages using compressed data that might not be editable (due to limited filter support), you can use:
|
43
|
+
# pdf.pages(nil, false).each {|page| page << stamp_page}
|
44
|
+
#
|
45
|
+
# == Page Numbering
|
46
|
+
# adding page numbers to a PDF object or file is as simple as can be:
|
47
|
+
# pdf = CombinePDF.load "file_to_number.pdf"
|
48
|
+
# pdf.number_pages
|
49
|
+
# pdf.save "file_with_numbering.pdf"
|
50
|
+
#
|
51
|
+
# numbering can be done with many different options, with different formating, with or without a box object, and even with opacity values.
|
52
|
+
#
|
53
|
+
# == Loading PDF data
|
54
|
+
# Loading PDF data can be done from file system or directly from the memory.
|
55
|
+
#
|
56
|
+
# Loading data from a file is easy:
|
57
|
+
# pdf = CombinePDF.load("file.pdf")
|
58
|
+
# you can also parse PDF files from memory:
|
59
|
+
# pdf_data = IO.read 'file.pdf' # for this demo, load a file to memory
|
60
|
+
# pdf = CombinePDF.parse(pdf_data)
|
61
|
+
# Loading from the memory is especially effective for importing PDF data recieved through the internet or from a different authoring library such as Prawn.
|
62
|
+
class PDF
|
63
|
+
# lists the Hash keys used for PDF objects
|
64
|
+
#
|
65
|
+
# the CombinePDF library doesn't use special classes for its objects (PDFPage class, PDFStream class or anything like that).
|
66
|
+
#
|
67
|
+
# there is only one PDF class which represents the whole of the PDF file.
|
68
|
+
#
|
69
|
+
# this Hash lists the private Hash keys that the CombinePDF library uses to
|
70
|
+
# differentiate between complex PDF objects.
|
71
|
+
PRIVATE_HASH_KEYS = [:indirect_reference_id, :indirect_generation_number, :raw_stream_content, :is_reference_only, :referenced_object, :indirect_without_dictionary, :related_objects].freeze
|
72
|
+
|
73
|
+
# the objects attribute is an Array containing all the PDF sub-objects for te class.
|
74
|
+
attr_reader :objects
|
75
|
+
# the info attribute is a Hash that sets the Info data for the PDF.
|
76
|
+
# use, for example:
|
77
|
+
# pdf.info[:Title] = "title"
|
78
|
+
attr_reader :info
|
79
|
+
# set/get the PDF version of the file (1.1-1.7) - shuold be type Float.
|
80
|
+
attr_accessor :version
|
81
|
+
# the viewer_preferences attribute is a Hash that sets the ViewerPreferences data for the PDF.
|
82
|
+
# use, for example:
|
83
|
+
# pdf.viewer_preferences[:HideMenubar] = true
|
84
|
+
attr_reader :viewer_preferences
|
85
|
+
|
86
|
+
def initialize(parser = nil)
|
87
|
+
# default before setting
|
88
|
+
@objects = []
|
89
|
+
@version = 0
|
90
|
+
@viewer_preferences = {}
|
91
|
+
@info = {}
|
92
|
+
parser ||= PDFParser.new('')
|
93
|
+
raise TypeError, "initialization error, expecting CombinePDF::PDFParser or nil, but got #{parser.class.name}" unless parser.is_a? PDFParser
|
94
|
+
@objects = parser.parse
|
95
|
+
# remove any existing id's
|
96
|
+
remove_old_ids
|
97
|
+
# set data from parser
|
98
|
+
@version = parser.version if parser.version.is_a? Float
|
99
|
+
@info = parser.info_object || {}
|
100
|
+
@names = parser.names_object || {}
|
101
|
+
@forms_data = parser.forms_object || {}
|
102
|
+
@outlines = parser.outlines_object || {}
|
103
|
+
# rebuild the catalo, to fix wkhtmltopdf's use of static page numbers
|
104
|
+
rebuild_catalog
|
105
|
+
|
106
|
+
# general globals
|
107
|
+
@set_start_id = 1
|
108
|
+
@info[:Producer] = "Ruby CombinePDF #{CombinePDF::VERSION} Library"
|
109
|
+
@info.delete :CreationDate
|
110
|
+
@info.delete :ModDate
|
111
|
+
end
|
112
|
+
|
113
|
+
# adds a new page to the end of the PDF object.
|
114
|
+
#
|
115
|
+
# returns the new page object.
|
116
|
+
#
|
117
|
+
# unless the media box is specified, it defaults to US Letter: [0, 0, 612.0, 792.0]
|
118
|
+
def new_page(mediabox = [0, 0, 612.0, 792.0], _location = -1)
|
119
|
+
p = PDFWriter.new(mediabox)
|
120
|
+
insert(-1, p)
|
121
|
+
p
|
122
|
+
end
|
123
|
+
|
124
|
+
# get the title for the pdf
|
125
|
+
# The title is stored in the information dictionary and isn't required
|
126
|
+
def title
|
127
|
+
@info[:Title]
|
128
|
+
end
|
129
|
+
|
130
|
+
# set the title for the pdf
|
131
|
+
# The title is stored in the information dictionary and isn't required
|
132
|
+
# new_title:: a string that is the new author value.
|
133
|
+
def title=(new_title = nil)
|
134
|
+
@info[:Title] = new_title
|
135
|
+
end
|
136
|
+
|
137
|
+
# get the author value for the pdf.
|
138
|
+
# The author is stored in the information dictionary and isn't required
|
139
|
+
def author
|
140
|
+
@info[:Author]
|
141
|
+
end
|
142
|
+
|
143
|
+
# set the author value for the pdf.
|
144
|
+
# The author is stored in the information dictionary and isn't required
|
145
|
+
#
|
146
|
+
# new_title:: a string that is the new author value.
|
147
|
+
def author=(new_author = nil)
|
148
|
+
@info[:Author] = new_author
|
149
|
+
end
|
150
|
+
|
151
|
+
# Clears any existing form data.
|
152
|
+
def clear_forms_data
|
153
|
+
@forms_data.nil? || @forms_data.clear
|
154
|
+
end
|
155
|
+
|
156
|
+
# Save the PDF to file.
|
157
|
+
#
|
158
|
+
# file_name:: is a string or path object for the output.
|
159
|
+
#
|
160
|
+
# **Notice!** if the file exists, it **WILL** be overwritten.
|
161
|
+
def save(file_name, options = {})
|
162
|
+
IO.binwrite file_name, to_pdf(options)
|
163
|
+
end
|
164
|
+
|
165
|
+
# Formats the data to PDF formats and returns a binary string that represents the PDF file content.
|
166
|
+
#
|
167
|
+
# This method is used by the save(file_name) method to save the content to a file.
|
168
|
+
#
|
169
|
+
# use this to export the PDF file without saving to disk (such as sending through HTTP ect').
|
170
|
+
def to_pdf(options = {})
|
171
|
+
# reset version if not specified
|
172
|
+
@version = 1.5 if @version.to_f == 0.0
|
173
|
+
# set info for merged file
|
174
|
+
@info[:ModDate] = @info[:CreationDate] = Time.now.strftime "D:%Y%m%d%H%M%S%:::z'00"
|
175
|
+
@info[:Subject] = options[:subject] if options[:subject]
|
176
|
+
@info[:Producer] = options[:producer] if options[:producer]
|
177
|
+
# rebuild_catalog
|
178
|
+
catalog = rebuild_catalog_and_objects
|
179
|
+
# add ID and generation numbers to objects
|
180
|
+
renumber_object_ids
|
181
|
+
|
182
|
+
out = []
|
183
|
+
xref = []
|
184
|
+
indirect_object_count = 1 # the first object is the null object
|
185
|
+
# write head (version and binanry-code)
|
186
|
+
out << "%PDF-#{@version}\n%\xFF\xFF\xFF\xFF\xFF\x00\x00\x00\x00".force_encoding(Encoding::ASCII_8BIT)
|
187
|
+
|
188
|
+
# collect objects and set xref table locations
|
189
|
+
loc = 0
|
190
|
+
out.each { |line| loc += line.bytesize + 1 }
|
191
|
+
@objects.each do |o|
|
192
|
+
indirect_object_count += 1
|
193
|
+
xref << loc
|
194
|
+
out << object_to_pdf(o)
|
195
|
+
loc += out.last.bytesize + 1
|
196
|
+
end
|
197
|
+
xref_location = loc
|
198
|
+
# xref_location = 0
|
199
|
+
# out.each { |line| xref_location += line.bytesize + 1}
|
200
|
+
out << "xref\n0 #{indirect_object_count}\n0000000000 65535 f \n"
|
201
|
+
xref.each { |offset| out << (out.pop + ("%010d 00000 n \n" % offset)) }
|
202
|
+
out << out.pop + 'trailer'
|
203
|
+
out << "<<\n/Root #{false || "#{catalog[:indirect_reference_id]} #{catalog[:indirect_generation_number]} R"}"
|
204
|
+
out << "/Size #{indirect_object_count}"
|
205
|
+
out << "/Info #{@info[:indirect_reference_id]} #{@info[:indirect_generation_number]} R"
|
206
|
+
out << ">>\nstartxref\n#{xref_location}\n%%EOF"
|
207
|
+
# when finished, remove the numbering system and keep only pointers
|
208
|
+
remove_old_ids
|
209
|
+
# output the pdf stream
|
210
|
+
out.join("\n").force_encoding(Encoding::ASCII_8BIT)
|
211
|
+
end
|
212
|
+
|
213
|
+
# this method returns all the pages cataloged in the catalog.
|
214
|
+
#
|
215
|
+
# if no catalog is passed, it seeks the existing catalog(s) and searches
|
216
|
+
# for any registered Page objects.
|
217
|
+
#
|
218
|
+
# Page objects are Hash class objects. the page methods are added using a mixin or inheritance.
|
219
|
+
#
|
220
|
+
# catalogs:: a catalog, or an Array of catalog objects. defaults to the existing catalog.
|
221
|
+
def pages(catalogs = nil)
|
222
|
+
page_list = []
|
223
|
+
catalogs ||= get_existing_catalogs
|
224
|
+
|
225
|
+
if catalogs.is_a?(Array)
|
226
|
+
catalogs.each { |c| page_list.concat pages(c) unless c.nil? }
|
227
|
+
elsif catalogs.is_a?(Hash)
|
228
|
+
if catalogs[:is_reference_only]
|
229
|
+
if catalogs[:referenced_object]
|
230
|
+
page_list.concat pages(catalogs[:referenced_object])
|
231
|
+
else
|
232
|
+
warn "couldn't follow reference!!! #{catalogs} not found!"
|
233
|
+
end
|
234
|
+
else
|
235
|
+
case catalogs[:Type]
|
236
|
+
when :Page
|
237
|
+
page_list << catalogs
|
238
|
+
when :Pages
|
239
|
+
page_list.concat pages(catalogs[:Kids]) unless catalogs[:Kids].nil?
|
240
|
+
when :Catalog
|
241
|
+
page_list.concat pages(catalogs[:Pages]) unless catalogs[:Pages].nil?
|
242
|
+
end
|
243
|
+
end
|
244
|
+
end
|
245
|
+
page_list
|
246
|
+
end
|
247
|
+
|
248
|
+
# returns an array with the different fonts used in the file.
|
249
|
+
#
|
250
|
+
# Type0 font objects ( "font[:Subtype] == :Type0" ) can be registered with the font library
|
251
|
+
# for use in PDFWriter objects (font numbering / table creation etc').
|
252
|
+
# @param limit_to_type0 [true,false] limits the list to type0 fonts.
|
253
|
+
def fonts(limit_to_type0 = false)
|
254
|
+
fonts_array = []
|
255
|
+
pages.each do |p|
|
256
|
+
p[:Resources][:Font].values.each do |f|
|
257
|
+
f = f[:referenced_object] if f[:referenced_object]
|
258
|
+
if (limit_to_type0 || f[:Subtype] = :Type0) && f[:Type] == :Font && !fonts_array.include?(f)
|
259
|
+
fonts_array << f
|
260
|
+
end
|
261
|
+
end
|
262
|
+
end
|
263
|
+
fonts_array
|
264
|
+
end
|
265
|
+
|
266
|
+
# add the pages (or file) to the PDF (combine/merge) and RETURNS SELF, for nesting.
|
267
|
+
# for example:
|
268
|
+
#
|
269
|
+
# pdf = CombinePDF.new "first_file.pdf"
|
270
|
+
#
|
271
|
+
# pdf << CombinePDF.new "second_file.pdf"
|
272
|
+
#
|
273
|
+
# pdf.save "both_files_merged.pdf"
|
274
|
+
# data:: is PDF page (Hash), and Array of PDF pages or a parsed PDF object to be added.
|
275
|
+
def <<(data)
|
276
|
+
insert -1, data
|
277
|
+
end
|
278
|
+
|
279
|
+
# add the pages (or file) to the BEGINNING of the PDF (combine/merge) and RETURNS SELF for nesting operators.
|
280
|
+
# for example:
|
281
|
+
#
|
282
|
+
# pdf = CombinePDF.new "second_file.pdf"
|
283
|
+
#
|
284
|
+
# pdf >> CombinePDF.new "first_file.pdf"
|
285
|
+
#
|
286
|
+
# pdf.save "both_files_merged.pdf"
|
287
|
+
# data:: is PDF page (Hash), and Array of PDF pages or a parsed PDF object to be added.
|
288
|
+
def >>(data)
|
289
|
+
insert 0, data
|
290
|
+
end
|
291
|
+
|
292
|
+
# add PDF pages (or PDF files) into a specific location.
|
293
|
+
#
|
294
|
+
# returns the new pages Array! (unlike `#<<`, doesn't return self!)
|
295
|
+
#
|
296
|
+
# location:: the location for the added page(s). Could be any number. negative numbers represent a count backwards (-1 being the end of the page array and 0 being the begining). if the location is beyond bounds, the pages will be added to the end of the PDF object (or at the begining, if the out of bounds was a negative number).
|
297
|
+
# data:: a PDF page, a PDF file (CombinePDF.new "filname.pdf") or an array of pages (CombinePDF.new("filname.pdf").pages[0..3]).
|
298
|
+
def insert(location, data)
|
299
|
+
pages_to_add = nil
|
300
|
+
if data.is_a? PDF
|
301
|
+
@version = [@version, data.version].max
|
302
|
+
pages_to_add = data.pages
|
303
|
+
actual_value(@names ||= {}.dup).update actual_value(data.names_object), &self.class.method(:hash_merge_new_no_page)
|
304
|
+
merge_outlines((@outlines ||= {}.dup), data.outlines_object, location) unless actual_value(data.outlines_object).empty?
|
305
|
+
if actual_value(@forms_data)
|
306
|
+
actual_value(@forms_data).update actual_value(data.forms_data), &self.class.method(:hash_merge_new_no_page) if data.forms_data
|
307
|
+
else
|
308
|
+
@forms_data = data.forms_data
|
309
|
+
end
|
310
|
+
warn 'Form data might be lost when combining PDF forms (possible conflicts).' unless data.forms_data.nil? || data.forms_data.empty?
|
311
|
+
elsif data.is_a?(Array) && (data.select { |o| !(o.is_a?(Hash) && o[:Type] == :Page) }).empty?
|
312
|
+
pages_to_add = data
|
313
|
+
elsif data.is_a?(Hash) && data[:Type] == :Page
|
314
|
+
pages_to_add = [data]
|
315
|
+
else
|
316
|
+
warn "Shouldn't add objects to the file unless they are PDF objects or PDF pages (an Array or a single PDF page)."
|
317
|
+
return false # return false, which will also stop any chaining.
|
318
|
+
end
|
319
|
+
# pages_to_add.map! {|page| page.copy }
|
320
|
+
catalog = rebuild_catalog
|
321
|
+
pages_array = catalog[:Pages][:referenced_object][:Kids]
|
322
|
+
page_count = pages_array.length
|
323
|
+
if location < 0 && (page_count + location < 0)
|
324
|
+
location = 0
|
325
|
+
elsif location > 0 && (location > page_count)
|
326
|
+
location = page_count
|
327
|
+
end
|
328
|
+
pages_array.insert location, pages_to_add
|
329
|
+
pages_array.flatten!
|
330
|
+
self
|
331
|
+
end
|
332
|
+
|
333
|
+
# removes a PDF page from the file and the catalog
|
334
|
+
#
|
335
|
+
# returns the removed page.
|
336
|
+
#
|
337
|
+
# returns nil if failed or if out of bounds.
|
338
|
+
#
|
339
|
+
# page_index:: the page's index in the zero (0) based page array. negative numbers represent a count backwards (-1 being the end of the page array and 0 being the begining).
|
340
|
+
def remove(page_index)
|
341
|
+
catalog = rebuild_catalog
|
342
|
+
pages_array = catalog[:Pages][:referenced_object][:Kids]
|
343
|
+
removed_page = pages_array.delete_at page_index
|
344
|
+
catalog[:Pages][:referenced_object][:Count] = pages_array.length
|
345
|
+
removed_page
|
346
|
+
end
|
347
|
+
|
348
|
+
# add page numbers to the PDF
|
349
|
+
#
|
350
|
+
# For unicode text, a unicode font(s) must first be registered. the registered font(s) must supply the
|
351
|
+
# subset of characters used in the text. UNICODE IS AN ISSUE WITH THE PDF FORMAT - USE CAUSION.
|
352
|
+
#
|
353
|
+
# options:: a Hash of options setting the behavior and format of the page numbers:
|
354
|
+
# - :number_format a string representing the format for page number. defaults to ' - %s - ' (allows for letter numbering as well, such as "a", "b"...).
|
355
|
+
# - :location an Array containing the location for the page numbers, can be :top, :buttom, :top_left, :top_right, :bottom_left, :bottom_right or :center (:center == full page). defaults to [:top, :buttom].
|
356
|
+
# - :start_at an Integer that sets the number for first page number. also accepts a letter ("a") for letter numbering. defaults to 1.
|
357
|
+
# - :margin_from_height a number (PDF points) for the top and buttom margins. defaults to 45.
|
358
|
+
# - :margin_from_side a number (PDF points) for the left and right margins. defaults to 15.
|
359
|
+
# - :page_range a range of pages to be numbered (i.e. (2..-1) ) defaults to all the pages (nil). Remember to set the :start_at to the correct value.
|
360
|
+
# the options Hash can also take all the options for {Page_Methods#textbox}.
|
361
|
+
# defaults to font: :Helvetica, font_size: 12 and no box (:border_width => 0, :box_color => nil).
|
362
|
+
def number_pages(options = {})
|
363
|
+
opt = {
|
364
|
+
number_format: ' - %s - ',
|
365
|
+
start_at: 1,
|
366
|
+
font: :Helvetica,
|
367
|
+
margin_from_height: 45,
|
368
|
+
margin_from_side: 15
|
369
|
+
}
|
370
|
+
opt.update options
|
371
|
+
opt[:location] ||= opt[:number_location] ||= opt[:stamp_location] ||= [:top, :bottom]
|
372
|
+
opt[:location] = [opt[:location]] unless opt[:location].is_a? Array
|
373
|
+
|
374
|
+
page_number = opt[:start_at]
|
375
|
+
format_repeater = opt[:number_format].count('%')
|
376
|
+
just_center = [:center]
|
377
|
+
small_font_size = opt[:font_size] || 12
|
378
|
+
|
379
|
+
# some common computations can be done only once.
|
380
|
+
from_height = opt[:margin_from_height]
|
381
|
+
from_side = opt[:margin_from_side]
|
382
|
+
left_position = from_side
|
383
|
+
|
384
|
+
(opt[:page_range] ? pages[opt[:page_range]] : pages).each do |page|
|
385
|
+
# Get page dimensions
|
386
|
+
mediabox = page[:CropBox] || page[:MediaBox] || [0, 0, 595.3, 841.9]
|
387
|
+
# set stamp text
|
388
|
+
text = opt[:number_format] % (Array.new(format_repeater) { page_number })
|
389
|
+
if opt[:location].include? :center
|
390
|
+
add_opt = {}
|
391
|
+
if opt[:margin_from_height] && !opt[:height] && !opt[:y]
|
392
|
+
add_opt[:height] = mediabox[3] - mediabox[1] - (2 * opt[:margin_from_height].to_f)
|
393
|
+
add_opt[:y] = opt[:margin_from_height]
|
394
|
+
end
|
395
|
+
if opt[:margin_from_side] && !opt[:width] && !opt[:x]
|
396
|
+
add_opt[:width] = mediabox[2] - mediabox[0] - (2 * opt[:margin_from_side].to_f)
|
397
|
+
add_opt[:x] = opt[:margin_from_side]
|
398
|
+
end
|
399
|
+
page.textbox text, opt.merge(add_opt)
|
400
|
+
end
|
401
|
+
unless opt[:location] == just_center
|
402
|
+
add_opt = { font_size: small_font_size }.merge(opt)
|
403
|
+
# text = opt[:number_format] % page_number
|
404
|
+
# compute locations for text boxes
|
405
|
+
text_dimantions = Fonts.dimensions_of(text, opt[:font], small_font_size)
|
406
|
+
box_width = text_dimantions[0] * 1.2
|
407
|
+
box_height = text_dimantions[1] * 2
|
408
|
+
page_width = mediabox[2]
|
409
|
+
page_height = mediabox[3]
|
410
|
+
|
411
|
+
add_opt[:width] ||= box_width
|
412
|
+
add_opt[:height] ||= box_height
|
413
|
+
|
414
|
+
center_position = (page_width - box_width) / 2
|
415
|
+
right_position = page_width - from_side - box_width
|
416
|
+
top_position = page_height - from_height
|
417
|
+
bottom_position = from_height + box_height
|
418
|
+
|
419
|
+
if opt[:location].include? :top
|
420
|
+
page.textbox text, { x: center_position, y: top_position }.merge(add_opt)
|
421
|
+
end
|
422
|
+
if opt[:location].include? :bottom
|
423
|
+
page.textbox text, { x: center_position, y: bottom_position }.merge(add_opt)
|
424
|
+
end
|
425
|
+
if opt[:location].include? :top_left
|
426
|
+
page.textbox text, { x: left_position, y: top_position, font_size: small_font_size }.merge(add_opt)
|
427
|
+
end
|
428
|
+
if opt[:location].include? :bottom_left
|
429
|
+
page.textbox text, { x: left_position, y: bottom_position, font_size: small_font_size }.merge(add_opt)
|
430
|
+
end
|
431
|
+
if opt[:location].include? :top_right
|
432
|
+
page.textbox text, { x: right_position, y: top_position, font_size: small_font_size }.merge(add_opt)
|
433
|
+
end
|
434
|
+
if opt[:location].include? :bottom_right
|
435
|
+
page.textbox text, { x: right_position, y: bottom_position, font_size: small_font_size }.merge(add_opt)
|
436
|
+
end
|
437
|
+
end
|
438
|
+
page_number = page_number.succ
|
439
|
+
end
|
440
|
+
end
|
441
|
+
|
442
|
+
# This method stamps all (or some) of the pages is the PDF with the requested stamp.
|
443
|
+
#
|
444
|
+
# The method accept:
|
445
|
+
# stamp:: either a String or a PDF page. If this is a String, you can add formating to add page numbering (i.e. "page number %i"). otherwise remember to escape any percent ('%') sign (i.e. "page \%number not shown\%").
|
446
|
+
# options:: an options Hash.
|
447
|
+
#
|
448
|
+
# If the stamp is a PDF page, only :page_range and :underlay (to reverse-stamp) are valid options.
|
449
|
+
#
|
450
|
+
# If the stamp is a String, than all the options used by {#number_pages} or {Page_Methods#textbox} can be used.
|
451
|
+
#
|
452
|
+
# The default :location option is :center = meaning the stamp will be stamped all across the page unless the :x, :y, :width or :height options are specified.
|
453
|
+
def stamp_pages(stamp, options = {})
|
454
|
+
case stamp
|
455
|
+
when String
|
456
|
+
options[:location] ||= [:center]
|
457
|
+
number_pages({ number_format: stamp }.merge(options))
|
458
|
+
when Page_Methods
|
459
|
+
# stamp = stamp.copy(true)
|
460
|
+
if options[:underlay]
|
461
|
+
(options[:page_range] ? pages[options[:page_range]] : pages).each { |p| p >> stamp }
|
462
|
+
else
|
463
|
+
(options[:page_range] ? pages[options[:page_range]] : pages).each { |p| p << stamp }
|
464
|
+
end
|
465
|
+
else
|
466
|
+
raise TypeError, 'expecting a String or a PDF page as the stamp.'
|
467
|
+
end
|
468
|
+
end
|
469
|
+
|
470
|
+
# the form_data attribute is a Hash that corresponds to the PDF form data (if any).
|
471
|
+
attr_reader :forms_data
|
472
|
+
|
473
|
+
# # experimental, allows form data updates. This function lists the form data fields.
|
474
|
+
# def form_list
|
475
|
+
# if(@forms_data && actual_value(@forms_data).is_a?(Hash) && actual_value(@forms_data)[:Fields])
|
476
|
+
# actual_value(actual_value(@forms_data)[:Fields]).map {|f| actual_value(f)[:Kids] ? ({actual_value(f)[:T] => actual_value(f)[:Kids].map {|k| actual_value(k)[:T]}}) : actual_value(f)[:T]}
|
477
|
+
# else
|
478
|
+
# nil
|
479
|
+
# end
|
480
|
+
# end
|
481
|
+
# # experimental, allows form data updates. This function gets the value of a specific form data field.
|
482
|
+
# def form_get name
|
483
|
+
# if(@forms_data && actual_value(@forms_data).is_a?(Hash) && actual_value(@forms_data)[:Fields])
|
484
|
+
# actual_value(actual_value(@forms_data)[:Fields]).map {|f| return actual_value(f)[:Kids] ? ({actual_value(f)[:V] => actual_value(f)[:Kids].map {|k| actual_value(k)[:V]}}) : actual_value(f)[:V] if actual_value(f)[:T] == name}
|
485
|
+
# # actual_value(actual_value(@forms_data)[:Fields]).each {|f| return actual_value(f)[:V] if actual_value(f)[:T] == name}
|
486
|
+
# else
|
487
|
+
# nil
|
488
|
+
# end
|
489
|
+
# nil
|
490
|
+
# end
|
491
|
+
# # experimental, allows form data updates. This function sets the value of a specific form data field.
|
492
|
+
# def form_set name, value
|
493
|
+
# if(@forms_data && actual_value(@forms_data).is_a?(Hash) && actual_value(@forms_data)[:Fields])
|
494
|
+
# actual_value(actual_value(@forms_data)[:Fields]).each {|f| return actual_value(f)[:V]=value if actual_value(f)[:T] == name}
|
495
|
+
# else
|
496
|
+
# nil
|
497
|
+
# end
|
498
|
+
# nil
|
499
|
+
# end
|
500
|
+
end
|
462
501
|
end
|
463
|
-
|