dragonfly_pdf 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 39808b6176e5805a9bc82d359f9d427bc42e476c
4
+ data.tar.gz: 60b458c14a7531d7a457f274b58ad4ce67a48ab8
5
+ SHA512:
6
+ metadata.gz: beee74901265a5e8f0c674f82d328140b5794385adb0f7d93514afc328df34a64e62210a86913ed3966c4fa0752e5e882c6b98c235e4b37ea371489516abac25
7
+ data.tar.gz: 88c5d02b9e3eea8c8a59d128b8e3b0381197abbb28b9102712bcfb91cdde6094373f77291160e11706546de3a8cfe256fa5e1474e94a048734dfa4f4d8611db5
data/.gitignore ADDED
@@ -0,0 +1,23 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
23
+ dragonfly.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in dragonfly_pdf.gemspec
4
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,8 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard :minitest do
5
+ watch(%r{^lib/(.+)\.rb$}) { |m| "test/#{m[1]}_test.rb" }
6
+ watch(%r{^test/.+_test\.rb$})
7
+ watch(%r{^test/test_helper\.rb$}) { 'test' }
8
+ end
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 Tomas Celizna
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,88 @@
1
+ # Dragonfly PDF
2
+
3
+ [![Build Status](https://travis-ci.org/tomasc/dragonfly_pdf.svg)](https://travis-ci.org/tomasc/dragonfly_pdf) [![Gem Version](https://badge.fury.io/rb/dragonfly_pdf.svg)](http://badge.fury.io/rb/dragonfly_pdf) [![Coverage Status](https://img.shields.io/coveralls/tomasc/dragonfly_pdf.svg)](https://coveralls.io/r/tomasc/dragonfly_pdf)
4
+
5
+ [Dragonfly](https://github.com/markevans/dragonfly) PDF analysers and processors.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'dragonfly_pdf'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install dragonfly_pdf
20
+
21
+ ## Plugin
22
+ The analyser and processors are added by configuring the plugin
23
+
24
+ ```ruby
25
+ Dragonfly.app.configure do
26
+ plugin :pdf
27
+ end
28
+ ```
29
+
30
+ ## Analysers
31
+
32
+ ### PDF properties
33
+
34
+ This processor has argument `spreads` (by default set to `false`). When `true`, the analyser assumes the PDF might contain 2 real pages per one PDF page (as when saving with the spreads option in InDesign) and recalculates the PDF properties accordingly (including situations when PDF starts or ends with single page).
35
+
36
+ ```ruby
37
+ pdf.pdf_properties(spreads=false)
38
+ ```
39
+
40
+ Returns Hash of PDF properties:
41
+
42
+ ```ruby
43
+ {
44
+ page_count: 4,
45
+ spread_count: 3,
46
+ page_numbers: [[1], [2, 3], [4]],
47
+ widths: [[210.0], [210.0, 210.0], [210.0]],
48
+ heights: [[297.0], [297.0, 297.0], [297.0]],
49
+ aspect_ratios: [[0.71], [0.71, 0.71], [0.71]],
50
+ info: { … }
51
+ }
52
+ ```
53
+
54
+ When the `spreads` argument is set to true, all page arrays (page_numbers, widths, heights, aspect_ratios) are two dimensional (as illustrated above), representing spreads and nested individual pages.
55
+
56
+ ## Processors
57
+
58
+ ### Page Thumb
59
+
60
+ Generates thumbnail of a specified page, in defined density (dpi) and format.
61
+
62
+ ```ruby
63
+ pdf.page_thumb(page_number=0, opts={})
64
+ ```
65
+
66
+ The available options and their default values are:
67
+
68
+ ```ruby
69
+ {
70
+ density: 600,
71
+ format: :png,
72
+ spreads: false
73
+ }
74
+ ```
75
+
76
+ Similarly to the `#pdf_properties`, the `#page_thumb` processor takes into account the `spreads` option.
77
+
78
+ ## TODO
79
+
80
+ * Add more tests for `#page_thumb`
81
+
82
+ ## Contributing
83
+
84
+ 1. Fork it ( https://github.com/tomasc/dragonfly_pdf/fork )
85
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
86
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
87
+ 4. Push to the branch (`git push origin my-new-feature`)
88
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs << 'test'
6
+ t.pattern = 'test/**/*_test.rb'
7
+ end
8
+
9
+ task default: :test
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'dragonfly_pdf/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "dragonfly_pdf"
8
+ spec.version = DragonflyPdf::VERSION
9
+ spec.authors = ["Tomas Celizna"]
10
+ spec.email = ["tomas.celizna@gmail.com"]
11
+ spec.summary = %q{Dragonfly PDF analysers and processors.}
12
+ spec.description = %q{Dragonfly PDF analysers and processors.}
13
+ spec.homepage = "https://github.com/tomasc/dragonfly_pdf"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "dragonfly", "~> 1.0"
22
+ spec.add_dependency "pdf-reader", "~> 1.3"
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.6"
25
+ spec.add_development_dependency "rake"
26
+ spec.add_development_dependency "guard"
27
+ spec.add_development_dependency "guard-minitest"
28
+ spec.add_development_dependency "minitest"
29
+ end
@@ -0,0 +1,148 @@
1
+ require 'pdf-reader'
2
+
3
+ module DragonflyPdf
4
+ module Analysers
5
+ class PdfProperties
6
+
7
+ def call content, spreads=false
8
+ pdf = PDF::Reader.new(content.file)
9
+
10
+ {
11
+ page_count: page_count(pdf, spreads),
12
+ spread_count: spread_count(pdf, spreads),
13
+ page_numbers: page_numbers(pdf, spreads),
14
+ widths: widths(pdf, spreads),
15
+ heights: heights(pdf, spreads),
16
+ aspect_ratios: aspect_ratios(pdf, spreads),
17
+ info: pdf.info
18
+ }
19
+ end
20
+
21
+ private # =============================================================
22
+
23
+ def widths pdf, spreads
24
+ pdf_page_widths = pdf.pages.map do |page|
25
+ media_box = page.attributes[:MediaBox]
26
+ pt2mm(media_box[2] - media_box[0]).round(2)
27
+ end
28
+
29
+ return pdf_page_widths unless spreads
30
+
31
+ res = []
32
+ i = 0
33
+ spread = []
34
+ page_numbers(pdf, spreads).each do |s|
35
+ if s.count == 1
36
+ spread << pdf_page_widths[i]
37
+ else
38
+ spread << pdf_page_widths[i]/2
39
+ spread << pdf_page_widths[i]/2
40
+ end
41
+ res << spread
42
+ spread = []
43
+ i = i+1
44
+ end
45
+ res
46
+ end
47
+
48
+ # ---------------------------------------------------------------------
49
+
50
+ def heights pdf, spreads
51
+ pdf_page_heights = pdf.pages.map do |page|
52
+ media_box = page.attributes[:MediaBox]
53
+ pt2mm(media_box[3] - media_box[1]).round(2)
54
+ end
55
+
56
+ return pdf_page_heights unless spreads
57
+
58
+ res = []
59
+ i = 0
60
+ spread = []
61
+ page_numbers(pdf, spreads).each do |s|
62
+ if s.count == 1
63
+ spread << pdf_page_heights[i]
64
+ else
65
+ spread << pdf_page_heights[i]
66
+ spread << pdf_page_heights[i]
67
+ end
68
+ res << spread
69
+ spread = []
70
+ i = i+1
71
+ end
72
+ res
73
+ end
74
+
75
+ # ---------------------------------------------------------------------
76
+
77
+ def aspect_ratios pdf, spreads
78
+ pdf_aspect_ratios = widths(pdf, false).zip(heights(pdf, false)).map do |width, height|
79
+ (width / height)
80
+ end
81
+
82
+ return pdf_aspect_ratios unless spreads
83
+
84
+ res = []
85
+ i = 0
86
+ spread = []
87
+ page_numbers(pdf, spreads).each do |s|
88
+ if s.count == 1
89
+ spread << pdf_aspect_ratios[i]
90
+ else
91
+ spread << pdf_aspect_ratios[i]/2
92
+ spread << pdf_aspect_ratios[i]/2
93
+ end
94
+ res << spread
95
+ spread = []
96
+ i = i+1
97
+ end
98
+ res
99
+ end
100
+
101
+ # ---------------------------------------------------------------------
102
+
103
+ def page_numbers pdf, spreads
104
+ return pdf.pages.collect { |p| p.number } unless spreads
105
+
106
+ page_widths = widths(pdf, false)
107
+ single_page_width = page_widths.uniq.count == 1 ? -9999999 : widths(pdf, false).min
108
+
109
+ i = 1
110
+ res = []
111
+ spread = []
112
+
113
+ page_widths.each do |page_width|
114
+ if page_width > single_page_width
115
+ spread << i
116
+ i = i+1
117
+ spread << i
118
+ else
119
+ spread << i
120
+ end
121
+ res << spread
122
+ spread = []
123
+ i = i+1
124
+ end
125
+
126
+ res
127
+ end
128
+
129
+ # ---------------------------------------------------------------------
130
+
131
+ def page_count pdf, spreads
132
+ page_numbers(pdf, spreads).flatten.count
133
+ end
134
+
135
+ def spread_count pdf, spreads
136
+ return 0 unless spreads
137
+ page_numbers(pdf, spreads).count
138
+ end
139
+
140
+ # =====================================================================
141
+
142
+ def pt2mm pt
143
+ (pt / 72.0) * 25.4
144
+ end
145
+
146
+ end
147
+ end
148
+ end
@@ -0,0 +1,46 @@
1
+ require 'dragonfly_pdf/analysers/pdf_properties'
2
+ require 'dragonfly_pdf/processors/page_thumb'
3
+
4
+ module DragonflyPdf
5
+ class Plugin
6
+
7
+ def call app, opts={}
8
+ app.add_analyser :pdf_properties, DragonflyPdf::Analysers::PdfProperties.new
9
+
10
+ app.add_analyser :page_count do |content|
11
+ content.analyse(:pdf_properties)[:page_count]
12
+ end
13
+
14
+ app.add_analyser :spread_count do |content|
15
+ content.analyse(:pdf_properties)[:spread_count]
16
+ end
17
+
18
+ app.add_analyser :page_numbers do |content|
19
+ content.analyse(:pdf_properties)[:page_numbers]
20
+ end
21
+
22
+ app.add_analyser :widths do |content|
23
+ content.analyse(:pdf_properties)[:widths]
24
+ end
25
+
26
+ app.add_analyser :heights do |content|
27
+ content.analyse(:pdf_properties)[:heights]
28
+ end
29
+
30
+ app.add_analyser :aspect_ratios do |content|
31
+ attrs = content.analyse(:pdf_properties)[:aspect_ratios]
32
+ end
33
+
34
+ app.add_analyser :info do |content|
35
+ attrs = content.analyse(:pdf_properties)[:info]
36
+ end
37
+
38
+ # ---------------------------------------------------------------------
39
+
40
+ app.add_processor :page_thumb, DragonflyPdf::Processors::PageThumb.new
41
+ end
42
+
43
+ end
44
+ end
45
+
46
+ Dragonfly::App.register_plugin(:pdf) { DragonflyPdf::Plugin.new }
@@ -0,0 +1,62 @@
1
+ require_relative '../analysers/pdf_properties'
2
+
3
+ module DragonflyPdf
4
+ module Processors
5
+ class PageThumb
6
+
7
+ class UnsupportedFormat < RuntimeError; end
8
+
9
+ def call content, page_number=0, opts={}
10
+ format = opts.fetch(:format, :png)
11
+ # raise UnsupportedFormat unless %w(tif jpg jpeg png gif).include?(format.to_s)
12
+
13
+ density = opts.fetch(:density, 600)
14
+ spreads = opts.fetch(:spreads, false)
15
+
16
+ args = "-alpha deactivate -background white -colorspace sRGB -density #{density}x#{density} -define pdf:use-cropbox=true -define pdf:use-trimbox=true"
17
+ crop_args = ''
18
+
19
+ pdf_properties = DragonflyPdf::Analysers::PdfProperties.new.call(content, spreads)
20
+
21
+ if spreads
22
+ spread = pdf_properties[:page_numbers].detect{ |s| s.include?(page_number) }
23
+ spread_number = pdf_properties[:page_numbers].index(spread)
24
+ spread_side = spread.index(page_number)
25
+ page_to_delete = 1-spread_side
26
+
27
+ pdf_page_number = spread_number
28
+ crop_args = "-crop 50%x100% -delete #{page_to_delete}"
29
+ else
30
+ pdf_page_number = page_number
31
+ end
32
+
33
+ content.shell_update(ext: format) do |old_path, new_path|
34
+ "#{convert_command} #{args} #{crop_args} #{old_path}[#{pdf_page_number}] #{new_path}"
35
+ end
36
+
37
+ content.meta['format'] = format.to_s
38
+ content.ext = format
39
+ end
40
+
41
+ def update_url attrs, args='', opts={}
42
+ format = opts['format']
43
+ attrs.ext = format if format
44
+ end
45
+
46
+ private # =============================================================
47
+
48
+ def convert_command
49
+ 'convert'
50
+ end
51
+
52
+ def pdf_page_number page_number, spreads
53
+ return 1
54
+ end
55
+
56
+ def pdf_crop_args page_number, spreads
57
+ return
58
+ end
59
+
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,3 @@
1
+ module DragonflyPdf
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,3 @@
1
+ require "dragonfly_pdf/plugin"
2
+
3
+ require "dragonfly_pdf/version"
Binary file
Binary file
Binary file
Binary file
@@ -0,0 +1,238 @@
1
+ require 'test_helper'
2
+
3
+ module DragonflyPdf
4
+ module Analysers
5
+ describe PdfProperties do
6
+
7
+ let(:app) { test_app.configure_with(:pdf) }
8
+ let(:analyser) { DragonflyPdf::Analysers::PdfProperties.new }
9
+
10
+ let(:single_pages) { app.fetch_file(SAMPLES_DIR.join('sample_pages.pdf')) }
11
+ let(:spreads) { app.fetch_file(SAMPLES_DIR.join('sample_spreads.pdf')) }
12
+ let(:spreads_back) { app.fetch_file(SAMPLES_DIR.join('sample_spreads_back.pdf')) }
13
+ let(:spreads_cover) { app.fetch_file(SAMPLES_DIR.join('sample_spreads_cover.pdf')) }
14
+ let(:spreads_cover_back) { app.fetch_file(SAMPLES_DIR.join('sample_spreads_cover_back.pdf')) }
15
+
16
+ describe 'call' do
17
+ let(:pdf_properties) { analyser.call(single_pages) }
18
+
19
+ it 'returns Hash' do
20
+ pdf_properties.must_be_kind_of Hash
21
+ end
22
+ end
23
+
24
+ # ---------------------------------------------------------------------
25
+
26
+ describe '#page_numbers' do
27
+ describe 'for single page PDF' do
28
+ it 'returns one-dimensional array' do
29
+ analyser.call(single_pages)[:page_numbers].must_equal [1,2,3,4,5,6,7,8,9,10]
30
+ end
31
+ end
32
+
33
+ describe 'for PDF with spreads' do
34
+ it 'returns two-dimensional array' do
35
+ analyser.call(spreads, true)[:page_numbers].must_equal [[1,2],[3,4],[5,6],[7,8]]
36
+ end
37
+ end
38
+
39
+ describe 'for PDF with spreads and a cover' do
40
+ it 'returns two-dimensional array' do
41
+ analyser.call(spreads_cover, true)[:page_numbers].must_equal [[1],[2,3],[4,5],[6,7],[8,9]]
42
+ end
43
+ end
44
+
45
+ describe 'for PDF with spreads and a back cover' do
46
+ it 'returns two-dimensional array' do
47
+ analyser.call(spreads_back, true)[:page_numbers].must_equal [[1,2],[3,4],[5,6],[7,8],[9]]
48
+ end
49
+ end
50
+
51
+ describe 'for PDF with spreads and a front and a back cover' do
52
+ it 'returns two-dimensional array' do
53
+ analyser.call(spreads_cover_back, true)[:page_numbers].must_equal [[1],[2,3],[4,5],[6,7],[8,9],[10]]
54
+ end
55
+ end
56
+ end
57
+
58
+ # ---------------------------------------------------------------------
59
+
60
+ describe '#page_count' do
61
+ describe 'for single page PDF' do
62
+ it 'returns correct page count' do
63
+ analyser.call(single_pages)[:page_count].must_equal 10
64
+ end
65
+ end
66
+
67
+ describe 'for PDF with spreads' do
68
+ it 'returns correct page count' do
69
+ analyser.call(spreads, true)[:page_count].must_equal 8
70
+ end
71
+ end
72
+
73
+ describe 'for PDF with spreads and a cover' do
74
+ it 'returns correct page count' do
75
+ analyser.call(spreads_cover, true)[:page_count].must_equal 9
76
+ end
77
+ end
78
+
79
+ describe 'for PDF with spreads and a back cover' do
80
+ it 'returns correct page count' do
81
+ analyser.call(spreads_back, true)[:page_count].must_equal 9
82
+ end
83
+ end
84
+
85
+ describe 'for PDF with spreads and a front and a back cover' do
86
+ it 'returns correct page count' do
87
+ analyser.call(spreads_cover_back, true)[:page_count].must_equal 10
88
+ end
89
+ end
90
+ end
91
+
92
+ # ---------------------------------------------------------------------
93
+
94
+ describe '#spread_count' do
95
+ describe 'for single page PDF' do
96
+ it 'returns correct page count' do
97
+ analyser.call(single_pages)[:spread_count].must_equal 0
98
+ end
99
+ end
100
+
101
+ describe 'for PDF with spreads' do
102
+ it 'returns correct page count' do
103
+ analyser.call(spreads, true)[:spread_count].must_equal 4
104
+ end
105
+ end
106
+
107
+ describe 'for PDF with spreads and a cover' do
108
+ it 'returns correct page count' do
109
+ analyser.call(spreads_cover, true)[:spread_count].must_equal 5
110
+ end
111
+ end
112
+
113
+ describe 'for PDF with spreads and a back cover' do
114
+ it 'returns correct page count' do
115
+ analyser.call(spreads_back, true)[:spread_count].must_equal 5
116
+ end
117
+ end
118
+
119
+ describe 'for PDF with spreads and a front and a back cover' do
120
+ it 'returns correct page count' do
121
+ analyser.call(spreads_cover_back, true)[:spread_count].must_equal 6
122
+ end
123
+ end
124
+ end
125
+
126
+ # ---------------------------------------------------------------------
127
+
128
+ describe '#widths' do
129
+ describe 'for single page PDF' do
130
+ it 'returns widths' do
131
+ analyser.call(single_pages)[:widths].must_equal [210.0, 210.0, 210.0, 210.0, 210.0, 210.0, 210.0, 210.0, 210.0, 210.0]
132
+ end
133
+ end
134
+
135
+ describe 'for PDF with spreads' do
136
+ it 'returns widths' do
137
+ analyser.call(spreads, true)[:widths].must_equal [[210.0, 210.0], [210.0, 210.0], [210.0, 210.0], [210.0, 210.0]]
138
+ end
139
+ end
140
+
141
+ describe 'for PDF with spreads and a cover' do
142
+ it 'returns correct widths' do
143
+ analyser.call(spreads_cover, true)[:widths].must_equal [[210.0], [210.0, 210.0], [210.0, 210.0], [210.0, 210.0], [210.0, 210.0]]
144
+ end
145
+ end
146
+
147
+ describe 'for PDF with spreads and a back cover' do
148
+ it 'returns correct widths' do
149
+ analyser.call(spreads_back, true)[:widths].must_equal [[210.0, 210.0], [210.0, 210.0], [210.0, 210.0], [210.0, 210.0], [210.0]]
150
+ end
151
+ end
152
+
153
+ describe 'for PDF with spreads and a front and a back cover' do
154
+ it 'returns correct widths' do
155
+ analyser.call(spreads_cover_back, true)[:widths].must_equal [[210.0], [210.0, 210.0], [210.0, 210.0], [210.0, 210.0], [210.0, 210.0], [210.0]]
156
+ end
157
+ end
158
+ end
159
+
160
+ # ---------------------------------------------------------------------
161
+
162
+ describe '#heights' do
163
+ describe 'for single page PDF' do
164
+ it 'returns heights' do
165
+ analyser.call(single_pages)[:heights].must_equal [297.0, 297.0, 297.0, 297.0, 297.0, 297.0, 297.0, 297.0, 297.0, 297.0]
166
+ end
167
+ end
168
+
169
+ describe 'for PDF with spreads' do
170
+ it 'returns heights' do
171
+ analyser.call(spreads, true)[:heights].must_equal [[297.0, 297.0], [297.0, 297.0], [297.0, 297.0], [297.0, 297.0]]
172
+ end
173
+ end
174
+
175
+ describe 'for PDF with spreads and a cover' do
176
+ it 'returns correct heights' do
177
+ analyser.call(spreads_cover, true)[:heights].must_equal [[297.0], [297.0, 297.0], [297.0, 297.0], [297.0, 297.0], [297.0, 297.0]]
178
+ end
179
+ end
180
+
181
+ describe 'for PDF with spreads and a back cover' do
182
+ it 'returns correct heights' do
183
+ analyser.call(spreads_back, true)[:heights].must_equal [[297.0, 297.0], [297.0, 297.0], [297.0, 297.0], [297.0, 297.0], [297.0]]
184
+ end
185
+ end
186
+
187
+ describe 'for PDF with spreads and a front and a back cover' do
188
+ it 'returns correct heights' do
189
+ analyser.call(spreads_cover_back, true)[:heights].must_equal [[297.0], [297.0, 297.0], [297.0, 297.0], [297.0, 297.0], [297.0, 297.0], [297.0]]
190
+ end
191
+ end
192
+ end
193
+
194
+ # ---------------------------------------------------------------------
195
+
196
+ describe '#aspect_ratios' do
197
+ describe 'for single page PDF' do
198
+ it 'returns aspect ratios' do
199
+ analyser.call(single_pages)[:aspect_ratios].map{ |i| i.round(2) }.must_equal [0.71, 0.71, 0.71, 0.71, 0.71, 0.71, 0.71, 0.71, 0.71, 0.71]
200
+ end
201
+ end
202
+
203
+ describe 'for PDF with spreads' do
204
+ it 'returns aspect ratios' do
205
+ analyser.call(spreads, true)[:aspect_ratios].map{ |i| i.is_a?(Array) ? i.map{ |j| j.round(2) } : i.round(2) }.must_equal [[0.71, 0.71], [0.71, 0.71], [0.71, 0.71], [0.71, 0.71]]
206
+ end
207
+ end
208
+
209
+ describe 'for PDF with spreads and a cover' do
210
+ it 'returns correct aspect ratios' do
211
+ analyser.call(spreads_cover, true)[:aspect_ratios].map{ |i| i.is_a?(Array) ? i.map{ |j| j.round(2) } : i.round(2) }.must_equal [[0.71], [0.71, 0.71], [0.71, 0.71], [0.71, 0.71], [0.71, 0.71]]
212
+ end
213
+ end
214
+
215
+ describe 'for PDF with spreads and a back cover' do
216
+ it 'returns correct aspect ratios' do
217
+ analyser.call(spreads_back, true)[:aspect_ratios].map{ |i| i.is_a?(Array) ? i.map{ |j| j.round(2) } : i.round(2) }.must_equal [[0.71, 0.71], [0.71, 0.71], [0.71, 0.71], [0.71, 0.71], [0.71]]
218
+ end
219
+ end
220
+
221
+ describe 'for PDF with spreads and a front and a back cover' do
222
+ it 'returns correct aspect ratios' do
223
+ analyser.call(spreads_cover_back, true)[:aspect_ratios].map{ |i| i.is_a?(Array) ? i.map{ |j| j.round(2) } : i.round(2) }.must_equal [[0.71], [0.71, 0.71], [0.71, 0.71], [0.71, 0.71], [0.71, 0.71], [0.71]]
224
+ end
225
+ end
226
+ end
227
+
228
+ # ---------------------------------------------------------------------
229
+
230
+ describe '#info' do
231
+ it 'returns Hash' do
232
+ analyser.call(single_pages)[:info].must_be_kind_of Hash
233
+ end
234
+ end
235
+
236
+ end
237
+ end
238
+ end
@@ -0,0 +1,36 @@
1
+ require 'test_helper'
2
+
3
+ module DragonflyPdf
4
+ describe Plugin do
5
+
6
+ let(:app) { test_app.configure_with(:pdf) }
7
+ let(:pdf) { app.fetch_file(SAMPLES_DIR.join('sample_pages.pdf')) }
8
+
9
+ # ---------------------------------------------------------------------
10
+
11
+ describe 'analysers' do
12
+ it 'adds #page_count' do
13
+ pdf.must_respond_to :page_count
14
+ end
15
+ it 'adds #spread_count' do
16
+ pdf.must_respond_to :spread_count
17
+ end
18
+ it 'adds #page_numbers' do
19
+ pdf.must_respond_to :page_numbers
20
+ end
21
+ it 'adds #widths' do
22
+ pdf.must_respond_to :widths
23
+ end
24
+ it 'adds #heights' do
25
+ pdf.must_respond_to :heights
26
+ end
27
+ it 'adds #aspect_ratios' do
28
+ pdf.must_respond_to :aspect_ratios
29
+ end
30
+ it 'adds #info' do
31
+ pdf.must_respond_to :info
32
+ end
33
+ end
34
+
35
+ end
36
+ end
@@ -0,0 +1,67 @@
1
+ require 'test_helper'
2
+ require 'pdf-reader'
3
+
4
+ module DragonflyPdf
5
+ module Processors
6
+ describe PageThumb do
7
+
8
+ let(:app) { test_app.configure_with(:pdf) }
9
+ let(:processor) { DragonflyPdf::Processors::PageThumb.new }
10
+
11
+ let(:single_pages) { Dragonfly::Content.new(app, SAMPLES_DIR.join('sample_pages.pdf')) }
12
+ let(:spreads) { Dragonfly::Content.new(app, SAMPLES_DIR.join('sample_spreads.pdf')) }
13
+ let(:spreads_back) { Dragonfly::Content.new(app, SAMPLES_DIR.join('sample_spreads_back.pdf')) }
14
+ let(:spreads_cover) { Dragonfly::Content.new(app, SAMPLES_DIR.join('sample_spreads_cover.pdf')) }
15
+ let(:spreads_cover_back) { Dragonfly::Content.new(app, SAMPLES_DIR.join('sample_spreads_cover_back.pdf')) }
16
+
17
+ it 'returns PNG by default' do
18
+ processor.call(single_pages, 0, density: 72)
19
+ get_mime_type(single_pages.path).must_include "image/png"
20
+ end
21
+
22
+ # ---------------------------------------------------------------------
23
+
24
+ describe 'single pages' do
25
+ it 'renders page' do
26
+ skip
27
+ processor.call(single_pages, 1, density: 72)
28
+ end
29
+ end
30
+
31
+ describe 'spreads' do
32
+ it 'recalculates the page number correctly' do
33
+ skip
34
+ processor.call(spreads, 3, density: 72, spreads: true)
35
+ end
36
+ end
37
+
38
+ describe 'spreads_back' do
39
+ it 'recalculates the page number correctly' do
40
+ skip
41
+ processor.call(spreads_back, 3, density: 72, spreads: true)
42
+ end
43
+ end
44
+
45
+ describe 'spreads_cover' do
46
+ it 'recalculates the page number correctly' do
47
+ skip
48
+ processor.call(spreads_cover, 3, density: 72, spreads: true)
49
+ end
50
+ end
51
+
52
+ describe 'spreads_cover_back' do
53
+ it 'recalculates the page number correctly' do
54
+ skip
55
+ processor.call(spreads_cover_back, 3, density: 72, spreads: true)
56
+ end
57
+ end
58
+
59
+ # ---------------------------------------------------------------------
60
+
61
+ def get_mime_type file_path
62
+ `file --mime-type #{file_path}`.gsub(/\n/, "")
63
+ end
64
+
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,21 @@
1
+ require 'bundler/setup'
2
+
3
+ require 'minitest'
4
+ require 'minitest/autorun'
5
+ require 'minitest/spec'
6
+
7
+ require 'dragonfly'
8
+ require 'dragonfly_pdf'
9
+
10
+ # ---------------------------------------------------------------------
11
+
12
+ SAMPLES_DIR = Pathname.new(File.expand_path('../../samples', __FILE__))
13
+
14
+ # ---------------------------------------------------------------------
15
+
16
+ def test_app name=nil
17
+ app = Dragonfly::App.instance(name)
18
+ app.datastore = Dragonfly::MemoryDataStore.new
19
+ app.secret = "test secret"
20
+ app
21
+ end
metadata ADDED
@@ -0,0 +1,167 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dragonfly_pdf
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Tomas Celizna
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: dragonfly
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: pdf-reader
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.3'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.6'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.6'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: guard
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: guard-minitest
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: minitest
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: Dragonfly PDF analysers and processors.
112
+ email:
113
+ - tomas.celizna@gmail.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - Gemfile
120
+ - Guardfile
121
+ - LICENSE
122
+ - README.md
123
+ - Rakefile
124
+ - dragonfly_pdf.gemspec
125
+ - lib/dragonfly_pdf.rb
126
+ - lib/dragonfly_pdf/analysers/pdf_properties.rb
127
+ - lib/dragonfly_pdf/plugin.rb
128
+ - lib/dragonfly_pdf/processors/page_thumb.rb
129
+ - lib/dragonfly_pdf/version.rb
130
+ - samples/sample_pages.pdf
131
+ - samples/sample_spreads.pdf
132
+ - samples/sample_spreads_back.pdf
133
+ - samples/sample_spreads_cover.pdf
134
+ - samples/sample_spreads_cover_back.pdf
135
+ - test/dragonfly_pdf/analysers/pdf_properties_test.rb
136
+ - test/dragonfly_pdf/plugin_test.rb
137
+ - test/dragonfly_pdf/processors/page_thumb_test.rb
138
+ - test/test_helper.rb
139
+ homepage: https://github.com/tomasc/dragonfly_pdf
140
+ licenses:
141
+ - MIT
142
+ metadata: {}
143
+ post_install_message:
144
+ rdoc_options: []
145
+ require_paths:
146
+ - lib
147
+ required_ruby_version: !ruby/object:Gem::Requirement
148
+ requirements:
149
+ - - ">="
150
+ - !ruby/object:Gem::Version
151
+ version: '0'
152
+ required_rubygems_version: !ruby/object:Gem::Requirement
153
+ requirements:
154
+ - - ">="
155
+ - !ruby/object:Gem::Version
156
+ version: '0'
157
+ requirements: []
158
+ rubyforge_project:
159
+ rubygems_version: 2.2.2
160
+ signing_key:
161
+ specification_version: 4
162
+ summary: Dragonfly PDF analysers and processors.
163
+ test_files:
164
+ - test/dragonfly_pdf/analysers/pdf_properties_test.rb
165
+ - test/dragonfly_pdf/plugin_test.rb
166
+ - test/dragonfly_pdf/processors/page_thumb_test.rb
167
+ - test/test_helper.rb