Skip to content

Instantly share code, notes, and snippets.

@spickermann
Created April 13, 2014 11:24
Show Gist options
  • Save spickermann/10579869 to your computer and use it in GitHub Desktop.
Save spickermann/10579869 to your computer and use it in GitHub Desktop.
Copies pdf files from one directory into an other and removes on the fly the password from the pdf files that is requested to open them.
# Script using [qpdf](http://qpdf.sourceforge.net/) to
# remove the password from all pdf files in a given
# directory and its subdirectories.
#
# Install qpdf with brew
#
# brew install qpdf
#
require 'fileutils'
DIR = './pdf'
OUT = "#{DIR}-decrypt"
PASSWORD = 'THE PASSWORD'
base = Pathname.new(DIR)
pdfs = Dir.glob("#{base}/**/*.pdf")
if pdfs.size > 0
puts "processing #{pdfs.size} pdf files"
pdfs.each do |path|
input = Pathname.new(path)
out = Pathname.new(File.join(OUT, input.relative_path_from(base)))
FileUtils.mkdir_p(out.dirname, :mode => 0755) unless Dir.exist?(out.dirname)
puts "decrypting #{input.relative_path_from(base)}"
puts " -> #{out.relative_path_from(base)}\n"
`qpdf --password="#{PASSWORD}" --decrypt "#{input}" "#{out}"`
if $?.success?
puts ' done'
else
FileUtils.copy_file(input, out)
end
end
puts "processed #{pdfs.size} pdf files"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment