Class: WaxIiif::Utilities::PdfSplitter

Inherits:
Object
  • Object
show all
Defined in:
lib/wax_iiif/utilities/pdf_splitter.rb

Overview

Class PdfSplitter is a utility function designed to convert a PDF into a stack of images.

Author:

Class Method Summary collapse

Class Method Details

.split(path, options = {}) ⇒ Array<String>

Convert a PDF File into a series of JPEG images, one per-page

Parameters:

  • path (String)

    The path to the PDF file

  • options (Hash) (defaults to: {})

    The configuration hash

Returns:

  • (Array<String>)

    The paths to the generated files



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/wax_iiif/utilities/pdf_splitter.rb', line 16

def self.split(path, options = {})
  puts "processing #{path}" if options.fetch(:verbose, false)

  name        = File.basename(path, File.extname(path))
  output_dir  = "#{options.fetch(:output_dir, '.')}/#{name}"
  pages       = []
  pdf         = MiniMagick::Image.open(path)
  max_digits  = pdf.pages.length.digits.length

  pdf.pages.each_with_index do |page, index|
    FileUtils.mkdir_p output_dir
    page_file_name = "#{output_dir}/#{index.to_s.rjust(max_digits, '0')}.jpg"

    MiniMagick::Tool::Convert.new do |convert|
      convert.density('300')
      convert.units('PixelsPerInch')
      convert << page.path
      convert.quality('80')
      convert.colorspace('sRGB')
      convert.interlace('none')
      convert.flatten
      convert << page_file_name
    end
    pages.push(page_file_name)
  end
  GC.start

  pages
end