Skip to content

Instantly share code, notes, and snippets.

@sethetter
Created October 10, 2015 13:48
Show Gist options
  • Save sethetter/d2940f125547d45053a2 to your computer and use it in GitHub Desktop.
Save sethetter/d2940f125547d45053a2 to your computer and use it in GitHub Desktop.
def send_secure_pdf(str, user, title, orientation, password)
# Pull in output as email attachment
base = "#{Rails.root}/tmp/#{Time.now.to_i}-#{(rand*50).round}"
unsecured_file = "#{base}.pdf"
secured_file = "#{base}_secure.pdf"
pdf = WickedPdf.new.pdf_from_string(str, orientation: orientation)
File.open(unsecured_file, 'wb') do |file|
file << pdf
end
pdftk_command = "pdftk #{unsecured_file} output #{secured_file} owner_pw #{password}Yonder user_pw #{password}"
Open3.popen3(pdftk_command) do |stdin, stdout, stderr|
# Check for error?
end
mail(:subject => "[BalancedComp] #{title}.pdf", :to => user.email) do |format|
format.html
format.pdf do
attachments["#{title}.pdf"] = File.read(secured_file)
end
end
File.delete(unsecured_file)
File.delete(secured_file)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment