pdf_thumbnailer 1.0.0 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ead9886da00530609620429ca66638ab420a96a5
4
- data.tar.gz: cc91f7aa92555b4e9297ac343213ffd6f2218105
3
+ metadata.gz: 47f518a6745d7a05c55d8ff96461bfb2ad35eee0
4
+ data.tar.gz: feb3e239262d77df2cbcdddcf9546255bbca77ab
5
5
  SHA512:
6
- metadata.gz: 09b9c9aed5769681c3c597040184be341c892a0cf5d1979af07558fa1e546ce4727503cf557b2755e6db8088cb2189042a7c6ba38e826261b0e307529fbb6282
7
- data.tar.gz: ce8c4db7ece241943a5278a2811dd7b6c4b73ffa8fd5e1b533bbf64e4a1ddafae855c54b4344e751d9c77d4461fcb76e4f1deadb78b63304bd52cf2e4fb06a08
6
+ metadata.gz: b35b036a58ff5d89d6b76f281193c1007f7664873e9854219ab2e026e3b028196296f9d791754d85892a21ca1fd8cd681712545bce21dda9cdaadf0a04aee533
7
+ data.tar.gz: d42b8bcbd3a15a3dc7a0b6391e6c23d60cc60e4755d30ced62de72ff534a60e488346c9060b438d00620c6884268772fca72736ece7f726c8932c51301343121
data/README.md CHANGED
@@ -27,7 +27,7 @@ First, configure `PdfThumbs`:
27
27
 
28
28
  ```ruby
29
29
  PdfThumbs.configure(
30
- pdf_dir: '/some/path/with/pdf/files', # required
30
+ pdf_dir: '/some/path/with/pdf/files',
31
31
  img_dir: '/where/to/save/page/images', # required
32
32
  thumb_sizes: [1000, 500, 100] # max pixel lengths for the long side (height or width) of the page images
33
33
  )
@@ -43,6 +43,16 @@ end
43
43
 
44
44
  `thumbnail!` optionally accepts a code block with one argument. This argument (`thumb_dir` in the above example) will be a string representing the path of the newly created directory containing page images for a particular pdf. The folder structure `img_dir` will be identical to that of `pdf_dir` but for every `.pdf` file there will instead be a directory of the same name containing `.png` iamges.
45
45
 
46
+ Alternatively a single pdf file can have its pages converted to images using the `thumbnail_single!` method:
47
+
48
+ ```ruby
49
+ PdfThumbs.configure img_dir: '/where/to/save/page/images', thumb_sizes: 500
50
+
51
+ thumb_dir = PdfThumbs.thumbnail_single! '/pdf/root/dir', 'relative/path/to/My File.pdf'
52
+
53
+ puts "Page thumbnails are located in: #{thumb_dir}"
54
+ ```
55
+
46
56
 
47
57
  License
48
58
  -----------------------------
@@ -1,34 +1,31 @@
1
1
  class PdfThumbs
2
2
  def self.configure **opts
3
- [:pdf_dir, :img_dir, :thumb_sizes].each do |required_opt|
3
+ # [:pdf_dir, :img_dir, :thumb_sizes].each do |required_opt|
4
+ [:img_dir, :thumb_sizes].each do |required_opt|
4
5
  raise ArgumentError, "Missing required config option: :#{required_opt}" unless opts[required_opt]
5
6
  end
6
- @@pdf_dir = File.join(opts[:pdf_dir], '') # adds trailing '/' if it doesn't already have one
7
7
  @@img_dir = File.join(opts[:img_dir], '') # adds trailing '/' if it doesn't already have one
8
+ @@pdf_dir = File.join(opts[:pdf_dir], '') if opts[:pdf_dir]# adds trailing '/' if it doesn't already have one
8
9
  @@thumb_sizes = opts[:thumb_sizes].is_a?(Fixnum) ? [opts[:thumb_sizes]] : opts[:thumb_sizes]
9
10
  end
10
11
 
11
12
  def self.thumbnail!
12
13
  Dir.glob(File.join(@@pdf_dir, '**', '*.pdf')) do |pdf|
13
- img_dir = clear_img_dir pdf
14
-
15
- @@thumb_sizes.each do |thumb_size|
16
- `pdftoppm -png -scale-to #{thumb_size} "#{pdf}" "#{img_dir}"`
17
-
18
- Dir.glob(File.join(img_dir, '-*.png')) do |img_path|
19
- page_num = File.basename(img_path, '.png').split('-')[-1].to_i
20
- File.rename(img_path, File.join(img_dir, "#{thumb_size}_#{page_num}.png"))
21
- end
22
- end
14
+ img_dir = thumbnail_pdf @@pdf_dir, pdf.gsub(@@pdf_dir, '')
23
15
  yield(img_dir) if block_given?
24
16
  end
25
17
  end
26
18
 
19
+ # TODO: add spec and document in README.md
20
+ def self.thumbnail_single! pdf_dir, relative_pdf_path
21
+ thumbnail_pdf pdf_dir, relative_pdf_path
22
+ end
23
+
27
24
  private
28
25
 
29
- def self.clear_img_dir pdf
30
- relative_path = File.dirname pdf.gsub(@@pdf_dir, '')
31
- relative_img_path = relative_path == '.' ? File.basename(pdf) : File.join(relative_path, File.basename(pdf))
26
+ def self.clear_img_dir relative_pdf_path
27
+ relative_path = File.dirname relative_pdf_path
28
+ relative_img_path = relative_path == '.' ? File.basename(relative_pdf_path) : File.join(relative_path, File.basename(relative_pdf_path))
32
29
  img_dir = File.join(@@img_dir, relative_img_path, '')
33
30
 
34
31
  FileUtils.rm_rf(img_dir) if File.directory?(img_dir)
@@ -36,4 +33,19 @@ class PdfThumbs
36
33
 
37
34
  img_dir
38
35
  end
36
+
37
+ def self.thumbnail_pdf pdf_dir, relative_pdf_path
38
+ img_dir = clear_img_dir relative_pdf_path
39
+
40
+ @@thumb_sizes.each do |thumb_size|
41
+ `pdftoppm -png -scale-to #{thumb_size} "#{File.join(pdf_dir, relative_pdf_path)}" "#{img_dir}"`
42
+
43
+ Dir.glob(File.join(img_dir, '-*.png')) do |img_path|
44
+ page_num = File.basename(img_path, '.png').split('-')[-1].to_i
45
+ File.rename(img_path, File.join(img_dir, "#{thumb_size}_#{page_num}.png"))
46
+ end
47
+ end
48
+
49
+ img_dir
50
+ end
39
51
  end
@@ -1,3 +1,3 @@
1
1
  module PdfThumbnailer
2
- VERSION = '1.0.0'
2
+ VERSION = '1.1.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pdf_thumbnailer
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sean Huber
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-08-24 00:00:00.000000000 Z
11
+ date: 2016-08-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler