Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

raise_on_encrypted: true #177

Merged
merged 5 commits into from
Apr 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
language: ruby
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Quick rundown:

Some links will be lost when ripping pages out of PDF files and merging them with another PDF.

* Some encrypted PDF files (usually the ones you can't view without a password) will fail quietly instead of noisily.
* Some encrypted PDF files (usually the ones you can't view without a password) will fail quietly instead of noisily. If you prefer to choose the noisy route, you can specify the `raise_on_encrypted` option using `CombinePDF.load(pdf_file, raise_on_encrypted: true)` which will raise a `CombinePDF::EncryptionError`.

* Sometimes the CombinePDF will raise an exception even if the PDF could be parsed (i.e., when PDF optional content exists)... I find it better to err on the side of caution, although for optional content PDFs an exception is avoidable using `CombinePDF.load(pdf_file, allow_optional_content: true)`.

Expand Down
9 changes: 8 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
require "bundler/gem_tasks"
require 'bundler/gem_tasks'
require 'rake/testtask'

task default: 'test'

Rake::TestTask.new do |t|
t.test_files = FileList['test/**/*_test.rb']
end

1 change: 1 addition & 0 deletions combine_pdf.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ Gem::Specification.new do |spec|
# spec.add_development_dependency "bundler", ">= 1.7"
spec.add_development_dependency "rake", ">= 12.3.3"
spec.add_development_dependency "minitest"
spec.add_development_dependency "minitest-around"
end
4 changes: 3 additions & 1 deletion lib/combine_pdf/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class PDFParser
# they are mainly to used to know if the file is (was) encrypted and to get more details.
attr_reader :info_object, :root_object, :names_object, :forms_object, :outlines_object, :metadata

attr_reader :allow_optional_content
attr_reader :allow_optional_content, :raise_on_encrypted
# when creating a parser, it is important to set the data (String) we wish to parse.
#
# <b>the data is required and it is not possible to set the data at a later stage</b>
Expand All @@ -58,6 +58,7 @@ def initialize(string, options = {})
@version = nil
@scanner = nil
@allow_optional_content = options[:allow_optional_content]
@raise_on_encrypted = options[:raise_on_encrypted]
end

# parse the data in the new parser (the data already set through the initialize / new method)
Expand Down Expand Up @@ -96,6 +97,7 @@ def parse
end

if @root_object[:Encrypt]
raise EncryptionError, 'the file is encrypted' if @raise_on_encrypted
# change_references_to_actual_values @root_object
warn 'PDF is Encrypted! Attempting to decrypt - not yet fully supported.'
decryptor = PDFDecrypt.new @parsed, @root_object
Expand Down
1 change: 1 addition & 0 deletions lib/combine_pdf/pdf_public.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ def initialize(parser = nil)
parser ||= PDFParser.new('')
raise TypeError, "initialization error, expecting CombinePDF::PDFParser or nil, but got #{parser.class.name}" unless parser.is_a? PDFParser
@objects = parser.parse
boazsegev marked this conversation as resolved.
Show resolved Hide resolved

# remove any existing id's
remove_old_ids
# set data from parser
Expand Down
48 changes: 48 additions & 0 deletions test/combine_pdf/load_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
require 'bundler/setup'
require 'minitest/autorun'
require 'minitest/around/spec'
require 'combine_pdf'

describe 'CombinePDF.load' do
let(:options) { {} }

subject { CombinePDF.load "test/fixtures/files/#{file}", options }

describe 'raise_on_encrypted option' do
let(:file) { 'sample_encrypted_pdf.pdf' }
let(:options) { { raise_on_encrypted: raise_on_encrypted } }

describe 'when raise_on_encrypted: true' do
let(:raise_on_encrypted) { true }

describe 'with encrypted file' do
it('raises an CombinePDF::EncryptionError') do
error = assert_raises(CombinePDF::EncryptionError) { subject }
assert_match 'the file is encrypted', error.message
end
end

describe 'with unencrypted file' do
let(:file) { 'sample_pdf.pdf' }

it('has a PDF') { assert_instance_of CombinePDF::PDF, subject }
end
end

describe 'when raise_on_encrypted: false' do
let(:raise_on_encrypted) { false }

describe 'with encrypted file' do
it('does not raise an CombinePDF::EncryptionError') do
assert_instance_of CombinePDF::PDF, subject
end
end

describe 'with unencrypted file' do
let(:file) { 'sample_pdf.pdf' }

it('has a PDF') { assert_instance_of CombinePDF::PDF, subject }
end
end
end
end
2 changes: 0 additions & 2 deletions test/combine_pdf/renderer_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
require 'bundler/setup'
require 'minitest/autorun'
require 'combine_pdf/renderer'

class CombinePDFRendererTest < Minitest::Test

Expand Down
Binary file added test/fixtures/files/sample_encrypted_pdf.pdf
Binary file not shown.
Binary file added test/fixtures/files/sample_pdf.pdf
Binary file not shown.