Skip to content

Instantly share code, notes, and snippets.

@kiennt
Created January 19, 2013 15:44
Show Gist options
  • Save kiennt/4573242 to your computer and use it in GitHub Desktop.
Save kiennt/4573242 to your computer and use it in GitHub Desktop.
Generate jpg image from pdf files
require 'grim'
require 'thread'
class ThreadPool
def initialize(size = 5)
@jobs = Queue.new
@size = size
@pool = Array.new(@size) do |i|
Thread.new do
Thread.current[:id] = i
catch(:exit) do
loop do
job, args = @jobs.pop
job.call *args
end
end
end
end
end
def spawn(*args, &block)
@jobs << [block, args]
end
def shutdown
@size.times do
spawn { throw :exit }
end
@pool.map(&:join)
end
end
class Converter
def initialize filepath, concurent = 10
@filepath = filepath
@concurent = concurent.to_i
@count = Grim.reap(@filepath).count
end
def run
@pool = ThreadPool.new @concurent
block = @count / @concurent
(0...@concurent).to_a.each do |index|
first_page = block * index + 1
last_page = first_page + block-1
@pool.spawn do
`gs -dNOPAUSE -sDEVICE=jpeg -dFirstPage=#{first_page} -dLastPage=#{last_page} -r100x100 -sOutputFile=image-#{index}-%d.jpg -q #{@filepath} -c quit`
end
end
end
def exit
@pool.shutdown
end
end
if $0 == __FILE__
unless ARGV[0].nil?
converter = Converter.new(ARGV[0], ARGV[1])
converter.run
at_exit { converter.exit }
end
end
@kiennt
Copy link
Author

kiennt commented Jan 19, 2013

This implementation use ghostscript library and grim gems
Actually grim was used for only calculate page count of pdf file. We can us ghostscript to do it

We using Thread Pool to run some process of ghostscript concurently.
Full implementation should upload files right after generating image
Upload should be implemented by evented loop for high performance

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment