Module: Common::PdfHelpers

Extended by:
SentryLogging
Defined in:
lib/common/pdf_helpers.rb

Class Method Summary collapse

Methods included from SentryLogging

log_exception_to_sentry, log_message_to_sentry, non_nil_hash?, normalize_level, rails_logger, set_sentry_metadata

Class Method Details

.unlock_pdf(input_file, password, output_file) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/common/pdf_helpers.rb', line 10

def self.unlock_pdf(input_file, password, output_file)
  doc = HexaPDF::Document.open(input_file, decryption_opts: { password: })
  doc.encrypt(name: nil)
  doc.write(output_file)
rescue HexaPDF::EncryptionError => e
  log_message_to_sentry(e.message, 'warn')
  raise Common::Exceptions::UnprocessableEntity.new(
    detail: I18n.t('errors.messages.uploads.pdf.incorrect_password'),
    source: 'Common::PdfHelpers.unlock_pdf'
  )
rescue HexaPDF::Error => e
  log_message_to_sentry(e.message, 'warn')
  raise Common::Exceptions::UnprocessableEntity.new(
    detail: I18n.t('errors.messages.uploads.pdf.invalid'),
    source: 'Common::PdfHelpers.unlock_pdf'
  )
end