Last active
October 7, 2015 12:39
-
-
Save pwelch/616d750b8f01bd097a1b to your computer and use it in GitHub Desktop.
Creates a PNG from the first page of each PDF in a given directory
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
#!/usr/bin/env ruby | |
## | |
# Creates a PNG file from the 1st page of each PDF in a given directory | |
require 'mkmf' | |
stop = Proc.new { |msg| puts "\nFailure#{msg}"; exit 1 } | |
ARGV[0].nil? ? stop.call(': Provide directory of PDF files as first argument') : Dir.chdir(ARGV[0]) | |
%w{gs convert}.each do |cmd| | |
cmd_path = MakeMakefile.find_executable(cmd) | |
stop.call(': Requires Imagemagick and Ghostscript') if cmd_path.nil? | |
define_method("#{cmd}_bin".to_sym) do | |
cmd_path | |
end | |
end | |
Dir.glob('*.pdf').each do |pdf| | |
filename = File.basename(pdf, '.pdf') | |
puts "Creating #{filename}.png" | |
# convert -flatten -density 300 -trim -quality 100 input.pdf\[0\] output.png | |
system("#{convert_bin} -flatten -density 300 -trim -quality 100 #{pdf}[0] #{filename}.png") | |
end | |
at_exit do | |
File.delete('mkmf.log') if File.exists?('mkmf.log') | |
puts "\nFinished" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment