class PdfCover::Converter

Constants

COMMAND_EXECUTION_SUCCESS_CODE
COMMAND_NOT_FOUND_CODE
DEFAULT_ANTIALIASING
DEFAULT_FORMAT

NOTE: Update the generate_jpegs.sh script when changing these values

DEFAULT_QUALITY
DEFAULT_RESOLUTION

Public Class Methods

new(file, options = {}) click to toggle source
# File lib/pdf_cover/converter.rb, line 32
def initialize(file, options = {})
  @file = file
  extract_options(options)
end

Public Instance Methods

converted_file() click to toggle source

@raises PdfCover::Converter::CommandNotFoundError if GhostScript is not found @raises PdfCover::Converter::CommandFailedError if GhostScript returns a non-zero status

# File lib/pdf_cover/converter.rb, line 39
def converted_file
  parameters = build_parameters(file_path, device)
  stdout_str, stderr_str, status = execute_command("gs #{parameters}")

  case status
    when COMMAND_EXECUTION_SUCCESS_CODE then destination_file
    when COMMAND_NOT_FOUND_CODE then fail CommandNotFoundError
    else fail CommandFailedError.new(stdout_str, stderr_str)
  end
rescue => e
  destination_file.close
  raise e
end

Private Instance Methods

basename() click to toggle source
# File lib/pdf_cover/converter.rb, line 55
def basename
  @basename ||= File.basename(@file.path, File.extname(@file.path))
end
build_parameters(source, device) click to toggle source
# File lib/pdf_cover/converter.rb, line 59
def build_parameters(source, device)
  %W(-sOutputFile="#{destination_path}" -dNOPAUSE
     -sDEVICE="#{device}" -dJPEGQ=#{@quality}
     -dFirstPage=1 -dLastPage=1
     -dGraphicsAlphaBits=#{@antialiasing}
     -r#{@resolution} -q "#{source}"
     -c quit)
    .join(" ")
end
destination_file() click to toggle source
# File lib/pdf_cover/converter.rb, line 69
def destination_file
  @destination_file ||= Tempfile.new([basename, ".#{@format}"]).tap do |file|
    file.binmode
  end
end
destination_path() click to toggle source
# File lib/pdf_cover/converter.rb, line 75
def destination_path
  File.expand_path(destination_file.path)
end
device() click to toggle source
# File lib/pdf_cover/converter.rb, line 79
def device
  @format.to_s == "jpg" ? "jpeg" : @format.to_s
end
execute_command(command) click to toggle source
# File lib/pdf_cover/converter.rb, line 83
def execute_command(command)
  Open3.capture3(command)
end
extract_options(options) click to toggle source
# File lib/pdf_cover/converter.rb, line 87
def extract_options(options)
  @format = options.fetch(:format, DEFAULT_FORMAT)
  @quality = options.fetch(:quality, DEFAULT_QUALITY).to_i
  @resolution = options.fetch(:resolution, DEFAULT_RESOLUTION).to_i
  @antialiasing = options.fetch(:antialiasing, DEFAULT_ANTIALIASING).to_i

  validate_options
end
file_path() click to toggle source
# File lib/pdf_cover/converter.rb, line 96
def file_path
  @file_path ||= File.expand_path(@file.path)
end
validate_options() click to toggle source
# File lib/pdf_cover/converter.rb, line 100
def validate_options
  fail InvalidOption.new(:format, @format) unless %w(jpg jpeg png).include?(@format)
  fail InvalidOption.new(:quality, @quality) unless @quality.between?(1, 100)
  fail InvalidOption.new(:resolution, @resolution) unless @resolution > 1
  fail InvalidOption.new(:antialiasing, @antialiasing) unless @antialiasing >= 1 && @antialiasing <= 4
end