class PDFToImage::Image

A class which is instantiated by PDFToImage when a PDF document is opened.

Constants

CUSTOM_IMAGE_METHODS

ImageMagick methods that we currently support.

Attributes

args[R]
filename[R]
height[R]
opened[R]
page[R]
pdf_name[R]
width[R]

Public Class Methods

new(pdf_name, filename, page, page_size, page_count) click to toggle source

Image constructor

@param pdf_name [String] The name of the PDF @param filename [String] The name of the image for the specified page @param page [Integer] The page number of the PDF @param page_size [Hash] Hash containing width and height dimensions of the page @param page_count [integer] The number of pages in the PDF

# File lib/pdftoimage/image.rb, line 35
def initialize(pdf_name, filename, page, page_size, page_count)
    @args = []

    @pdf_name = pdf_name
    @filename = filename
    @opened = false
    @width = page_size[:width]
    @height = page_size[:height]
    @page_count = page_count

    @page = page
end

Public Instance Methods

<=>(img) click to toggle source
# File lib/pdftoimage/image.rb, line 68
def <=>(img)
    if @page == img.page
        return 0
    elsif @page < img.page
        return -1
    else
        return 1
    end
end
save(outname) click to toggle source

Saves the converted image to the specified location

@param outname [String] The output filename of the image

# File lib/pdftoimage/image.rb, line 52
def save(outname)
    generate_temp_file

    cmd = "convert "

    if not @args.empty?
        cmd += "#{@args.join(' ')} "
    end

    cmd += "#{@filename} #{outname}"

    PDFToImage.exec(cmd)

    return true
end

Private Instance Methods

generate_temp_file() click to toggle source
# File lib/pdftoimage/image.rb, line 80
def generate_temp_file
    if @opened == false
        cmd = "pdftoppm -png -f #{@page} -l #{@page} #{@pdf_name} #{@filename}"
        PDFToImage.exec(cmd)
        @filename = "#{@filename}-#{page_suffix}.png"
        @opened = true
    end

    return true
end
page_suffix() click to toggle source
# File lib/pdftoimage/image.rb, line 91
def page_suffix
    cur_page_len = @page.to_s.length
    total_page_len = @page_count.to_s.length

    num_zeroes = total_page_len - cur_page_len

    # This is really weird. Basically, poppler_utils does not
    # prepend a '0' to the page count when the total number of
    # pages is 10, 100, 1000, 10000, etc. I hate putting this here,
    # but whatever...

    # TODO: Keep an eye on this. This suddenly started causing problems.
    # Did poppler_utils change?
    #      if @page_count.to_s.reverse.to_i == 1 && num_zeroes > 0
    #       num_zeroes = num_zeroes - 1
    #      end

    if cur_page_len == total_page_len
        @page
    end

    padded = '0' * num_zeroes + @page.to_s
    padded
end