diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..f819a51 --- /dev/null +++ b/.travis.yml @@ -0,0 +1 @@ +language: ruby diff --git a/README.md b/README.md index a357728..8c4e8b7 100644 --- a/README.md +++ b/README.md @@ -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)`. diff --git a/Rakefile b/Rakefile index 809eb56..df70ff0 100644 --- a/Rakefile +++ b/Rakefile @@ -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 diff --git a/combine_pdf.gemspec b/combine_pdf.gemspec index 39870b7..5856acc 100644 --- a/combine_pdf.gemspec +++ b/combine_pdf.gemspec @@ -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 diff --git a/lib/combine_pdf/parser.rb b/lib/combine_pdf/parser.rb index e33b5f0..588d044 100644 --- a/lib/combine_pdf/parser.rb +++ b/lib/combine_pdf/parser.rb @@ -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. # # the data is required and it is not possible to set the data at a later stage @@ -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) @@ -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 diff --git a/lib/combine_pdf/pdf_public.rb b/lib/combine_pdf/pdf_public.rb index a55b81b..8784d21 100644 --- a/lib/combine_pdf/pdf_public.rb +++ b/lib/combine_pdf/pdf_public.rb @@ -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 + # remove any existing id's remove_old_ids # set data from parser diff --git a/test/combine_pdf/load_test.rb b/test/combine_pdf/load_test.rb new file mode 100644 index 0000000..47ee57a --- /dev/null +++ b/test/combine_pdf/load_test.rb @@ -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 diff --git a/test/combine_pdf/renderer_test.rb b/test/combine_pdf/renderer_test.rb index 882d20d..53ec41a 100644 --- a/test/combine_pdf/renderer_test.rb +++ b/test/combine_pdf/renderer_test.rb @@ -1,6 +1,4 @@ -require 'bundler/setup' require 'minitest/autorun' -require 'combine_pdf/renderer' class CombinePDFRendererTest < Minitest::Test diff --git a/test/fixtures/files/sample_encrypted_pdf.pdf b/test/fixtures/files/sample_encrypted_pdf.pdf new file mode 100644 index 0000000..71db793 Binary files /dev/null and b/test/fixtures/files/sample_encrypted_pdf.pdf differ diff --git a/test/fixtures/files/sample_pdf.pdf b/test/fixtures/files/sample_pdf.pdf new file mode 100644 index 0000000..8b9f049 Binary files /dev/null and b/test/fixtures/files/sample_pdf.pdf differ