require 'mysql2' load 'config.rb' db = Mysql2::Client.new(:host => DB_HOST, :username => DB_USER, :database => DB_DB) invoice_id = ARGV[0] puts invoice_id output_dir = "./output/" statement = db.prepare("SELECT * FROM Invoices WHERE id = ?") result = statement.execute(invoice_id) result.each do |row| File.open("#{output_dir}/#{invoice_id}.pdf",'w+') do |file| file.write(row['data']) end end