Created
January 19, 2013 15:44
-
-
Save kiennt/4573242 to your computer and use it in GitHub Desktop.
Generate jpg image from pdf files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This implementation use
ghostscript
library andgrim
gemsActually
grim
was used for only calculate page count of pdf file. We can usghostscript
to do itWe 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