Class: Iguvium::Image

Inherits:
Object
  • Object
show all
Defined in:
lib/iguvium/image.rb

Overview

PDF to image converter

Class Method Summary collapse

Class Method Details

.read(path, pagenumber = 1, **opts) ⇒ ChunkyPNG::Image

Prints single page without text to .rgb file and reads it back to memory

Parameters:

  • path (String)

    path to PDF file to be read

  • pagenumber (Integer) (defaults to: 1)

    number of page, first page is 1, not 0

  • opts (Hash)

    a customizable set of options

Options Hash (**opts):

  • :images (Boolean) — default: false

    consider pictures in PDF as possible table separators

  • :gspath (String) — default: nil

    explicit path to the GhostScript executable. Use it in case of non-standard gs executable placement. If not specified, gem tries standard options like C:/Program Files/gs/gs*/bin/gswin??c.exe on Windows or just gs on Mac and Linux

Returns:

  • (ChunkyPNG::Image)


17
18
19
20
21
22
23
24
25
26
# File 'lib/iguvium/image.rb', line 17

def self.read(path, pagenumber = 1, **opts)
  rgb = path.gsub(/\.pdf$/, '.rgb')
  Iguvium.logger.info `#{opts[:gspath]} -dSAFER -dBATCH -dNOPAUSE -sDEVICE=pnggray -dGraphicsAlphaBits=4 \
-r72 -dFirstPage=#{pagenumber} -dLastPage=#{pagenumber} \
-dFILTERTEXT #{'-dFILTERIMAGE' unless opts[:images]} -sOutputFile=#{rgb.shellescape} #{path.shellescape} 2>&1`

  image = ChunkyPNG::Image.from_file(rgb)
  File.delete rgb
  image
end