combine_pdf 0.2.3 → 0.2.4

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: 84c4c10854fe1876f80ef97367f22ac2ea60e2b1
4
- data.tar.gz: 6ebc1e28df2124ceb2f845532601e48f962b908e
3
+ metadata.gz: bfa5b9792e629049a4ebf6a379b1e834f6f76eb7
4
+ data.tar.gz: 0ea35cdbccc1fa1480370cccfa8e11351f6973d5
5
5
  SHA512:
6
- metadata.gz: 4779c64b3e923500c2e0aaab1e655763b9b055e5c85b44d00d304fc5e39f851ac075b8530453b55e3565c3ee540e584c518744de7e516b0dbceaa3109b2a30ae
7
- data.tar.gz: 567e5a818742da6e0b76e5eb647cde9b8b98ed184b557e0b1c6718f289559097b145f34647c4a5cac12521f42027c97184aa973af2bb18a0b3742423d73444c3
6
+ metadata.gz: a5baf7554a0757a4666e22464ef0d6f515153a8ea7c6b1058edb92090f4f683808996b259539b5e2553ced8e74c1c6a78ef4bf947442ee131bb5be31c43d34f0
7
+ data.tar.gz: 171cf6606fa1a4d91b9652f4d166bfa198ed6acd049631f0f50137362cbcf10a7c5d15b6191434a9bb9bc52b7eab9a00cafe07fb2edeb656bfba2f06eb71eb92
@@ -2,6 +2,14 @@
2
2
 
3
3
  ***
4
4
 
5
+ Change log v.0.2.4
6
+
7
+ **fixed**: Fixed the default page sizes which weren't as described in the documentation and now default to US Letter. The documentation was also fixed. No major version bump is declered since the defaults were faulty and weren't as described (fixed a bug, not changed the API).
8
+
9
+ **feature**: added the `resize` page method, to allow resizing of pages with or without conserving the content's aspect ratio (defaults to conserving the aspect ratio).
10
+
11
+ ***
12
+
5
13
  Change log v.0.2.3
6
14
 
7
15
  **update**: a better general error message for CombinePDF.new
@@ -92,6 +92,23 @@ load "combine_pdf/version.rb"
92
92
  #
93
93
  # font support for the writer is still in the works and is limited to extracting know fonts by location of the 14 standard fonts.
94
94
  #
95
+ # == Resizing pages
96
+ #
97
+ # Using the {http://www.prepressure.com/library/paper-size PDF standards for page sizes}, it is now possible to resize
98
+ # existing PDF pages, as well as stretch and shrink their content to the new size.
99
+ #
100
+ # pdf = CombinePDF.load "file.pdf"
101
+ # a4_size = [0, 0, 595, 842]
102
+ # # keep aspect ratio intact
103
+ # pdf.pages.each {|p| p.resize a4_size}
104
+ # pdf.save "a4.pdf"
105
+ #
106
+ # pdf = CombinePDF.load "file.pdf"
107
+ # a4_squared = [0, 0, 595, 595]
108
+ # # stretch or shrink content to fit new size
109
+ # pdf.pages.each {|p| p.resize a4_squared, false}
110
+ # pdf.save "square.pdf"
111
+ #
95
112
  # == Decryption & Filters
96
113
  #
97
114
  # Some PDF files are encrypted and some are compressed (the use of filters)... not all files can be opened, merged, stamped or used and stamps.
@@ -47,10 +47,10 @@ module CombinePDF
47
47
  # PDFWriter objects are used internally for numbering pages (by creating a PDF page
48
48
  # with the page number and "stamping" it over the existing page).
49
49
  #
50
- # ::mediabox an Array representing the size of the PDF document. defaults to: [0.0, 0.0, 612.0, 792.0]
50
+ # ::mediabox an Array representing the size of the PDF document. defaults to: [0.0, 0.0, 612.0, 792.0] (US Letter)
51
51
  #
52
52
  # if the page is PDFWriter object as a stamp, the final size will be that of the original page.
53
- def create_page(mediabox = [0, 0, 595.3, 841.9])
53
+ def create_page(mediabox = [0, 0, 612.0, 792.0])
54
54
  PDFWriter.new mediabox
55
55
  end
56
56
 
@@ -37,8 +37,8 @@ module CombinePDF
37
37
 
38
38
  # create a new PDFWriter object.
39
39
  #
40
- # mediabox:: the PDF page size in PDF points. defaults to [0, 0, 595.3, 841.9] (A4)
41
- def initialize(mediabox = [0, 0, 595.3, 841.9])
40
+ # mediabox:: the PDF page size in PDF points. defaults to [0, 0, 612.0, 792.0] (US Letter)
41
+ def initialize(mediabox = [0, 0, 612.0, 792.0])
42
42
  # indirect_reference_id, :indirect_generation_number
43
43
  @contents = ""
44
44
  @base_font_name = "Writer" + SecureRandom.hex(7) + "PDF"
@@ -406,6 +406,36 @@ module CombinePDF
406
406
  self
407
407
  end
408
408
 
409
+ # resizes the page relative to it's current viewport (either the cropbox or the mediabox), setting the new viewport to the requested size.
410
+ #
411
+ # accepts:
412
+ # new_size:: an Array with four elements: [X0, Y0, X_max, Y_max]. For example, A4: `[0, 0, 595, 842]`. It is important that the first two numbers are 0 unless a special effect is attempted. If the first two numbers change, the final result might not be the size requested, but the nearest possible transformation (calling the method again will allow a better resizing).
413
+ # conserve_aspect_ratio:: whether to keep the current content in the same aspect ratio or to allow streaching. Defaults to true - so that although the content is resized, it might not fill the new size completely.
414
+ def resize new_size = nil, conserve_aspect_ratio = true
415
+ return page_size unless new_size
416
+ c_mediabox = mediabox
417
+ c_cropbox = cropbox
418
+ c_size = c_cropbox || c_mediabox
419
+ x_ratio = 1.0 * (new_size[2]-new_size[0]) / (c_size[2])#-c_size[0])
420
+ y_ratio = 1.0 * (new_size[3]-new_size[1]) / (c_size[3])#-c_size[1])
421
+ x_move = new_size[0] - c_size[0]
422
+ y_move = new_size[1] - c_size[1]
423
+ puts "ctm will be: #{x_ratio.round(4).to_s} 0 0 #{y_ratio.round(4).to_s} #{x_move} #{y_move}"
424
+ self[:MediaBox] = [(c_mediabox[0] + x_move), (c_mediabox[1] + y_move), ((c_mediabox[2] * x_ratio) + x_move ), ((c_mediabox[3] * y_ratio) + y_move)]
425
+ self[:CropBox] = [(c_cropbox[0] + x_move), (c_cropbox[1] + y_move), ((c_cropbox[2] * x_ratio) + x_move), ((c_cropbox[3] * y_ratio) + y_move)] if c_cropbox
426
+ x_ratio = y_ratio = [x_ratio, y_ratio].min if conserve_aspect_ratio
427
+ # insert the rotation stream into the current content stream
428
+ # insert_content "q\n#{x_ratio.round(4).to_s} 0 0 #{y_ratio.round(4).to_s} 0 0 cm\n1 0 0 1 #{x_move} #{y_move} cm\n", 0
429
+ insert_content "q\n#{x_ratio.round(4).to_s} 0 0 #{y_ratio.round(4).to_s} #{x_move} #{y_move} cm\n", 0
430
+ # close the rotation stream
431
+ insert_content CONTENT_CONTAINER_END
432
+ # disconnect the content stream, so that future inserts aren't rotated
433
+ @contents = false #init_contents
434
+
435
+ # always return self, for chaining.
436
+ self
437
+ end
438
+
409
439
  # rotate the page 90 degrees counter clockwise
410
440
  def rotate_left
411
441
  self[:Rotate] = self[:Rotate].to_f + 90
@@ -432,7 +462,7 @@ module CombinePDF
432
462
  #
433
463
  # * Notice: a square page always returns the :portrait value and is ignored when trying to set the orientation.
434
464
  def orientation force = nil, clockwise = true
435
- a = self[:CropBox] || self[:MediaBox]
465
+ a = page_size
436
466
  unless force
437
467
  return (a[2] - a[0] > a[3] - a[1]) ? :landscape : :portrait
438
468
  end
@@ -114,7 +114,9 @@ module CombinePDF
114
114
  # adds a new page to the end of the PDF object.
115
115
  #
116
116
  # returns the new page object.
117
- def new_page(mediabox = [0, 0, 595.3, 841.9], location = -1)
117
+ #
118
+ # unless the media box is specified, it defaults to US Letter: [0, 0, 612.0, 792.0]
119
+ def new_page(mediabox = [0, 0, 612.0, 792.0], location = -1)
118
120
  p = PDFWriter.new(mediabox)
119
121
  insert(-1, p )
120
122
  p
@@ -1,4 +1,4 @@
1
1
  module CombinePDF
2
- VERSION = "0.2.3"
2
+ VERSION = "0.2.4"
3
3
  end
4
4
 
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'benchmark'
4
+ $LOAD_PATH.unshift File.expand_path(File.join('..', '..', 'lib'), __FILE__ )
5
+ require "combine_pdf"
6
+ require "bundler/setup"
7
+
8
+ # You can add fixtures and/or initialization code here to make experimenting
9
+ # with your gem easier. You can also use a different console, if you like.
10
+
11
+ # (If you use this, don't forget to add pry to your Gemfile!)
12
+ # require "pry"
13
+ # Pry.start
14
+
15
+ require "irb"
16
+ IRB.start
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: combine_pdf
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.2.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Boaz Segev
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-08-07 00:00:00.000000000 Z
11
+ date: 2015-08-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ruby-rc4
@@ -81,6 +81,7 @@ files:
81
81
  - lib/combine_pdf/pdf_public.rb
82
82
  - lib/combine_pdf/renderer.rb
83
83
  - lib/combine_pdf/version.rb
84
+ - test/console
84
85
  homepage: https://github.com/boazsegev/combine_pdf
85
86
  licenses:
86
87
  - MIT
@@ -105,4 +106,5 @@ rubygems_version: 2.4.7
105
106
  signing_key:
106
107
  specification_version: 4
107
108
  summary: Combine, stamp and watermark PDF files in pure Ruby.
108
- test_files: []
109
+ test_files:
110
+ - test/console