pdf_cover 0.1.4 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/pdf_cover/converter.rb +29 -36
- data/lib/pdf_cover/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 50f84966f940de9a7a2c76e5befb83f5365b3e94
|
4
|
+
data.tar.gz: 4813cc9fbec831e8951fb6d11cf74ab172372f17
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a10fe4bba5600588ad392490a034b97c40e375e86d8f9818562c8c89544b4499ac0c48721a6b72a9407dee97090b7c877a4183cdd7f66b97a6dda4eda5022f7b
|
7
|
+
data.tar.gz: 2d308c23e2cdc4f5f12892d03fdacac234a84c3103171521d29ebf206019e335466263b670b1c9a92ffc34b724544e731fb0650bb091926ec6d30e0980e65467
|
data/lib/pdf_cover/converter.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require "open3"
|
2
|
+
|
1
3
|
module PdfCover
|
2
4
|
class Converter
|
3
5
|
# NOTE: Update the generate_jpegs.sh script when changing these values
|
@@ -5,6 +7,15 @@ module PdfCover
|
|
5
7
|
DEFAULT_QUALITY = 85
|
6
8
|
DEFAULT_RESOLUTION = 300
|
7
9
|
|
10
|
+
COMMAND_EXECUTION_SUCCESS_CODE = 0
|
11
|
+
COMMAND_NOT_FOUND_CODE = 127 # @see http://www.tldp.org/LDP/abs/html/exitcodes.html
|
12
|
+
|
13
|
+
class CommandFailedError < StandardError
|
14
|
+
def initialize(stdout_str, stderr_str)
|
15
|
+
super("PDF conversion failed:\nSTDOUT: #{stdout_str}\nSTDERR: #{stderr_str}")
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
8
19
|
class CommandNotFoundError < StandardError
|
9
20
|
def initialize
|
10
21
|
super("Could not run the `gs` command. Make sure that GhostScript is in your PATH.")
|
@@ -23,15 +34,19 @@ module PdfCover
|
|
23
34
|
end
|
24
35
|
|
25
36
|
# @raises PdfCover::Converter::CommandNotFoundError if GhostScript is not found
|
37
|
+
# @raises PdfCover::Converter::CommandFailedError if GhostScript returns a non-zero status
|
26
38
|
def converted_file
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
when
|
33
|
-
|
39
|
+
parameters = build_parameters(file_path, device)
|
40
|
+
stdout_str, stderr_str, status = execute_command("gs #{parameters}")
|
41
|
+
|
42
|
+
case status
|
43
|
+
when COMMAND_EXECUTION_SUCCESS_CODE then destination_file
|
44
|
+
when COMMAND_NOT_FOUND_CODE then fail CommandNotFoundError
|
45
|
+
else fail CommandFailedError.new(stdout_str, stderr_str)
|
34
46
|
end
|
47
|
+
rescue => e
|
48
|
+
destination_file.close
|
49
|
+
raise e
|
35
50
|
end
|
36
51
|
|
37
52
|
private
|
@@ -40,8 +55,8 @@ module PdfCover
|
|
40
55
|
@basename ||= File.basename(@file.path, File.extname(@file.path))
|
41
56
|
end
|
42
57
|
|
43
|
-
def build_parameters(source, device
|
44
|
-
%W(-sOutputFile='#{
|
58
|
+
def build_parameters(source, device)
|
59
|
+
%W(-sOutputFile='#{destination_path}' -dNOPAUSE
|
45
60
|
-sDEVICE='#{device}' -dJPEGQ=#{@quality}
|
46
61
|
-dFirstPage=1 -dLastPage=1
|
47
62
|
-r#{@resolution} -q '#{source}'
|
@@ -49,29 +64,22 @@ module PdfCover
|
|
49
64
|
).join(" ")
|
50
65
|
end
|
51
66
|
|
52
|
-
def convert_file
|
53
|
-
parameters = build_parameters(file_path, device, File.expand_path(destination_file.path))
|
54
|
-
result = execute_command("gs #{parameters}")
|
55
|
-
|
56
|
-
if result
|
57
|
-
:ok
|
58
|
-
else
|
59
|
-
failed_result(result)
|
60
|
-
end
|
61
|
-
end
|
62
|
-
|
63
67
|
def destination_file
|
64
68
|
@destination_file ||= Tempfile.new([basename, ".#{@format}"]).tap do |file|
|
65
69
|
file.binmode
|
66
70
|
end
|
67
71
|
end
|
68
72
|
|
73
|
+
def destination_path
|
74
|
+
File.expand_path(destination_file.path)
|
75
|
+
end
|
76
|
+
|
69
77
|
def device
|
70
78
|
@format.to_s == "jpg" ? "jpeg" : @format.to_s
|
71
79
|
end
|
72
80
|
|
73
81
|
def execute_command(command)
|
74
|
-
|
82
|
+
Open3.capture3(command)
|
75
83
|
end
|
76
84
|
|
77
85
|
def extract_options(options)
|
@@ -84,23 +92,8 @@ module PdfCover
|
|
84
92
|
fail InvalidOption.new(:resolution, @resolution) unless @resolution > 1
|
85
93
|
end
|
86
94
|
|
87
|
-
def failed_result(result)
|
88
|
-
destination_file.close
|
89
|
-
|
90
|
-
if result == false
|
91
|
-
logger.warn("GhostScript execution failed: #{$CHILD_STATUS}")
|
92
|
-
:command_failed
|
93
|
-
else
|
94
|
-
:command_not_found
|
95
|
-
end
|
96
|
-
end
|
97
|
-
|
98
95
|
def file_path
|
99
96
|
@file_path ||= File.expand_path(@file.path)
|
100
97
|
end
|
101
|
-
|
102
|
-
def logger
|
103
|
-
@logger ||= defined?(Rails) ? Rails.logger : Logger.new(STDERR)
|
104
|
-
end
|
105
98
|
end
|
106
99
|
end
|
data/lib/pdf_cover/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pdf_cover
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Juan González
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-06-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: awesome_print
|