Skip to content

Instantly share code, notes, and snippets.

@zaid
Created August 3, 2016 19:19
Show Gist options
  • Save zaid/75738db88301abf55079240f3c9bcb69 to your computer and use it in GitHub Desktop.
Save zaid/75738db88301abf55079240f3c9bcb69 to your computer and use it in GitHub Desktop.
Simple method to convert JPGs to PDFs
require 'rmagick'
def jpgs_to_pdfs(path)
Dir.glob(File.join(path, '*.{jpg,jpeg,tif,tiff,png}')).each do |file_path|
next unless File.basename(file_path) =~ /\d+[a-z]*_\d+/i
next if File.basename(file_path) =~ /cheque/i
destination_path = "/home/zaid/Downloads/CMS\ PDFs"
destination_filename = "SUP#{File.basename(file_path).match(/\d+[a-z]*/i)[0]}.pdf"
destination_filepath = File.join(destination_path, destination_filename)
next if File.exist?(destination_filepath)
image = Magick::ImageList.new(file_path)
image.write(destination_filepath)
print '.'
end
end
def fix_pdf_filesnames(path)
Dir.glob('/home/zaid/Downloads/superior_gas_pdfs/*.pdf').each do |file_path|
next unless File.basename(file_path) =~ /^\d+_\d+/
next if File.basename(file_path) =~ /renewal/i
destination_path = "/home/zaid/Downloads/CMS\ PDFs"
destination_filename = "SUP#{File.basename(file_path).match(/(\d+)_/)[1]}.pdf"
destination_filepath = File.join(destination_path, destination_filename)
puts "#{File.basename(file_path)} -> #{destination_filename}"
FileUtils.cp(file_path, destination_filepath)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment