origamindee 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (139) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +89 -0
  3. data/COPYING.LESSER +165 -0
  4. data/README.md +131 -0
  5. data/bin/config/pdfcop.conf.yml +236 -0
  6. data/bin/pdf2pdfa +87 -0
  7. data/bin/pdf2ruby +333 -0
  8. data/bin/pdfcop +476 -0
  9. data/bin/pdfdecompress +97 -0
  10. data/bin/pdfdecrypt +91 -0
  11. data/bin/pdfencrypt +113 -0
  12. data/bin/pdfexplode +223 -0
  13. data/bin/pdfextract +277 -0
  14. data/bin/pdfmetadata +143 -0
  15. data/bin/pdfsh +12 -0
  16. data/bin/shell/console.rb +128 -0
  17. data/bin/shell/hexdump.rb +59 -0
  18. data/bin/shell/irbrc +69 -0
  19. data/examples/README.md +34 -0
  20. data/examples/attachments/attachment.rb +38 -0
  21. data/examples/attachments/nested_document.rb +51 -0
  22. data/examples/encryption/encryption.rb +28 -0
  23. data/examples/events/events.rb +72 -0
  24. data/examples/flash/flash.rb +37 -0
  25. data/examples/flash/helloworld.swf +0 -0
  26. data/examples/forms/javascript.rb +54 -0
  27. data/examples/forms/xfa.rb +115 -0
  28. data/examples/javascript/hello_world.rb +22 -0
  29. data/examples/javascript/js_emulation.rb +54 -0
  30. data/examples/loop/goto.rb +32 -0
  31. data/examples/loop/named.rb +33 -0
  32. data/examples/signature/signature.rb +65 -0
  33. data/examples/uri/javascript.rb +56 -0
  34. data/examples/uri/open-uri.rb +21 -0
  35. data/examples/uri/submitform.rb +47 -0
  36. data/lib/origami/3d.rb +364 -0
  37. data/lib/origami/acroform.rb +321 -0
  38. data/lib/origami/actions.rb +318 -0
  39. data/lib/origami/annotations.rb +711 -0
  40. data/lib/origami/array.rb +242 -0
  41. data/lib/origami/boolean.rb +90 -0
  42. data/lib/origami/catalog.rb +418 -0
  43. data/lib/origami/collections.rb +144 -0
  44. data/lib/origami/compound.rb +161 -0
  45. data/lib/origami/destinations.rb +252 -0
  46. data/lib/origami/dictionary.rb +192 -0
  47. data/lib/origami/encryption.rb +1084 -0
  48. data/lib/origami/extensions/fdf.rb +347 -0
  49. data/lib/origami/extensions/ppklite.rb +422 -0
  50. data/lib/origami/filespec.rb +197 -0
  51. data/lib/origami/filters/ascii.rb +211 -0
  52. data/lib/origami/filters/ccitt/tables.rb +267 -0
  53. data/lib/origami/filters/ccitt.rb +357 -0
  54. data/lib/origami/filters/crypt.rb +38 -0
  55. data/lib/origami/filters/dct.rb +54 -0
  56. data/lib/origami/filters/flate.rb +69 -0
  57. data/lib/origami/filters/jbig2.rb +57 -0
  58. data/lib/origami/filters/jpx.rb +47 -0
  59. data/lib/origami/filters/lzw.rb +170 -0
  60. data/lib/origami/filters/predictors.rb +292 -0
  61. data/lib/origami/filters/runlength.rb +129 -0
  62. data/lib/origami/filters.rb +364 -0
  63. data/lib/origami/font.rb +196 -0
  64. data/lib/origami/functions.rb +79 -0
  65. data/lib/origami/graphics/colors.rb +230 -0
  66. data/lib/origami/graphics/instruction.rb +98 -0
  67. data/lib/origami/graphics/path.rb +182 -0
  68. data/lib/origami/graphics/patterns.rb +174 -0
  69. data/lib/origami/graphics/render.rb +62 -0
  70. data/lib/origami/graphics/state.rb +149 -0
  71. data/lib/origami/graphics/text.rb +225 -0
  72. data/lib/origami/graphics/xobject.rb +918 -0
  73. data/lib/origami/graphics.rb +38 -0
  74. data/lib/origami/header.rb +75 -0
  75. data/lib/origami/javascript.rb +713 -0
  76. data/lib/origami/linearization.rb +330 -0
  77. data/lib/origami/metadata.rb +172 -0
  78. data/lib/origami/name.rb +135 -0
  79. data/lib/origami/null.rb +65 -0
  80. data/lib/origami/numeric.rb +181 -0
  81. data/lib/origami/obfuscation.rb +245 -0
  82. data/lib/origami/object.rb +760 -0
  83. data/lib/origami/optionalcontent.rb +183 -0
  84. data/lib/origami/outline.rb +54 -0
  85. data/lib/origami/outputintents.rb +85 -0
  86. data/lib/origami/page.rb +722 -0
  87. data/lib/origami/parser.rb +269 -0
  88. data/lib/origami/parsers/fdf.rb +56 -0
  89. data/lib/origami/parsers/pdf/lazy.rb +176 -0
  90. data/lib/origami/parsers/pdf/linear.rb +122 -0
  91. data/lib/origami/parsers/pdf.rb +118 -0
  92. data/lib/origami/parsers/ppklite.rb +57 -0
  93. data/lib/origami/pdf.rb +1108 -0
  94. data/lib/origami/reference.rb +134 -0
  95. data/lib/origami/signature.rb +702 -0
  96. data/lib/origami/stream.rb +705 -0
  97. data/lib/origami/string.rb +444 -0
  98. data/lib/origami/template/patterns.rb +56 -0
  99. data/lib/origami/template/widgets.rb +151 -0
  100. data/lib/origami/trailer.rb +190 -0
  101. data/lib/origami/tree.rb +62 -0
  102. data/lib/origami/version.rb +23 -0
  103. data/lib/origami/webcapture.rb +100 -0
  104. data/lib/origami/xfa/config.rb +453 -0
  105. data/lib/origami/xfa/connectionset.rb +146 -0
  106. data/lib/origami/xfa/datasets.rb +49 -0
  107. data/lib/origami/xfa/localeset.rb +42 -0
  108. data/lib/origami/xfa/package.rb +59 -0
  109. data/lib/origami/xfa/pdf.rb +73 -0
  110. data/lib/origami/xfa/signature.rb +42 -0
  111. data/lib/origami/xfa/sourceset.rb +43 -0
  112. data/lib/origami/xfa/stylesheet.rb +44 -0
  113. data/lib/origami/xfa/template.rb +1691 -0
  114. data/lib/origami/xfa/xdc.rb +42 -0
  115. data/lib/origami/xfa/xfa.rb +146 -0
  116. data/lib/origami/xfa/xfdf.rb +43 -0
  117. data/lib/origami/xfa/xmpmeta.rb +43 -0
  118. data/lib/origami/xfa.rb +62 -0
  119. data/lib/origami/xreftable.rb +557 -0
  120. data/lib/origami.rb +47 -0
  121. data/test/dataset/calc.pdf +85 -0
  122. data/test/dataset/crypto.pdf +36 -0
  123. data/test/dataset/empty.pdf +49 -0
  124. data/test/test_actions.rb +27 -0
  125. data/test/test_annotations.rb +68 -0
  126. data/test/test_forms.rb +30 -0
  127. data/test/test_native_types.rb +83 -0
  128. data/test/test_object_tree.rb +33 -0
  129. data/test/test_pages.rb +60 -0
  130. data/test/test_pdf.rb +20 -0
  131. data/test/test_pdf_attachment.rb +34 -0
  132. data/test/test_pdf_create.rb +24 -0
  133. data/test/test_pdf_encrypt.rb +102 -0
  134. data/test/test_pdf_parse.rb +134 -0
  135. data/test/test_pdf_parse_lazy.rb +69 -0
  136. data/test/test_pdf_sign.rb +97 -0
  137. data/test/test_streams.rb +184 -0
  138. data/test/test_xrefs.rb +67 -0
  139. metadata +280 -0
@@ -0,0 +1,36 @@
1
+ %PDF-1.7
2
+ 1 0 obj
3
+ <</Type /Catalog/Pages 3 0 R/Extensions <</ADBE <</BaseVersion /1.7/ExtensionLevel 3>>>>>>
4
+ endobj
5
+ 3 0 obj
6
+ <</Type /Pages/Kids [4 0 R]/Count 1>>
7
+ endobj
8
+ 4 0 obj
9
+ <</Type /Page/Contents 5 0 R/Parent 3 0 R/MediaBox [0 0 795 842]/Resources <</Font <</F1 6 0 R>>>>>>
10
+ endobj
11
+ 5 0 obj
12
+ <</Length 80>>stream
13
+ � CH4���C�]d��5.j(�6�\Ì���>�H�1[L��F��e�5�{6��]�}q�8x6n�NЋ�5�SR��'dY
14
+ endstream
15
+ endobj
16
+ 6 0 obj
17
+ <</Type /Font/Subtype /Type1/Name /F1/BaseFont /Helvetica>>
18
+ endobj
19
+ 7 0 obj
20
+ <</Filter /Standard/V 5/R 5/Length 256/P -1/EncryptMetadata true/CF <</StdCF <</AuthEvent /DocOpen/CFM /AESV3/Length 32>>>>/StrF /StdCF/StmF /StdCF/U (!�iцj�e�lO��l��lL�5g*=�hM�ec c���<:\r����Y�)/O (ϧ%Ǧ�7|��X�I]:1@���NZ��jcM�P~�a"���2z)/UE (��i������s�D�ۑ����OrnZ�M��`&)/OE (׉�s\)��x{���3T����֏�;s����H)/Perms (h���"�X��� �y��)>>
21
+ endobj
22
+ xref
23
+ 0 2
24
+ 0000000000 65535 f
25
+ 0000000009 00000 n
26
+ 3 5
27
+ 0000000115 00000 n
28
+ 0000000168 00000 n
29
+ 0000000284 00000 n
30
+ 0000000411 00000 n
31
+ 0000000486 00000 n
32
+ trailer
33
+ <</Root 1 0 R/Size 7/ID [(ab6b04434692cfa11b430c824105ff35) (ab6b04434692cfa11b430c824105ff35)]/Encrypt 7 0 R>>
34
+ startxref
35
+ 860
36
+ %%EOF
@@ -0,0 +1,49 @@
1
+ %PDF-1.0
2
+ 1 0 obj
3
+ <<
4
+ /Pages 2 0 R
5
+ /Type /Catalog
6
+ >>
7
+ endobj
8
+ 2 0 obj
9
+ <<
10
+ /Kids [ 3 0 R ]
11
+ /Count 1
12
+ /Type /Pages
13
+ >>
14
+ endobj
15
+ 3 0 obj
16
+ <<
17
+ /Type /Page
18
+ /Parent 2 0 R
19
+ /MediaBox [ 0 0 795 842 ]
20
+ /Resources <<
21
+ /Font <<
22
+ /F1 4 0 R
23
+ >>
24
+ >>
25
+ >>
26
+ endobj
27
+ 4 0 obj
28
+ <<
29
+ /Name /F1
30
+ /Subtype /Type1
31
+ /Type /Font
32
+ /BaseFont /Helvetica
33
+ >>
34
+ endobj
35
+ xref
36
+ 0 5
37
+ 0000000000 65535 f
38
+ 0000000010 00000 n
39
+ 0000000067 00000 n
40
+ 0000000136 00000 n
41
+ 0000000272 00000 n
42
+ trailer
43
+ <<
44
+ /Root 1 0 R
45
+ /Size 5
46
+ >>
47
+ startxref
48
+ 364
49
+ %%EOF
@@ -0,0 +1,27 @@
1
+ require 'minitest/autorun'
2
+
3
+ class TestActions < MiniTest::Test
4
+ def setup
5
+ @target = PDF.new
6
+ @page = Page.new
7
+ @action = Action::JavaScript "app.alert(null);"
8
+ end
9
+
10
+ def test_pdf_actions
11
+ @target.onDocumentOpen @action
12
+ @target.onDocumentClose @action
13
+ @target.onDocumentPrint @action
14
+
15
+ assert_equal @target.Catalog.OpenAction, @action
16
+ assert_equal @target.Catalog.AA.WC, @action
17
+ assert_equal @target.Catalog.AA.WP, @action
18
+ end
19
+
20
+ def test_page_actions
21
+ @page.onOpen @action
22
+ @page.onClose @action
23
+
24
+ assert_equal @page.AA.O, @action
25
+ assert_equal @page.AA.C, @action
26
+ end
27
+ end
@@ -0,0 +1,68 @@
1
+ require 'minitest/autorun'
2
+ require 'stringio'
3
+
4
+ class TestAnnotations < Minitest::Test
5
+ def setup
6
+ @target = PDF.new
7
+ @page = Page.new
8
+ @action = Action::JavaScript["app.alert(null);"]
9
+ @output = StringIO.new
10
+
11
+ @types = [
12
+ Annotation::Circle, Annotation::Square,
13
+ Annotation::Text, Annotation::Link,
14
+ Annotation::FileAttachment, Annotation::Screen,
15
+ Annotation::Sound, Annotation::Widget::CheckBox,
16
+ Annotation::Widget::Radio, Annotation::Widget::Text,
17
+ Annotation::Widget::ComboBox, Annotation::Widget::ListBox,
18
+ Annotation::Widget::Signature
19
+ ]
20
+ end
21
+
22
+ def test_annotations
23
+ @target.append_page @page
24
+
25
+ annotations = @types.map(&:new)
26
+ annotations.each do |annotation|
27
+ @page.add_annotation(annotation)
28
+ end
29
+
30
+ @page.each_annotation do |annotation|
31
+ assert_kind_of Annotation, annotation
32
+
33
+ assert annotations.include?(annotation)
34
+ end
35
+
36
+ assert_equal @page.annotations.size, annotations.size
37
+
38
+ @target.save(@output)
39
+ end
40
+
41
+ def test_annotation_actions
42
+ screen = Annotation::Screen.new
43
+
44
+ @page.add_annotation screen
45
+
46
+ screen.onMouseOver @action
47
+ screen.onMouseOut @action
48
+ screen.onMouseDown @action
49
+ screen.onMouseUp @action
50
+ screen.onFocus @action
51
+ screen.onBlur @action
52
+ screen.onPageOpen @action
53
+ screen.onPageClose @action
54
+ screen.onPageVisible @action
55
+ screen.onPageInvisible @action
56
+
57
+ assert_equal screen.AA.E, @action
58
+ assert_equal screen.AA.X, @action
59
+ assert_equal screen.AA.D, @action
60
+ assert_equal screen.AA.U, @action
61
+ assert_equal screen.AA.Fo, @action
62
+ assert_equal screen.AA.Bl, @action
63
+ assert_equal screen.AA.PO, @action
64
+ assert_equal screen.AA.PC, @action
65
+ assert_equal screen.AA.PV, @action
66
+ assert_equal screen.AA.PI, @action
67
+ end
68
+ end
@@ -0,0 +1,30 @@
1
+ require 'minitest/autorun'
2
+
3
+ class TestForms < Minitest::Test
4
+ def setup
5
+ @target = PDF.new
6
+ @target.append_page
7
+ end
8
+
9
+ def test_create_acroform
10
+ fields = [
11
+ Annotation::Widget::Text.new.set_name('text'),
12
+ Annotation::Widget::PushButton.new.set_name('button')
13
+ ]
14
+
15
+ @target.create_form(*fields)
16
+
17
+ assert @target.form?
18
+ assert_kind_of InteractiveForm, @target.Catalog.AcroForm
19
+ assert_equal @target.fields.length, fields.length
20
+
21
+ @target.each_field do |field|
22
+ assert_kind_of Field, field
23
+ assert %w{text button}.include?(field.name)
24
+ end
25
+
26
+ assert_nil @target.get_field('not_existent_field')
27
+ assert_kind_of Field, @target.get_field('button')
28
+ assert_kind_of Field, @target.get_field('text')
29
+ end
30
+ end
@@ -0,0 +1,83 @@
1
+ require 'minitest/autorun'
2
+ require 'strscan'
3
+
4
+ class TestPDFCreate < Minitest::Test
5
+ using Origami::TypeConversion
6
+
7
+ def test_type_string
8
+ assert_kind_of Origami::String, "".to_o
9
+ assert_kind_of ::String, "".to_o.value
10
+
11
+ assert_equal "<616263>", HexaString.new("abc").to_s
12
+ assert_equal "(test)", LiteralString.new("test").to_s
13
+ assert_equal '(\(\(\(\)\)\)\))', LiteralString.new("((())))").to_s
14
+ assert_equal "abc", "abc".to_s.to_o
15
+ end
16
+
17
+ def test_string_encoding
18
+ str = LiteralString.new("test")
19
+
20
+ assert_equal Origami::String::Encoding::PDFDocEncoding, str.encoding
21
+ assert_equal "UTF-8", str.to_utf8.encoding.to_s
22
+
23
+ assert_equal "\xFE\xFF\x00t\x00e\x00s\x00t".b, str.to_utf16be
24
+ assert_equal Origami::String::Encoding::UTF16BE, str.to_utf16be.to_o.encoding
25
+ assert_equal str, str.to_utf16be.to_o.to_pdfdoc
26
+ end
27
+
28
+ def test_type_null
29
+ assert_instance_of Null, nil.to_o
30
+ assert_nil Null.new.value
31
+ assert_equal Null.new.to_s, "null"
32
+ end
33
+
34
+ def test_type_name
35
+ assert_instance_of Name, :test.to_o
36
+ assert_instance_of Symbol, :test.to_o.value
37
+
38
+ assert_equal "/test", Name.new(:test).to_s
39
+ assert_equal "/#20#23#09#0d#0a#00#5b#5d#3c#3e#28#29#25#2f", Name.new(" #\t\r\n\0[]<>()%/").to_s
40
+ assert_equal " #\t\r\n\0[]<>()%/", Name.new(" #\t\r\n\0[]<>()%/").value.to_s
41
+ end
42
+
43
+ def test_type_boolean
44
+ assert_instance_of Boolean, true.to_o
45
+ assert_instance_of Boolean, false.to_o
46
+ assert Boolean.new(true).value
47
+ refute Boolean.new(false).value
48
+ assert_equal "true", true.to_o.to_s
49
+ assert_equal "false", false.to_o.to_s
50
+ end
51
+
52
+ def test_type_numeric
53
+ assert_instance_of Origami::Real, Math::PI.to_o
54
+ assert_instance_of Origami::Integer, 1.to_o
55
+
56
+ assert_equal "1.8", Origami::Real.new(1.8).to_s
57
+ assert_equal "100", Origami::Integer.new(100).to_s
58
+ assert_equal 1.8, 1.8.to_o.value
59
+ assert_equal 100, 100.to_o.value
60
+ end
61
+
62
+ def test_type_array
63
+ array = [1, "a", [], {}, :test, nil, true, 3.14]
64
+
65
+ assert_instance_of Origami::Array, [].to_o
66
+ assert_instance_of ::Array, [].to_o.value
67
+
68
+ assert_equal array, array.to_o.value
69
+ assert_equal "[1 (a) [] <<>> /test null true 3.14]", array.to_o.to_s
70
+ assert array.to_o.all? {|o| o.is_a? Origami::Object}
71
+ end
72
+
73
+ def test_type_dictionary
74
+ assert_instance_of Origami::Dictionary, {}.to_o
75
+ assert_instance_of Hash, {}.to_o.value
76
+
77
+ dict = {a: 1, b: false, c: nil, d: "abc", e: :abc, f: []}
78
+
79
+ assert_equal "<</a 1/b false/c null/d (abc)/e /abc/f []>>", dict.to_o.to_s(indent: 0)
80
+ assert_equal dict, dict.to_o.value
81
+ assert dict.to_o.all?{|k,v| k.is_a?(Name) and v.is_a?(Origami::Object)}
82
+ end
83
+ end
@@ -0,0 +1,33 @@
1
+ require 'minitest/autorun'
2
+ require 'stringio'
3
+
4
+ class TestPDFObjectTree < Minitest::Test
5
+
6
+ def setup
7
+ @pdf = PDF.new.append_page
8
+ @contents = ContentStream.new("abc")
9
+ @pdf.pages.first.Contents = @contents
10
+ @pdf.Catalog.Loop = @pdf.Catalog
11
+ @pdf.save StringIO.new
12
+ end
13
+
14
+ def test_pdf_object_tree
15
+ assert_instance_of Catalog, @pdf.Catalog
16
+ assert_nil @pdf.Catalog.parent
17
+
18
+ @pdf.each_object(recursive: true) do |obj|
19
+ assert_kind_of Origami::Object, obj
20
+ assert_equal obj.document, @pdf
21
+
22
+ unless obj.indirect?
23
+ assert_kind_of Origami::Object, obj.parent
24
+ assert_equal obj.parent.document, @pdf
25
+ end
26
+ end
27
+
28
+ enum = @pdf.each_object(recursive: true)
29
+ assert_kind_of Enumerator, enum
30
+ assert enum.include?(@pdf.Catalog.Pages)
31
+ assert enum.include?(@contents.dictionary)
32
+ end
33
+ end
@@ -0,0 +1,60 @@
1
+ require 'minitest/autorun'
2
+ require 'stringio'
3
+
4
+ class TestPages < Minitest::Test
5
+ def setup
6
+ @target = PDF.new
7
+ @output = StringIO.new
8
+ end
9
+
10
+ def test_append_page
11
+ p1, p2, p3 = Page.new, Page.new, Page.new
12
+
13
+ @target.append_page p1
14
+ @target.append_page p2
15
+ @target.append_page p3
16
+
17
+ assert_equal @target.pages.count, 3
18
+
19
+ assert_equal @target.get_page(1), p1
20
+ assert_equal @target.get_page(2), p2
21
+ assert_equal @target.get_page(3), p3
22
+
23
+ assert_raises(IndexError) { @target.get_page(0) }
24
+ assert_raises(IndexError) { @target.get_page(4) }
25
+
26
+ assert_equal @target.Catalog.Pages, p1.Parent
27
+ assert_equal @target.Catalog.Pages, p2.Parent
28
+ assert_equal @target.Catalog.Pages, p3.Parent
29
+
30
+ @target.save(@output)
31
+
32
+ assert_equal @target.Catalog.Pages.Count, 3
33
+ assert_equal @target.pages, [p1, p2, p3]
34
+ assert_equal @target.each_page.to_a, [p1, p2, p3]
35
+ end
36
+
37
+ def test_insert_page
38
+ pages = Array.new(10) { Page.new }
39
+
40
+ pages.each_with_index do |page, index|
41
+ @target.insert_page(index + 1, page)
42
+ end
43
+
44
+ assert_equal @target.pages, pages
45
+
46
+ new_page = Page.new
47
+ @target.insert_page(1, new_page)
48
+ assert_equal @target.get_page(1), new_page
49
+
50
+ assert_raises(IndexError) { @target.insert_page(0, Page.new) }
51
+ assert_raises(IndexError) { @target.insert_page(1000, Page.new) }
52
+ end
53
+
54
+ def test_example_write_page
55
+ @target.append_page
56
+ @target.pages.first.write 'Hello, world!', size: 30
57
+ @target.save(@output)
58
+ assert_equal @target.Catalog.Pages.Count, 1
59
+ end
60
+ end
data/test/test_pdf.rb ADDED
@@ -0,0 +1,20 @@
1
+ require 'minitest/autorun'
2
+
3
+ $:.unshift File.join(__dir__, "..", "lib")
4
+ require 'origami'
5
+ include Origami
6
+
7
+ require_relative 'test_native_types'
8
+ require_relative 'test_pdf_parse'
9
+ require_relative 'test_pdf_parse_lazy'
10
+ require_relative 'test_pdf_create'
11
+ require_relative 'test_streams'
12
+ require_relative 'test_pdf_encrypt'
13
+ require_relative 'test_pdf_sign'
14
+ require_relative 'test_pdf_attachment'
15
+ require_relative 'test_pages'
16
+ require_relative 'test_actions'
17
+ require_relative 'test_annotations'
18
+ require_relative 'test_forms'
19
+ require_relative 'test_xrefs'
20
+ require_relative 'test_object_tree'
@@ -0,0 +1,34 @@
1
+ require 'minitest/autorun'
2
+ require 'stringio'
3
+
4
+ class TestAttachment < Minitest::Test
5
+ def setup
6
+ @target = PDF.new
7
+ @attachment = StringIO.new("test")
8
+ @output = StringIO.new
9
+ end
10
+
11
+ def test_attach_file
12
+ @target.attach_file(@attachment, name: "foo.bar", filter: :A85)
13
+
14
+ @target.save(@output)
15
+
16
+ @output = @output.reopen(@output.string, "r")
17
+ pdf = PDF.read(@output, ignore_errors: false, verbosity: Parser::VERBOSE_QUIET)
18
+
19
+ assert_equal pdf.each_named_embedded_file.count, 1
20
+ assert_nil pdf.get_embedded_file_by_name("foo.baz")
21
+
22
+ file = pdf.get_embedded_file_by_name('foo.bar')
23
+ refute_equal file, nil
24
+
25
+ assert file.key?(:EF)
26
+ assert file.EF.key?(:F)
27
+
28
+ stream = file.EF.F
29
+ assert stream.is_a?(Stream)
30
+
31
+ assert_equal stream.dictionary.Filter, :A85
32
+ assert_equal stream.data, "test"
33
+ end
34
+ end
@@ -0,0 +1,24 @@
1
+ require 'minitest/autorun'
2
+ require 'stringio'
3
+
4
+ class TestPDFCreate < Minitest::Test
5
+
6
+ def setup
7
+ @output = StringIO.new
8
+ end
9
+
10
+ def test_pdf_create
11
+ pdf = PDF.new
12
+
13
+ null = Null.new
14
+ pdf << null
15
+
16
+ pdf.save(@output)
17
+
18
+ assert null.indirect?
19
+ assert_equal null.reference.solve, null
20
+ assert pdf.root_objects.include?(null)
21
+ assert_equal pdf.revisions.first.body[null.reference], null
22
+ assert_equal null.reference.solve, null
23
+ end
24
+ end
@@ -0,0 +1,102 @@
1
+ require 'minitest/autorun'
2
+ require 'stringio'
3
+ require 'openssl'
4
+
5
+ class TestEncryption < Minitest::Test
6
+ def setup
7
+ @target = PDF.read(File.join(__dir__, "dataset/calc.pdf"),
8
+ ignore_errors: false, verbosity: Parser::VERBOSE_QUIET)
9
+ @output = StringIO.new
10
+ end
11
+
12
+ def test_encrypt_rc4_40b
13
+ return if OpenSSL::VERSION[0].to_i > 2
14
+ @output.string = ""
15
+ @target.encrypt(cipher: 'rc4', key_size: 40).save(@output)
16
+ end
17
+
18
+ def test_encrypt_rc4_128b
19
+ return if OpenSSL::VERSION[0].to_i > 2
20
+ @output.string = ""
21
+ @target.encrypt(cipher: 'rc4', key_size: 128).save(@output)
22
+ end
23
+
24
+ def test_encrypt_aes_128b
25
+ return if OpenSSL::VERSION[0].to_i > 2
26
+ @output.string = ""
27
+ @target.encrypt(cipher: 'aes', key_size: 128).save(@output)
28
+ end
29
+
30
+ def test_decrypt_rc4_40b
31
+ return if OpenSSL::VERSION[0].to_i > 2
32
+ @output.string = ""
33
+
34
+ pdf = PDF.new.encrypt(cipher: 'rc4', key_size: 40)
35
+ pdf.Catalog[:Test] = "test"
36
+ pdf.save(@output)
37
+
38
+ refute_equal pdf.Catalog[:Test], "test"
39
+
40
+ @output = @output.reopen(@output.string, "r")
41
+ pdf = PDF.read(@output, ignore_errors: false, verbosity: Parser::VERBOSE_QUIET)
42
+
43
+ assert_equal pdf.Catalog[:Test], "test"
44
+ end
45
+
46
+ def test_decrypt_rc4_128b
47
+ return if OpenSSL::VERSION[0].to_i > 2
48
+ @output.string = ""
49
+ pdf = PDF.new.encrypt(cipher: 'rc4', key_size: 128)
50
+ pdf.Catalog[:Test] = "test"
51
+ pdf.save(@output)
52
+
53
+ refute_equal pdf.Catalog[:Test], "test"
54
+
55
+ @output.reopen(@output.string, "r")
56
+ pdf = PDF.read(@output, ignore_errors: false, verbosity: Parser::VERBOSE_QUIET)
57
+
58
+ assert_equal pdf.Catalog[:Test], "test"
59
+ end
60
+
61
+ def test_decrypt_aes_128b
62
+ return if OpenSSL::VERSION[0].to_i > 2
63
+ @output.string = ""
64
+ pdf = PDF.new.encrypt(cipher: 'aes', key_size: 128)
65
+ pdf.Catalog[:Test] = "test"
66
+ pdf.save(@output)
67
+
68
+ refute_equal pdf.Catalog[:Test], "test"
69
+
70
+ @output = @output.reopen(@output.string, "r")
71
+ pdf = PDF.read(@output, ignore_errors: false, verbosity: Parser::VERBOSE_QUIET)
72
+
73
+ assert_equal pdf.Catalog[:Test], "test"
74
+ end
75
+
76
+ def test_decrypt_aes_256b
77
+ @output.string = ""
78
+ pdf = PDF.new.encrypt(cipher: 'aes', key_size: 256)
79
+ pdf.Catalog[:Test] = "test"
80
+ pdf.save(@output)
81
+
82
+ refute_equal pdf.Catalog[:Test], "test"
83
+
84
+ @output = @output.reopen(@output.string, "r")
85
+ pdf = PDF.read(@output, ignore_errors: false, verbosity: Parser::VERBOSE_QUIET)
86
+
87
+ assert_equal pdf.Catalog[:Test], "test"
88
+ end
89
+
90
+ def test_crypt_filter
91
+ @output.string = ""
92
+ pdf = PDF.new.encrypt(cipher: 'aes', key_size: 256)
93
+
94
+ pdf.Catalog[:S1] = Stream.new("test", :Filter => :Crypt)
95
+ pdf.Catalog[:S2] = Stream.new("test")
96
+
97
+ pdf.save(@output)
98
+
99
+ assert_equal pdf.Catalog.S1.encoded_data, "test"
100
+ refute_equal pdf.Catalog.S2.encoded_data, "test"
101
+ end
102
+ end