combine_pdf 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 20e1c50aa5948f3a2ba3ad9c170ba08bc3e8ec38
4
- data.tar.gz: 75ed75edadc4fcdee63ab579419f83f8685a8c47
3
+ metadata.gz: 1415b642f2d84f101f6f444d16191faa35d3a6b9
4
+ data.tar.gz: aa872079b6576afd266bcb2b628772e80e33b9b8
5
5
  SHA512:
6
- metadata.gz: 8b70a1624d699441aab808da4ee0461796d643607622a42c2ecd58342bebd8ee8f1edcc633962e93b36dba82c0ba89400f29b06f7e115d3e21eaf0803130f032
7
- data.tar.gz: 8e5d4f16d65cd2306149dc69012aae66d5295f54d18760a335cf32fd68c3d9bd2563689bee87e44724c260653d232ddb2db93ef7e91d4c27265a48da96e7b616
6
+ metadata.gz: 78ada1b24bc160cc492b272dd31a1115a76dd217baebbac78fc259a077720427c837c053c5fe54cdfab6f84629142a45275629a714f693a6e8416bf47cb3bfa7
7
+ data.tar.gz: 03ced7106be2f831f5dc51df810d810f1f8b1828f8d6364800eb4d248764d30a6e67281b5d34d40fdbe5581673e9da1bed7a494f094e4a7ef4d6550c6e2984f2
@@ -235,7 +235,7 @@ module CombinePDF
235
235
  page_list
236
236
  end
237
237
 
238
- # this function adds pages or CombinePDF objects at the end of the file (merge)
238
+ # add the pages (or file) to the PDF (combine/merge) and return the new pages array.
239
239
  # for example:
240
240
  #
241
241
  # pdf = CombinePDF.new "first_file.pdf"
@@ -243,39 +243,64 @@ module CombinePDF
243
243
  # pdf << CombinePDF.new "second_file.pdf"
244
244
  #
245
245
  # pdf.save "both_files_merged.pdf"
246
- # @params obj is Hash, PDF or Array of parsed PDF data.
247
- def << (obj)
246
+ # data:: is PDF page (Hash), and Array of PDF pages or a parsed PDF object to be added.
247
+ def << (data)
248
248
  #########
249
249
  ## how should we add data to PDF?
250
250
  ## and how to handles imported pages?
251
- case
252
- when (obj.is_a?(PDF))
253
- @version = [@version, obj.version].max
251
+ if data.is_a?(PDF)
252
+ @version = [@version, data.version].max
254
253
 
255
- obj.renumber_object_ids @set_start_id + @objects.length
254
+ @need_to_rebuild_resources = true
256
255
 
257
- @objects.push(*obj.objects)
256
+ @objects.push(*data.objects)
258
257
  # rebuild_catalog
259
- @need_to_rebuild_resources = true
260
- when (obj.is_a?(Hash) && obj[:Type] == :Page), (obj.is_a?(Array) && (obj.reject {|i| i.is_a?(Hash) && i[:Type] == :Page}).empty?)
261
- # set obj paramater to array if it's only one page
262
- obj = [obj] if obj.is_a?(Hash)
263
- # add page(s) to objects
264
- @objects.push(*obj)
265
- # add page dependencies to objects
266
- add_referenced(obj)
267
- # add page(s) to Catalog(s)
268
- rebuild_catalog obj
269
- @need_to_rebuild_resources = true
270
- when (obj.is_a?(Hash) && obj[:indirect_reference_id] && obj[:referenced_object].nil?)
271
- #only let top level indirect objects into the PDF tree.
272
- @objects << obj
273
- @need_to_rebuild_resources = true
258
+ return rebuild_catalog[:Pages][:referenced_object][:Kids]
259
+ end
260
+ insert -1, data
261
+ end
262
+
263
+ # add the pages (or file) to the BEGINNING of the PDF (combine/merge) and return the new pages array.
264
+ # for example:
265
+ #
266
+ # pdf = CombinePDF.new "second_file.pdf"
267
+ #
268
+ # pdf >> CombinePDF.new "first_file.pdf"
269
+ #
270
+ # pdf.save "both_files_merged.pdf"
271
+ # data:: is PDF page (Hash), and Array of PDF pages or a parsed PDF object to be added.
272
+ def >> (data)
273
+ insert 0, data
274
+ end
275
+
276
+ # add PDF pages (or PDF files) into a specific location.
277
+ #
278
+ # returns the new pages Array
279
+ #
280
+ # 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).
281
+ # data:: a PDF page, a PDF file (CombinePDF.new "filname.pdf") or an array of pages (CombinePDF.new("filname.pdf").pages[0..3]).
282
+ def insert(location, data)
283
+ pages_to_add = nil
284
+ if data.is_a? PDF
285
+ pages_to_add = data.pages
286
+ elsif data.is_a?(Array) && (data.select {|o| !(o.is_a?(Hash) && o[:Type] == :Page) } ).empty?
287
+ pages_to_add = data
288
+ elsif data.is_a?(Hash) && data[:Type] == :Page
289
+ pages_to_add = [data]
274
290
  else
275
- warn "Shouldn't add objects to the file if they are not top-level indirect PDF objects."
276
- retrun false # return false, which will also stop any chaining.
291
+ warn "Shouldn't add objects to the file unless they are PDF objects or PDF pages (an Array or a single PDF page)."
292
+ retrun false # return false, which will also stop any chaining.
293
+ end
294
+ catalog = rebuild_catalog
295
+ pages_array = catalog[:Pages][:referenced_object][:Kids]
296
+ page_count = pages_array.length
297
+ if location < 0 && (page_count + location < 0 )
298
+ location = 0
299
+ elsif location > 0 && (location > page_count)
300
+ location = page_count
277
301
  end
278
- return self #return self object for injection chaining (pdf << page << page << page)
302
+ pages_array.insert location, pages_to_add
303
+ pages_array
279
304
  end
280
305
 
281
306
  # add page numbers to the PDF
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: combine_pdf
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Boaz Segev
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-09-12 00:00:00.000000000 Z
12
+ date: 2014-09-13 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: ruby-rc4