spec/document_spec.rb in podoff-1.1.1 vs spec/document_spec.rb in podoff-1.2.0

- old
+ new

@@ -10,11 +10,11 @@ describe Podoff::Document do before :all do - @d = Podoff.load('pdfs/udocument0.pdf') + @d = Podoff.load('pdfs/udocument0.pdf', 'iso-8859-1') end describe '#objs' do it 'returns a hash of PDF "obj"' do @@ -37,45 +37,41 @@ describe '#page' do it 'returns a page given an index (starts at 1)' do p = @d.page(1) + expect(p.ref).to eq('56 0') expect(p.class).to eq(Podoff::Obj) expect(p.type).to eq('/Page') - expect(p.attributes[:pagenum]).to eq('1') - expect(p.page_number).to eq(1) end it 'returns nil if the page doesn\'t exist' do expect(@d.page(0)).to eq(nil) expect(@d.page(9)).to eq(nil) end - it 'returns the page, even for a doc without pdftk_PageNum' do + it 'returns a page given an index (starts at 1) (2)' do - d = Podoff::Document.load('pdfs/t2.pdf') + d = Podoff::Document.load('pdfs/t2.pdf', 'utf-8') expect(d.page(1).ref).to eq('3 0') - expect(d.page(1).page_number).to eq(nil) expect(d.page(0)).to eq(nil) expect(d.page(2)).to eq(nil) end it 'returns pages from the last when the index is negative' do - expect(@d.page(-1).ref).to eq('33 0') - expect(@d.page(-1).page_number).to eq(3) + expect(@d.page(-1).ref).to eq('58 0') end - it 'returns pages from the last when the index is negative (no PageNum)' do + it 'returns pages from the last when the index is negative (2)' do - d = Podoff::Document.load('pdfs/t2.pdf') + d = Podoff::Document.load('pdfs/t2.pdf', 'utf-8') expect(d.page(-1).ref).to eq('3 0') - expect(d.page(-1).page_number).to eq(nil) end end describe '#dup' do @@ -84,20 +80,22 @@ d = @d.dup expect(d.class).to eq(Podoff::Document) expect(d.hash).not_to eq(@d.hash) + expect(d.encoding).to eq('iso-8859-1') + expect(d.objs.hash).not_to eq(@d.objs.hash) expect(d.objs.values.first.hash).not_to eq(@d.objs.values.first.hash) expect(d.objs.values.first.class).to eq(Podoff::Obj) expect(d.objs.values.first.document.class).to eq(Podoff::Document) expect(d.objs.values.first.document).to equal(d) expect(@d.objs.values.first.document).to equal(@d) - expect(d.root).to eq('65 0') + expect(d.root).to eq('1 0') end it 'sports objs with properly recomputed attributes' do pa = @d.page(1) @@ -110,11 +108,11 @@ context 'additions' do before :each do - @d = Podoff.load('pdfs/t0.pdf') + @d = Podoff.load('pdfs/t0.pdf', 'utf-8') end describe '#add_base_font' do it 'adds a new /Font obj to the document' do @@ -130,13 +128,15 @@ expect(fo.source).to eq( '7 0 obj <</Type /Font /Subtype /Type1 /BaseFont /Helvetica>> endobj') s = @d.write(:string) - d = Podoff.parse(s) + d = Podoff.parse(s, 'utf-8') - expect(d.xref).to eq(680) + expect(d.xref).to eq(686) + + expect(s).to be_a_valid_pdf end it 'doesn\'t mind a slash in front of the font name' do fo = @d.add_base_font('/Helvetica') @@ -173,13 +173,17 @@ BT 70 80 Td /Helvetica 35 Tf (Hello!) Tj ET endstream endobj }.strip) - d = Podoff.parse(@d.write(:string)) + s = @d.write(:string) - expect(d.xref).to eq(705) + expect(s).to be_a_valid_pdf + + d = Podoff.parse(s, 'utf-8') + + expect(d.xref).to eq(711) end it 'accepts a block' do st = @@ -200,14 +204,18 @@ BT /Helvetica 35 Tf 40 50 Td (sixty there) Tj ET endstream endobj }.strip) - d = Podoff.parse(@d.write(:string)) + s = @d.write(:string) - expect(d.source.index('<</Length 97>>')).to eq(618) - expect(d.xref).to eq(757) + expect(s).to be_a_valid_pdf + + d = Podoff.parse(s, 'utf-8') + + expect(d.source.index('<</Length 97>>')).to eq(625) + expect(d.xref).to eq(763) end it 'returns the open stream when no arg given' do st = @d.add_stream @@ -248,16 +256,15 @@ expect(re.source).not_to equal(pa.source) end it 'recomputes the attributes correctly' do - d = Podoff.load('pdfs/qdocument0.pdf') + d = Podoff.load('pdfs/qdocument0.pdf', 'iso-8859-1') pa = d.re_add(d.page(1)) - expect(pa.attributes).to eq( - { type: '/Page', contents: '151 0 R', pagenum: '1' }) + expect(pa.attributes).to eq({ type: '/Page', contents: '151 0 R' }) end end end describe '#write' do @@ -273,11 +280,11 @@ expect(lines.last).to match(/^%%EOF$/) end it 'writes open streams as well' do - d = Podoff.load('pdfs/t0.pdf') + d = Podoff.load('pdfs/t0.pdf', 'utf-8') pa = d.re_add(d.page(1)) st = d.add_stream st.bt(10, 20, 'hello open stream') pa.insert_contents(st) @@ -291,49 +298,51 @@ stream BT 10 20 Td (hello open stream) Tj ET endstream endobj }.strip) - ).to eq(722) + ).to eq(729) end it 'writes a proper xref table' do - d = Podoff.load('pdfs/t0.pdf') + d = Podoff.load('pdfs/t0.pdf', 'utf-8') pa = d.re_add(d.page(1)) st = d.add_stream st.bt(10, 20, 'hello open stream') pa.insert_contents(st) s = d.write(:string) - expect(s[808..-1].strip).to eq(%{ + expect(s).to be_a_valid_pdf + + expect(s[814..-1].strip).to eq(%{ xref 0 1 -0000000000 65535 f +0000000000 65535 f 3 1 -0000000611 00000 n +0000000617 00000 n 7 1 -0000000723 00000 n +0000000729 00000 n trailer << -/Prev 414 -/Size 7 +/Prev 413 +/Size 8 /Root 1 0 R >> -startxref 809 +startxref 815 %%EOF }.strip) end end describe '#rewrite' do it 'rewrites a document in one go' do - d = Podoff.load('pdfs/t2.pdf') + d = Podoff.load('pdfs/t2.pdf', 'utf-8') s = d.rewrite(:string) expect(s.strip).to eq(%{ %PDF-1.4 @@ -359,26 +368,48 @@ BT /F1 24 Tf 175 520 Td (Smurf Megane)Tj ET endstream endobj xref 0 1 -0000000000 65535 f +0000000000 65535 f 1 7 -0000000010 00000 n -0000000057 00000 n -0000000112 00000 n -0000000222 00000 n -0000000261 00000 n -0000000329 00000 n -0000000420 00000 n +0000000009 00000 n +0000000056 00000 n +0000000111 00000 n +0000000221 00000 n +0000000260 00000 n +0000000328 00000 n +0000000419 00000 n trailer << -/Size 7 +/Size 8 /Root 1 0 R >> -startxref 511 +startxref 510 %%EOF }.strip) + + expect(s).to be_a_valid_pdf + end + end + + describe '#extract_refs' do + + it 'extracts a ref' do + + expect( + Podoff::Document.allocate.send(:extract_refs, '17 0 R') + ).to eq([ '17 0' ]) + expect( + Podoff::Document.allocate.send(:extract_refs, ' 17 0 R') + ).to eq([ '17 0' ]) + end + + it 'extracts a list of ref' do + + expect( + Podoff::Document.allocate.send(:extract_refs, '[17 0 R 6 0 R]') + ).to eq([ '17 0', '6 0' ]) end end end