module PDFToImage
Constants
- VERSION
pdftoimage version
Public Class Methods
exec(cmd, error = nil)
click to toggle source
Executes the specified command, returning the output.
@param cmd [String] The command to run @return [String] The output of the command
# File lib/pdftoimage.rb, line 60 def exec(cmd, error = nil) output = `#{cmd}` if $? != 0 if error == nil raise PDFError, "Error executing command: #{cmd}" else raise PDFError, error end end return output end
open(filename, &block)
click to toggle source
Opens a PDF document and prepares it for splitting into images.
@param filename [String] The filename of the PDF to open
@return [Array] An array of images
# File lib/pdftoimage.rb, line 35 def open(filename, &block) if not File.exists?(filename) raise PDFError, "File '#{filename}' not found." end pages = page_count(filename) # Array of images images = [] 1.upto(pages) { |n| dimensions = page_size(filename, n) image = Image.new(filename, random_filename, n, dimensions, pages) images << image } images.each(&block) if block_given? return images end
Private Class Methods
page_count(filename)
click to toggle source
# File lib/pdftoimage.rb, line 93 def page_count(filename) cmd = "pdfinfo #{filename} | grep Pages" output = exec(cmd) matches = /^Pages:.*?(\d+)$/.match(output) if matches.nil? raise PDFError, "Error determining page count." end return matches[1].to_i end
page_size(filename, page)
click to toggle source
# File lib/pdftoimage.rb, line 75 def page_size(filename, page) cmd = "pdfinfo -f #{page} -l #{page} #{filename} | grep Page" output = exec(cmd) matches = /^Page.*?size:.*?(\d+).*?(\d+)/.match(output) if matches.nil? raise PDFError, "Unable to determine page size." end scale = 2.08333333333333333 dimension = { width: (matches[1].to_i * scale).to_i, height: (matches[2].to_i * scale).to_i } dimension end
random_filename()
click to toggle source
Generate a random file name in the system's tmp folder
# File lib/pdftoimage.rb, line 105 def random_filename File.join(@@pdf_temp_dir, random_name) end
random_name(length = 15)
click to toggle source
Generate a random name of {#length} characters.
# File lib/pdftoimage.rb, line 110 def random_name(length = 15) @@chars ||= ("a".."z").to_a + ("A".."Z").to_a + ("1".."9").to_a return 'pdftoimage-' + Array.new(length, '').collect { @@chars[rand(@@chars.size)] }.join end