Search code examples
ruby-on-railsrubyattachmentpdfkitmailer

Rails mailer attachment with password protection +rails


Not sure if I am on right track or not but I was searching for a way where I can make the attachment(PDF that is created by PDFKit) password-protected, though I couldn't find any satisfactory solution for this issue.

Another approach for making the pdf with the password is to make changes to how it was being created, but again, there are not so many things on the web to create the PDF with the password with PDFKit

Hope anyone can show some light on the issue.

Thanks in advance


Solution

  • Finally, I got what I was looking for,

    https://github.com/rubyzip/rubyzip#password-protection-experimental

    By doing some little changes in this method, it turned out to be what I was expecting it to be.

    There'll be for sure any better solution for this but have found this one as of now, and hope it will be helpful for others.

    compressed_filestream = Zip::OutputStream.write_buffer(::StringIO.new(''), Zip::TraditionalEncrypter.new(PASSWORD_WHICH_WE_WANT_TO_SET)) do |zos|
      zos.put_next_entry FILE_NAME
      zos.print FILE_PATH.EXTENSION
    end
    compressed_filestream.rewind