Skip to content

Commit

Permalink
Add option to suppress warnings which would otherwise be treated as e…
Browse files Browse the repository at this point in the history
…rrors and raise exceptions.

This requires qpdf version 10.0.2 or higher.

See: qpdf/qpdf#232
  • Loading branch information
emmeryn committed Jan 12, 2021
1 parent 83637aa commit fe16ed8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
16 changes: 11 additions & 5 deletions lib/qpdf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 15,23 @@ class Qpdf
@@config = {}
cattr_accessor :config

def initialize(execute_path = nil)
def initialize(execute_path = nil, options: {})
@exe_path = execute_path || find_binary_path
raise "Location of #{EXE_NAME} unknown" if @exe_path.empty?
raise "Bad location of #{EXE_NAME}'s path" unless File.exist?(@exe_path)
raise "#{EXE_NAME} is not executable" unless File.executable?(@exe_path)

@options = options
end

def unlock(source_file, unlocked_file, password = nil)
command = "#{@exe_path} --decrypt --password='#{password}' '#{source_file}' '#{unlocked_file}'"
command = "#{@exe_path} #{suppress_warnings_args} --decrypt --password='#{password}' '#{source_file}' '#{unlocked_file}'"
_, error_str, status = Open3.capture3(command)
raise "Error: #{error_str}" unless status.success?
end

def lock(source_file, locked_file, user_password, owner_password, key_length = 40)
command = "#{@exe_path} --encrypt #{user_password} #{owner_password} #{key_length} -- '#{source_file}' '#{locked_file}'"
command = "#{@exe_path} #{suppress_warnings_args} --encrypt #{user_password} #{owner_password} #{key_length} -- '#{source_file}' '#{locked_file}'"
_, error_str, status = Open3.capture3(command)
raise "Error: #{error_str}" unless status.success?
end
Expand All @@ -56,7 58,7 @@ def split_pages(source_file, dest_filename)
dest_filename_before = "#{dest_filename}-"
end

command = "#{@exe_path} --split-pages '#{source_file}' '#{dest_filename}'"
command = "#{@exe_path} #{suppress_warnings_args} --split-pages '#{source_file}' '#{dest_filename}'"
num_pages = num_pages source_file
num_pages_digits = num_pages.to_s.length

Expand All @@ -71,7 73,7 @@ def split_pages(source_file, dest_filename)
end

def num_pages(source_file)
command = "#{@exe_path} --show-npages '#{source_file}'"
command = "#{@exe_path} #{suppress_warnings_args} --show-npages '#{source_file}'"
output_str, error_str, status = Open3.capture3(command)
raise "Error: #{error_str}" unless status.success?
output_str.to_i
Expand All @@ -85,4 87,8 @@ def find_binary_path
exe_path ||= possible_locations.map{|l| File.expand_path("#{l}/#{EXE_NAME}") }.find{|location| File.exists? location}
exe_path || ''
end

def suppress_warnings_args
@options[:suppress_warnings] ? '--no-warn --warning-exit-0' : ''
end
end
8 changes: 4 additions & 4 deletions qpdf.gemspec
Original file line number Diff line number Diff line change
@@ -1,13 1,13 @@
Gem::Specification.new do |g|
g.name = 'qpdf'
g.version = '0.0.3'
g.date = '2019-10-14'
g.version = '0.0.4'
g.date = '2020-01-13'
g.summary = 'Qpdf library for Rails'
g.description = 'Qpdf is used for unlocking locked pdf files'
g.license = 'MIT'
g.authors = ['Ken Berland', 'Justin Ahn', 'Brett Suwyn']
g.authors = ['Ken Berland', 'Justin Ahn', 'Brett Suwyn', 'Sue Zheng Hao']
g.email = '[email protected]'
g.homepage = 'https://github.com/ConsultingMD/qpdf.git'
g.homepage = 'https://github.com/emmeryn/qpdf.git'
g.files = %w(README.md LICENSE)
g.files = Dir.glob("{lib,generators}/**/*")
g.require_paths = ['lib']
Expand Down

0 comments on commit fe16ed8

Please sign in to comment.