Class: Paperclip::PdfCoverProcessor

Inherits:
Processor
  • Object
show all
Defined in:
lib/paperclip/pdf_cover_processor.rb

Overview

This is a Paperclip::Processor that can be used to generate an image from a PDF file. The image is only of the first page.

We inherit the following instance variables from Paperclip::Processor:

@file the file that will be operated on (which is an instance of File)
@options a hash of options that were defined in has_attached_file's style hash
@attachment the Paperclip::Attachment itself

Constant Summary collapse

QUALITY_CONVERT_OPTION_REGEX =
/-quality\s+(?<quality>\d+)/
RESOLUTION_CONVERT_OPTION_REGEX =
/-density\s+(?<resolution>\d+)/

Instance Method Summary collapse

Instance Method Details

#converter_optionsObject



20
21
22
# File 'lib/paperclip/pdf_cover_processor.rb', line 20

def converter_options
  format.merge(jpeg_quality).merge(jpeg_resolution)
end

#extract_convert_option(key, regex) ⇒ Object



36
37
38
39
40
41
# File 'lib/paperclip/pdf_cover_processor.rb', line 36

def extract_convert_option(key, regex)
  match_data = regex.match(@options[:convert_options])
  match = match_data && match_data[key]

  match ? { key => match } : {}
end

#formatObject



24
25
26
# File 'lib/paperclip/pdf_cover_processor.rb', line 24

def format
  { format: @options[:format].to_s }
end

#jpeg_qualityObject



28
29
30
# File 'lib/paperclip/pdf_cover_processor.rb', line 28

def jpeg_quality
  extract_convert_option(:quality, QUALITY_CONVERT_OPTION_REGEX)
end

#jpeg_resolutionObject



32
33
34
# File 'lib/paperclip/pdf_cover_processor.rb', line 32

def jpeg_resolution
  extract_convert_option(:resolution, RESOLUTION_CONVERT_OPTION_REGEX)
end

#makeObject



16
17
18
# File 'lib/paperclip/pdf_cover_processor.rb', line 16

def make
  ::PdfCover::Converter.new(@file, converter_options).converted_file
end