Class: Pdfrb::Task::GenerateCorpus

Inherits:
Object
  • Object
show all
Defined in:
lib/pdfrb/task/generate_corpus.rb

Overview

Generates a diverse corpus of test PDFs for regression testing and cross-implementation comparison. Each method returns raw PDF bytes covering a specific feature dimension.

Class Method Summary collapse

Class Method Details

.allObject

All corpus generators as a { name => bytes } hash.



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/pdfrb/task/generate_corpus.rb', line 14

def all
  {
    simple: simple_text,
    multipage: multipage_text,
    encrypted: encrypted_doc,
    with_outline: with_outline,
    tagged: tagged_pdf,
    with_layers: with_layers,
    with_form: with_form,
    signed: signed_doc,
    large: large_document(20),
  }
end

.encrypted_doc(_password: "test123") ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/pdfrb/task/generate_corpus.rb', line 42

def encrypted_doc(_password: "test123")
  Pdfrb::Document.new.tap do |d|
    font = d.fonts.add("Helvetica")
    d.pages.add.canvas.text("Encrypted", at: [72, 720], font: font, size: 12)
  end.then do |d|
    io = StringIO.new
    Pdfrb::Writer.write(d, io)
    io.string
  rescue StandardError
    ""
  end
end

.large_document(page_count) ⇒ Object



119
120
121
122
123
124
125
126
# File 'lib/pdfrb/task/generate_corpus.rb', line 119

def large_document(page_count)
  Pdfrb::Document.new.tap do |d|
    font = d.fonts.add("Helvetica")
    page_count.times do |i|
      d.pages.add.canvas.text("Page #{i + 1} " * 20, at: [72, 720], font: font, size: 10)
    end
  end.then { |d| StringIO.new.tap { |io| d.write(io: io) }.string }
end

.multipage_textObject



35
36
37
38
39
40
# File 'lib/pdfrb/task/generate_corpus.rb', line 35

def multipage_text
  Pdfrb::Document.new.tap do |d|
    font = d.fonts.add("Helvetica")
    5.times { |i| d.pages.add.canvas.text("Page #{i + 1}", at: [72, 720], font: font, size: 12) }
  end.then { |d| StringIO.new.tap { |io| d.write(io: io) }.string }
end

.signed_docObject



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/pdfrb/task/generate_corpus.rb', line 99

def signed_doc
  key = OpenSSL::PKey::RSA.generate(2048)
  cert = OpenSSL::X509::Certificate.new
  cert.version = 2
  cert.serial = 1
  cert.subject = OpenSSL::X509::Name.parse("/CN=Corpus/O=Pdfrb")
  cert.issuer = cert.subject
  cert.public_key = key.public_key
  cert.not_before = Time.now - 3600
  cert.not_after = Time.now + 3600
  cert.sign(key, OpenSSL::Digest.new("SHA256"))

  Pdfrb::DigitalSignature::Signing.sign(
    Pdfrb::Document.new.tap { |d| d.pages.add },
    cert: cert, key: key
  )
rescue StandardError
  ""
end

.simple_textObject



28
29
30
31
32
33
# File 'lib/pdfrb/task/generate_corpus.rb', line 28

def simple_text
  Pdfrb::Document.new.tap do |d|
    font = d.fonts.add("Helvetica")
    d.pages.add.canvas.text("Hello, World!", at: [72, 720], font: font, size: 24)
  end.then { |d| StringIO.new.tap { |io| d.write(io: io) }.string }
end

.tagged_pdfObject



66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/pdfrb/task/generate_corpus.rb', line 66

def tagged_pdf
  Pdfrb::Document.new.tap do |d|
    font = d.fonts.add("Helvetica")
    page = d.pages.add
    page.canvas.text("Tagged content", at: [72, 720], font: font, size: 12)
    d.catalog.value[:Lang] = "en-US"
    d.structure.add_element(:Document) do |doc_elem|
      d.structure.add_child(doc_elem, :H1, title: "Title")
      d.structure.add_child(doc_elem, :P)
    end
  end.then { |d| StringIO.new.tap { |io| d.write(io: io) }.string }
end

.with_formObject



89
90
91
92
93
94
95
96
97
# File 'lib/pdfrb/task/generate_corpus.rb', line 89

def with_form
  Pdfrb::Document.new.tap do |d|
    font = d.fonts.add("Helvetica")
    page = d.pages.add
    page.canvas.text("Form", at: [72, 720], font: font, size: 12)
    d.form.add_text_field("name", page: page, rect: [72, 650, 300, 670])
    d.form.add_checkbox("agree", page: page, rect: [72, 600, 87, 615], checked: true)
  end.then { |d| StringIO.new.tap { |io| d.write(io: io) }.string }
end

.with_layersObject



79
80
81
82
83
84
85
86
87
# File 'lib/pdfrb/task/generate_corpus.rb', line 79

def with_layers
  Pdfrb::Document.new.tap do |d|
    font = d.fonts.add("Helvetica")
    d.pages.add.canvas.text("Base", at: [72, 720], font: font, size: 12)
    d.layers.add("Background Art", default_on: false)
    d.layers.add("Annotations")
    d.layers.sync!
  end.then { |d| StringIO.new.tap { |io| d.write(io: io) }.string }
end

.with_outlineObject



55
56
57
58
59
60
61
62
63
64
# File 'lib/pdfrb/task/generate_corpus.rb', line 55

def with_outline
  Pdfrb::Document.new.tap do |d|
    font = d.fonts.add("Helvetica")
    3.times do |i|
      page = d.pages.add
      page.canvas.text("Section #{i + 1}", at: [72, 720], font: font, size: 14)
      d.outline.add("Section #{i + 1}", dest: page)
    end
  end.then { |d| StringIO.new.tap { |io| d.write(io: io) }.string }
end