pdfrb 0.7.43 → 0.7.44
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.
- checksums.yaml +4 -4
- data/docs/USAGE.md +11 -2
- data/lib/pdfrb/content/canvas.rb +13 -3
- data/lib/pdfrb/document.rb +21 -0
- data/lib/pdfrb/task/extract_text.rb +12 -0
- data/lib/pdfrb/version.rb +1 -1
- data/lib/pdfrb.rb +14 -0
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 836c9a2bfe781c6816a25f2db87e99c3b30265fcfb80f116f1b54ccfe9ce149d
|
|
4
|
+
data.tar.gz: 2a7b2a9f1d607b1dd29fd6aca4a2b9a977b315e98834026974ae73d384a62718
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: dc2be0c8bcf1f7a6a77f3db649dba782cc789e532a17afb2fd6152b5fa8109e2c0d6410a01f8df0c820e53fa3439035ff3db24fde8e889a32b6bc880a93cda25
|
|
7
|
+
data.tar.gz: ad3a5ae6eb6dc87577477bba0a7b4ccb6e75e5c914dee698ee4bc661f2df96b363172ef6aded34815b5965ec3f01edf0e6c710b70ba4a4ab95510acc6a0a2b2d
|
data/docs/USAGE.md
CHANGED
|
@@ -137,9 +137,18 @@ File.binwrite("linearized.pdf", io.string)
|
|
|
137
137
|
## Encryption
|
|
138
138
|
|
|
139
139
|
```ruby
|
|
140
|
-
doc = Pdfrb.
|
|
140
|
+
doc = Pdfrb::Document.new
|
|
141
|
+
doc.pages.add
|
|
142
|
+
doc.encrypt!(user_password: "secret", owner_password: "owner", bits: 128)
|
|
141
143
|
doc.write("encrypted.pdf")
|
|
142
|
-
|
|
144
|
+
|
|
145
|
+
# Opening an encrypted document:
|
|
146
|
+
doc = Pdfrb.open("encrypted.pdf",
|
|
147
|
+
config: { "encryption.password" => "secret" })
|
|
148
|
+
|
|
149
|
+
# Removing encryption from a parsed document:
|
|
150
|
+
doc.decrypt!
|
|
151
|
+
doc.write("decrypted.pdf")
|
|
143
152
|
```
|
|
144
153
|
|
|
145
154
|
## Merging PDFs
|
data/lib/pdfrb/content/canvas.rb
CHANGED
|
@@ -64,7 +64,11 @@ module Pdfrb
|
|
|
64
64
|
self
|
|
65
65
|
end
|
|
66
66
|
|
|
67
|
-
def line(x1, y1, x2, y2)
|
|
67
|
+
def line(x1 = nil, y1 = nil, x2 = nil, y2 = nil, from: nil, to: nil)
|
|
68
|
+
if from && to
|
|
69
|
+
x1, y1 = from
|
|
70
|
+
x2, y2 = to
|
|
71
|
+
end
|
|
68
72
|
move_to(x1, y1).line_to(x2, y2)
|
|
69
73
|
end
|
|
70
74
|
|
|
@@ -189,8 +193,14 @@ module Pdfrb
|
|
|
189
193
|
self
|
|
190
194
|
end
|
|
191
195
|
|
|
192
|
-
def rectangle(x, y,
|
|
193
|
-
|
|
196
|
+
def rectangle(x = nil, y = nil, w = nil, h = nil,
|
|
197
|
+
point: nil, width: nil, height: nil)
|
|
198
|
+
x, y = point if point
|
|
199
|
+
w = width if w.nil?
|
|
200
|
+
h = height if h.nil?
|
|
201
|
+
raise ArgumentError, "rectangle needs x, y, width and height" if [x, y, w, h].any?(&:nil?)
|
|
202
|
+
|
|
203
|
+
emit_op(Pdfrb::Content::Operator::Rectangle, x, y, w, h)
|
|
194
204
|
self
|
|
195
205
|
end
|
|
196
206
|
|
data/lib/pdfrb/document.rb
CHANGED
|
@@ -166,6 +166,14 @@ module Pdfrb
|
|
|
166
166
|
|
|
167
167
|
def encryption; @encryption ||= Document::Encryption.new(self); end
|
|
168
168
|
|
|
169
|
+
def encrypt!(**)
|
|
170
|
+
encryption.encrypt!(**)
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
def decrypt!
|
|
174
|
+
encryption.decrypt!
|
|
175
|
+
end
|
|
176
|
+
|
|
169
177
|
def info; @info ||= Document::Info.new(self); end
|
|
170
178
|
|
|
171
179
|
def display; @display ||= Document::Display.new(self); end
|
|
@@ -306,6 +314,19 @@ module Pdfrb
|
|
|
306
314
|
|
|
307
315
|
@xref, @trailer_dict = load_xref_and_trailer(io, sxref)
|
|
308
316
|
@object_reader = Pdfrb::Source::ObjectReader.new(self, @xref) if @xref
|
|
317
|
+
seed_next_oid
|
|
318
|
+
end
|
|
319
|
+
|
|
320
|
+
# New/modified objects must not collide with objects loaded from
|
|
321
|
+
# the xref: continue allocating past the highest known oid (or
|
|
322
|
+
# the trailer /Size, whichever is larger).
|
|
323
|
+
def seed_next_oid
|
|
324
|
+
return if @xref.nil?
|
|
325
|
+
|
|
326
|
+
max_oid = @xref.entries.keys.max || 0
|
|
327
|
+
size = @trailer_dict && @trailer_dict[:Size]
|
|
328
|
+
size = size.to_i if size.respond_to?(:to_i)
|
|
329
|
+
@next_oid = [max_oid + 1, size.to_i, @next_oid].max
|
|
309
330
|
end
|
|
310
331
|
|
|
311
332
|
def load_xref_and_trailer(io, sxref)
|
|
@@ -31,6 +31,18 @@ module Pdfrb
|
|
|
31
31
|
block_given? ? document : results
|
|
32
32
|
end
|
|
33
33
|
|
|
34
|
+
# Extract the text of a single page (see #call for the
|
|
35
|
+
# resolution strategy).
|
|
36
|
+
def call_single_page(page)
|
|
37
|
+
extractor = TextCollector.new(page.document, page)
|
|
38
|
+
begin
|
|
39
|
+
extractor.process(page.decoded_content)
|
|
40
|
+
rescue StandardError
|
|
41
|
+
# Malformed content stream — best effort, skip.
|
|
42
|
+
end
|
|
43
|
+
extractor.text
|
|
44
|
+
end
|
|
45
|
+
|
|
34
46
|
# Internal: collects bytes from +Tj+ / +TJ+ / +'+ / +"'.
|
|
35
47
|
# Tracks text-matrix y to insert line breaks at vertical moves.
|
|
36
48
|
# Resolves glyph codes to Unicode via the active font's
|
data/lib/pdfrb/version.rb
CHANGED
data/lib/pdfrb.rb
CHANGED
|
@@ -58,6 +58,20 @@ module Pdfrb
|
|
|
58
58
|
autoload :CLI, "pdfrb/cli"
|
|
59
59
|
|
|
60
60
|
require "logger"
|
|
61
|
+
require "stringio"
|
|
62
|
+
|
|
63
|
+
class << self
|
|
64
|
+
# Open a PDF file (path or block over the file handle).
|
|
65
|
+
def open(path, **, &)
|
|
66
|
+
Document.open(path, **, &)
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
# Parse PDF bytes/IO into a Document.
|
|
70
|
+
def parse(io, **)
|
|
71
|
+
io = StringIO.new(io.b) if io.is_a?(String)
|
|
72
|
+
Document.new(io: io, **)
|
|
73
|
+
end
|
|
74
|
+
end
|
|
61
75
|
|
|
62
76
|
def self.logger
|
|
63
77
|
@logger ||= Logger.new($stderr, level: Logger::WARN)
|