pdf_cover 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 +4 -4
- data/.rubocop.yml +2 -2
- data/README.md +6 -6
- data/lib/paperclip/pdf_cover.rb +18 -5
- data/lib/pdf_cover/converter.rb +24 -4
- data/lib/pdf_cover/version.rb +1 -1
- data/lib/pdf_cover.rb +7 -6
- 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: 97202642bc05deda3ecf113ac840a054a9d6af36
|
4
|
+
data.tar.gz: b4e6417e3be0d9efa7706fd9e704d9115cc69b68
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5181a73f41d036c73277a91df1071494cfd67fd4001e501cf282a77bef379ed48f29c13b413ae06d7d708e00a09f9740cb8fb60e1d41aaac88086a582045e504
|
7
|
+
data.tar.gz: ba2c5f6520f4e7dd278cdfde851021cc572ac0a72d8fc10e1e9875c266803c2830fe0d78706721f1c8305709077e45c8879d894e42880a8755d0d5ea260e8b2d
|
data/.rubocop.yml
CHANGED
@@ -18,8 +18,8 @@ AllCops:
|
|
18
18
|
|
19
19
|
# By default, the rails cops are not run. Override in project or home
|
20
20
|
# directory .rubocop.yml files, or by giving the -R/--rails option.
|
21
|
-
Rails:
|
22
|
-
|
21
|
+
Rails:
|
22
|
+
Enabled: true
|
23
23
|
|
24
24
|
# Indent private/protected/public as deep as method definitions
|
25
25
|
AccessModifierIndentation:
|
data/README.md
CHANGED
@@ -1,7 +1,4 @@
|
|
1
|
-
# PdfCover
|
2
|
-
|
3
|
-
![Build Status](https://api.travis-ci.org/xing/pdf_cover.svg)
|
4
|
-
[![Coverage Status](https://coveralls.io/repos/github/xing/pdf_cover/badge.svg?branch=master)](https://coveralls.io/github/xing/pdf_cover?branch=master)
|
1
|
+
# PdfCover [![Build Status](https://api.travis-ci.org/xing/pdf_cover.svg)](https://travis-ci.org/xing/pdf_cover) [![Code Climate](https://codeclimate.com/github/xing/pdf_cover/badges/gpa.svg)](https://codeclimate.com/github/xing/pdf_cover) [![Coverage Status](https://coveralls.io/repos/github/xing/pdf_cover/badge.svg?branch=master)](https://coveralls.io/github/xing/pdf_cover?branch=master)
|
5
2
|
|
6
3
|
With this gem you can easily have attachments for PDF files that have associated
|
7
4
|
images generated for their first page.
|
@@ -9,6 +6,9 @@ images generated for their first page.
|
|
9
6
|
Support is provided both for [Paperclip](https://github.com/thoughtbot/paperclip)
|
10
7
|
and [CarrierWave](https://github.com/carrierwaveuploader/carrierwave).
|
11
8
|
|
9
|
+
In both cases the JPEG quality and resolution are optional and will be set to 85%
|
10
|
+
and 300dpi respectively when not provided.
|
11
|
+
|
12
12
|
## Paperclip Support
|
13
13
|
|
14
14
|
To add a PDF cover style to your attachments you can do something like this:
|
@@ -18,7 +18,7 @@ class WithPaperclip < ActiveRecord::Base
|
|
18
18
|
include PdfCover
|
19
19
|
|
20
20
|
pdf_cover_attachment :pdf, styles: { pdf_cover: ['', :jpeg]},
|
21
|
-
convert_options: { all: '-quality 95' },
|
21
|
+
convert_options: { all: '-quality 95 -density 300' },
|
22
22
|
|
23
23
|
validates_attachment_content_type :pdf, content_type: %w(application/pdf)
|
24
24
|
end
|
@@ -41,7 +41,7 @@ class WithCarrierwaveUploader < CarrierWave::Uploader::Base
|
|
41
41
|
storage :file
|
42
42
|
|
43
43
|
version :image do
|
44
|
-
pdf_cover_attachment
|
44
|
+
pdf_cover_attachment quality: 95, resolution: 300
|
45
45
|
end
|
46
46
|
end
|
47
47
|
```
|
data/lib/paperclip/pdf_cover.rb
CHANGED
@@ -11,20 +11,33 @@ if Kernel.const_defined?(:Paperclip)
|
|
11
11
|
# @attachment the Paperclip::Attachment itself
|
12
12
|
class PdfCover < Processor
|
13
13
|
QUALITY_CONVERT_OPTION_REGEX = /-quality\s+(?<quality>\d+)/
|
14
|
+
RESOLUTION_CONVERT_OPTION_REGEX = /-density\s+(?<resolution>\d+)/
|
14
15
|
|
15
16
|
def make
|
16
|
-
::PdfCover::Converter.new(@file,
|
17
|
+
::PdfCover::Converter.new(@file, converter_options).converted_file
|
18
|
+
end
|
19
|
+
|
20
|
+
def converter_options
|
21
|
+
format.merge(jpeg_quality).merge(jpeg_resolution)
|
17
22
|
end
|
18
23
|
|
19
24
|
def format
|
20
|
-
@options[:format].to_s
|
25
|
+
{ format: @options[:format].to_s }
|
21
26
|
end
|
22
27
|
|
23
28
|
def jpeg_quality
|
24
|
-
|
29
|
+
extract_convert_option(:quality, QUALITY_CONVERT_OPTION_REGEX)
|
30
|
+
end
|
31
|
+
|
32
|
+
def jpeg_resolution
|
33
|
+
extract_convert_option(:resolution, RESOLUTION_CONVERT_OPTION_REGEX)
|
34
|
+
end
|
35
|
+
|
36
|
+
def extract_convert_option(key, regex)
|
37
|
+
match_data = regex.match(@options[:convert_options])
|
38
|
+
match = match_data && match_data[key]
|
25
39
|
|
26
|
-
|
27
|
-
match_data && match_data[:quality]
|
40
|
+
match ? { key => match } : {}
|
28
41
|
end
|
29
42
|
end
|
30
43
|
end
|
data/lib/pdf_cover/converter.rb
CHANGED
@@ -1,15 +1,25 @@
|
|
1
1
|
module PdfCover
|
2
2
|
class Converter
|
3
|
+
# NOTE: Update the generate_jpegs.sh script when changing these values
|
4
|
+
DEFAULT_FORMAT = "jpeg"
|
5
|
+
DEFAULT_QUALITY = 85
|
6
|
+
DEFAULT_RESOLUTION = 300
|
7
|
+
|
3
8
|
class CommandNotFoundError < StandardError
|
4
9
|
def initialize
|
5
10
|
super("Could not run the `gs` command. Make sure that GhostScript is in your PATH.")
|
6
11
|
end
|
7
12
|
end
|
8
13
|
|
14
|
+
class InvalidOption < StandardError
|
15
|
+
def initialize(option_name, option_value)
|
16
|
+
super("Invalid option '#{option_name}' with value: '#{option_value}'")
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
9
20
|
def initialize(file, options = {})
|
10
21
|
@file = file
|
11
|
-
|
12
|
-
@quality = options[:quality] || 95
|
22
|
+
extract_options(options)
|
13
23
|
end
|
14
24
|
|
15
25
|
# @raises PdfCover::Converter::CommandNotFoundError if GhostScript is not found
|
@@ -18,7 +28,7 @@ module PdfCover
|
|
18
28
|
when :ok
|
19
29
|
destination_file
|
20
30
|
when :command_failed
|
21
|
-
@file
|
31
|
+
@file
|
22
32
|
when :command_not_found
|
23
33
|
fail CommandNotFoundError
|
24
34
|
end
|
@@ -34,7 +44,7 @@ module PdfCover
|
|
34
44
|
%W(-sOutputFile='#{destination}' -dNOPAUSE
|
35
45
|
-sDEVICE='#{device}' -dJPEGQ=#{@quality}
|
36
46
|
-dFirstPage=1 -dLastPage=1
|
37
|
-
-
|
47
|
+
-r#{@resolution} -q '#{source}'
|
38
48
|
-c quit
|
39
49
|
).join(" ")
|
40
50
|
end
|
@@ -64,6 +74,16 @@ module PdfCover
|
|
64
74
|
Kernel.system(command)
|
65
75
|
end
|
66
76
|
|
77
|
+
def extract_options(options)
|
78
|
+
@format = options.fetch(:format, DEFAULT_FORMAT)
|
79
|
+
@quality = options.fetch(:quality, DEFAULT_QUALITY).to_i
|
80
|
+
@resolution = options.fetch(:resolution, DEFAULT_RESOLUTION).to_i
|
81
|
+
|
82
|
+
fail InvalidOption.new(:format, @format) unless %w(jpg jpeg png).include?(@format)
|
83
|
+
fail InvalidOption.new(:quality, @quality) unless @quality.between?(1, 100)
|
84
|
+
fail InvalidOption.new(:resolution, @resolution) unless @resolution > 1
|
85
|
+
end
|
86
|
+
|
67
87
|
def failed_result(result)
|
68
88
|
destination_file.close
|
69
89
|
|
data/lib/pdf_cover/version.rb
CHANGED
data/lib/pdf_cover.rb
CHANGED
@@ -29,8 +29,8 @@ module PdfCover
|
|
29
29
|
# pdf_cover_attachment
|
30
30
|
# end
|
31
31
|
# end
|
32
|
-
def pdf_cover_attachment
|
33
|
-
process :
|
32
|
+
def pdf_cover_attachment(options = {})
|
33
|
+
process pdf_cover: [options[:quality], options[:resolution]]
|
34
34
|
|
35
35
|
define_method :full_filename do |for_file = model.logo.file|
|
36
36
|
for_file.gsub(/pdf$/, "jpeg")
|
@@ -54,7 +54,7 @@ module PdfCover
|
|
54
54
|
# include PdfCover
|
55
55
|
#
|
56
56
|
# pdf_cover_attachment :pdf, styles: { pdf_cover: ['', :jpeg]},
|
57
|
-
# convert_options: { all: '-quality 95' }
|
57
|
+
# convert_options: { all: '-quality 95 -density 300' }
|
58
58
|
#
|
59
59
|
# # Note that you must set content type validation as required by Paperclip
|
60
60
|
# validates_attachment_content_type :pdf, content_type: %w(application/pdf)
|
@@ -89,9 +89,10 @@ module PdfCover
|
|
89
89
|
end
|
90
90
|
end
|
91
91
|
|
92
|
-
# This is the method used by the
|
93
|
-
def pdf_cover
|
94
|
-
|
92
|
+
# This is the method used by the CarrierWave processor mechanism
|
93
|
+
def pdf_cover(quality, resolution)
|
94
|
+
options = { quality: quality, resolution: resolution }
|
95
|
+
converted_file = PdfCover::Converter.new(file, options).converted_file
|
95
96
|
FileUtils.cp(converted_file, current_path)
|
96
97
|
end
|
97
98
|
end
|
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.1.
|
4
|
+
version: 0.1.1
|
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-03-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: awesome_print
|