Skip to content

Commit

Permalink
Remove pdf metadata
Browse files Browse the repository at this point in the history
In order to remove metadata from PDF documents we will use the
exiftool_vendored gem.

The following line:
  Exiftool.new(attachment_path, "-overwrite_original -all:all=")
Overwrites the original file with another file without metadata.

So far this is the best solution we have found to perform this
metadata deletion.

When using Exiftool an exception is thrown, so we added a rescue
to handle it. Here is a task created where this problem is
discussed: exiftool-rb/exiftool.rb#28.
We'll wait to see if this will be fixed in future versions.

Translated with www.DeepL.com/Translator (free version)
  • Loading branch information
taitus committed May 11, 2023
1 parent 852796d commit 8fb3ffa
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,10 @@ GEM
multi_json (>= 1.3)
rake
execjs (2.8.1)
exiftool (1.2.4)
json
exiftool_vendored (12.60.0)
exiftool (>= 0.7.0)
factory_bot (6.2.0)
activesupport (>= 5.0.0)
factory_bot_rails (6.2.0)
Expand Down Expand Up @@ -694,6 +698,7 @@ DEPENDENCIES
devise-security (~> 0.16.0)
email_spec (~> 2.2.0)
erb_lint (~> 0.3.1)
exiftool_vendored (~> 12.60.0)
factory_bot_rails (~> 6.2.0)
faker (~> 2.22.0)
file_validators (~> 3.0.0)
Expand Down
1 change: 1 addition & 0 deletions Gemfile_custom
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@
#
# Add your custom gem dependencies here

gem "exiftool_vendored", "~> 12.60.0"
gem "ruby-ntlm", "~> 0.0.4"
12 changes: 12 additions & 0 deletions app/models/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class Document < ApplicationRecord

scope :admin, -> { where(admin: true) }

before_save :remove_metadata

def self.humanized_accepted_content_types
Setting.accepted_content_types_for("documents").join(", ")
end
Expand All @@ -36,4 +38,14 @@ def association_name
def documentable_class
association_class
end

def remove_metadata
return unless self.attachment.attached?
attachment_path = ActiveStorage::Blob.service.path_for(self.attachment.key)

Exiftool.new(attachment_path, "-overwrite_original -all:all=")
self.attachment.attach(io: File.open(attachment_path), filename: self.attachment.filename.to_s)
rescue Exiftool::ExiftoolNotInstalled
nil
end
end

0 comments on commit 8fb3ffa

Please sign in to comment.