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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # PDF text extraction reliability review
2
+
3
+ Workpackage: `WP15 — PDF text extraction reliability review only`
4
+ Repository: `solidprivacy-nl/scrub`
5
+ Status: completed review/specification only
6
+
7
+ This document evaluates whether Scrub should support PDF input in the reinsert workflow.
8
+
9
+ No code, UI, tests, dependencies, export behavior, Scrub Key behavior, AI calls, OCR or cloud processing are changed by this review.
10
+
11
+ ---
12
+
13
+ ## Executive recommendation
14
+
15
+ Scrub should **not** implement full PDF reinsert now.
16
+
17
+ Scrub may later consider **text-based PDF input to restored TXT output only**, but only as a helper-first, test-first feature with strong warnings and audit output.
18
+
19
+ Recommended product position for the next phase:
20
+
21
+ ```text
22
+ B/C — text-extraction-only PDF input, producing restored TXT preview/download.
23
+ ```
24
+
25
+ More explicitly:
26
+
27
+ ```text
28
+ PDF input -> local text extraction -> deterministic Scrub Key reinsert -> restored TXT output + audit.
29
+ ```
30
+
31
+ Not recommended now:
32
+
33
+ ```text
34
+ PDF input -> restored PDF output
35
+ PDF input -> restored DOCX output
36
+ OCR for scanned PDFs
37
+ Automatic layout reconstruction
38
+ ```
39
+
40
+ DOCX remains the preferred document-level reinsert path.
41
+
42
+ ---
43
+
44
+ ## 1. PDF types and reliability
45
+
46
+ ### Text-based PDFs
47
+
48
+ Text-based PDFs contain selectable text. These are the only PDFs that are realistic candidates for a future limited extraction path.
49
+
50
+ Even then, reliability is not guaranteed. The visual order on the page can differ from the extracted text order. Placeholders may be split, duplicated, hidden, hyphenated or encoded differently from their visible form.
51
+
52
+ ### Scanned or image-only PDFs
53
+
54
+ Scanned PDFs are images. They do not contain usable text unless an OCR layer exists or OCR is performed.
55
+
56
+ Scrub should treat scanned/image-only PDFs as unsupported for reinsert for now. Adding OCR would create a separate accuracy, dependency, privacy and user-expectation problem.
57
+
58
+ ### OCR-dependent PDFs
59
+
60
+ Some PDFs contain a text layer produced by OCR. The user sees an image, while extraction reads OCR text behind it.
61
+
62
+ This is risky because OCR may introduce wrong characters, missing brackets, confused numbers, broken line order or incomplete text. A placeholder such as `[PERSOON_1]` may become `[PERS0ON_1]`, `[PERSOON l]`, or be split across text fragments.
63
+
64
+ ### PDFs generated from Word
65
+
66
+ Word-generated PDFs may preserve a usable text layer, but layout elements such as headers, footers, page numbers, footnotes, tables and columns may extract in an unexpected order.
67
+
68
+ A Word-generated PDF is often less reliable for reinsert than the original DOCX. For reliable document-level reinsert, Scrub should prefer DOCX whenever possible.
69
+
70
+ ### PDFs generated by AI tools
71
+
72
+ AI tools may generate PDFs through unknown conversion pipelines. Their PDFs may contain odd text layers, duplicated text, hidden content, visual text that differs from extracted text, or paragraph/order changes.
73
+
74
+ This makes AI-generated PDF output a weak foundation for high-trust legal restoration.
75
+
76
+ ### PDFs with columns, tables, footnotes, headers, footers or page numbers
77
+
78
+ These layouts are high risk for extraction. Text may be returned by coordinate order, stream order or internal object order, not legal reading order.
79
+
80
+ Risks include:
81
+
82
+ - two columns interleaving line by line;
83
+ - table rows becoming a long unreadable sentence;
84
+ - footnotes appearing in the middle of body text;
85
+ - headers and footers appearing repeatedly;
86
+ - page numbers entering the restored text;
87
+ - captions and exhibit labels moving away from their context.
88
+
89
+ ### PDFs with hidden or duplicated text layers
90
+
91
+ Some PDFs contain duplicated text for accessibility, search, OCR or rendering reasons. Extraction may return both visible and hidden text.
92
+
93
+ This can produce duplicate placeholders and duplicate restored values, which may mislead users.
94
+
95
+ ### PDFs where reading order differs from visual order
96
+
97
+ PDF is a presentation format, not a reliable semantic document format. The internal text order can differ from what a human reads on screen.
98
+
99
+ That matters for legal documents because paragraph order, headings, footnotes, tables and references often carry legal meaning.
100
+
101
+ ---
102
+
103
+ ## 2. Extraction risks
104
+
105
+ ### Incomplete text extraction
106
+
107
+ Some text may not be extracted at all. This can happen with scanned pages, images, vector text, unsupported fonts, form fields, annotations, headers, footers or unusual PDF structures.
108
+
109
+ If a placeholder is not extracted, Scrub cannot restore it.
110
+
111
+ ### Broken reading order
112
+
113
+ The extracted text may be in the wrong order. This is especially likely with multi-column layouts, tables, footnotes and positioned text blocks.
114
+
115
+ Broken order can make the restored TXT output legally confusing or misleading.
116
+
117
+ ### Missing headers and footers
118
+
119
+ PDF extraction tools may include, exclude or reorder headers and footers. This creates uncertainty around document titles, page identifiers, case references and repeated legal context.
120
+
121
+ ### Duplicated text
122
+
123
+ Text may be duplicated because of hidden layers, OCR layers, accessibility layers or repeated rendering objects.
124
+
125
+ If a placeholder appears twice in extracted text but once visually, the audit may show extra replacements and the restored output may appear over-complete.
126
+
127
+ ### Split placeholders
128
+
129
+ A placeholder can be split across PDF text objects, for example:
130
+
131
+ ```text
132
+ [PERS
133
+ OON_1]
134
+ ```
135
+
136
+ or:
137
+
138
+ ```text
139
+ [PERSOON_
140
+ 1]
141
+ ```
142
+
143
+ The current deterministic placeholder replacement expects intact placeholder tokens. Split placeholders may not be restored.
144
+
145
+ ### Changed placeholder characters
146
+
147
+ PDF extraction may alter brackets, underscores, hyphens, digits or letters. Examples include smart punctuation, substituted glyphs, OCR confusion and encoding changes.
148
+
149
+ If `[ZAAKNUMMER_1]` is extracted as `[ZAAKNUMMER-l]` or `[ZAAKNUMMER 1]`, Scrub should report an unknown or missing placeholder rather than guessing.
150
+
151
+ ### Ligatures or encoding issues
152
+
153
+ PDFs can contain ligatures or custom font encodings. Extracted text may differ from visible text. That may damage names, legal terms or placeholders.
154
+
155
+ ### Hyphenation and line wrapping
156
+
157
+ PDF text extraction may preserve hard line breaks and hyphenation. A word split at line end may be extracted as two parts. This can affect readability and placeholder detection.
158
+
159
+ ### Tables becoming unreadable
160
+
161
+ Tables in PDFs often lose cell boundaries during extraction. A table row can become a sequence of unrelated values. A restored TXT output should never be presented as a faithful table reconstruction.
162
+
163
+ ### Loss of legal formatting and context
164
+
165
+ PDF extraction loses or weakens legal formatting such as headings, numbering, indentation, footnotes, quote blocks, signatures, exhibits and page layout.
166
+
167
+ This can change how a legal reader interprets the result.
168
+
169
+ ### False completeness
170
+
171
+ The biggest user risk is that a user assumes the restored output is complete because Scrub produced a file.
172
+
173
+ For PDF text extraction, Scrub must communicate that output is a convenience preview/download, not a guaranteed complete restored legal document.
174
+
175
+ ---
176
+
177
+ ## 3. Privacy and legal risk
178
+
179
+ Restored output may contain original confidential or personal values again. That means the output must be treated as sensitive.
180
+
181
+ PDF extraction can miss placeholders. It can also produce unknown placeholder-like tokens or duplicates. The audit must clearly show:
182
+
183
+ - placeholders from the key that were not found;
184
+ - unknown placeholders found in extracted text;
185
+ - duplicate placeholders in the key;
186
+ - validation issues;
187
+ - whether extraction was text-based only;
188
+ - whether OCR was used, which should be `False` for the recommended next phase.
189
+
190
+ The user must manually check the full restored result before sharing, filing, sending or using it in legal work.
191
+
192
+ Privacy boundaries:
193
+
194
+ - no cloud OCR;
195
+ - no external PDF conversion service;
196
+ - no AI calls;
197
+ - no external document processing;
198
+ - no real personal data in tests;
199
+ - deterministic local processing only;
200
+ - Scrub Key remains local and protected.
201
+
202
+ ---
203
+
204
+ ## 4. Product strategy
205
+
206
+ ### Option A — Unsupported for reinsert
207
+
208
+ This is the safest product option.
209
+
210
+ Pros:
211
+
212
+ - avoids misleading users;
213
+ - avoids unreliable extraction;
214
+ - reinforces DOCX/TXT as trusted paths.
215
+
216
+ Cons:
217
+
218
+ - users with PDF-only AI output must manually copy text or convert outside Scrub;
219
+ - creates workflow friction.
220
+
221
+ Assessment:
222
+
223
+ ```text
224
+ Safe baseline, acceptable until a helper-only extraction path is proven.
225
+ ```
226
+
227
+ ### Option B — Text-extraction-only, output as TXT
228
+
229
+ This means Scrub accepts a text-based PDF, extracts text locally, reinserts values, and returns restored TXT.
230
+
231
+ Pros:
232
+
233
+ - realistic limited scope;
234
+ - avoids layout reconstruction claims;
235
+ - can reuse existing deterministic text reinsert logic;
236
+ - audit can show limitations clearly.
237
+
238
+ Cons:
239
+
240
+ - extraction can still be incomplete or reordered;
241
+ - users may still over-trust the output if warnings are weak.
242
+
243
+ Assessment:
244
+
245
+ ```text
246
+ Best candidate for a future helper-only spike.
247
+ ```
248
+
249
+ ### Option C — PDF input -> restored TXT output
250
+
251
+ This is the practical product form of option B.
252
+
253
+ Recommended future behavior:
254
+
255
+ ```text
256
+ Upload text-based PDF
257
+ Extract text locally
258
+ Run Scrub Key reinsert
259
+ Show restored TXT preview
260
+ Allow restored TXT download
261
+ Show audit and limitations
262
+ ```
263
+
264
+ Assessment:
265
+
266
+ ```text
267
+ Recommended next implementation direction, if PDF support is pursued.
268
+ ```
269
+
270
+ ### Option D — PDF input -> restored DOCX output
271
+
272
+ Not recommended now.
273
+
274
+ PDF-to-DOCX reconstruction is not a simple reinsert operation. It implies layout interpretation, table reconstruction, paragraph reconstruction and potential semantic changes.
275
+
276
+ Assessment:
277
+
278
+ ```text
279
+ Out of scope.
280
+ ```
281
+
282
+ ### Option E — PDF input -> restored PDF output
283
+
284
+ Not recommended now.
285
+
286
+ A restored PDF would imply visual fidelity. Achieving this safely would require layout reconstruction, font handling, coordinate mapping, metadata handling and possibly OCR. This is far beyond the safe reinsert helper model.
287
+
288
+ Assessment:
289
+
290
+ ```text
291
+ Out of scope.
292
+ ```
293
+
294
+ ### Product recommendation
295
+
296
+ Recommended status:
297
+
298
+ ```text
299
+ Do not support full PDF reinsert now.
300
+ Keep PDF unsupported for document-level reinsert until text extraction is proven.
301
+ Consider only text-based PDF input -> restored TXT output as a future helper-only package.
302
+ Keep DOCX as the preferred document-level reinsert workflow.
303
+ ```
304
+
305
+ ---
306
+
307
+ ## 5. Technical architecture
308
+
309
+ ### Existing dependencies
310
+
311
+ The inspected `requirements.txt` does not include a dedicated PDF extraction dependency such as `pypdf`, `pdfplumber`, `PyMuPDF`, `pdfminer.six`, `pytesseract` or OCR tooling.
312
+
313
+ Therefore a future PDF extraction helper would likely require either:
314
+
315
+ - a new dependency decision; or
316
+ - use of an already transitive dependency, which should be avoided unless explicit and tested.
317
+
318
+ This review does not add dependencies.
319
+
320
+ ### Existing helper architecture
321
+
322
+ The current deterministic reinsert core is text-based:
323
+
324
+ ```python
325
+ reinsert_from_scrub_key(text, scrub_key)
326
+ ```
327
+
328
+ The document wrapper foundation already exists for TXT and DOCX:
329
+
330
+ ```python
331
+ reinsert_text_document(text, scrub_key)
332
+ reinsert_txt_bytes(content, scrub_key, encoding="utf-8")
333
+ reinsert_docx_bytes(content, scrub_key)
334
+ ```
335
+
336
+ A future PDF helper should not create a new replacement engine. It should extract text and then feed the extracted text into the existing deterministic text reinsert path.
337
+
338
+ ### Possible future helper shape
339
+
340
+ A future helper-only workpackage could define something like:
341
+
342
+ ```python
343
+ extract_text_from_pdf_bytes(content) -> dict
344
+ reinsert_pdf_text_bytes(content, scrub_key) -> dict
345
+ ```
346
+
347
+ The result should include:
348
+
349
+ - extracted text;
350
+ - restored text;
351
+ - restored TXT bytes;
352
+ - extraction warnings;
353
+ - unsupported PDF reason when applicable;
354
+ - placeholder audit fields from the existing reinsert helper;
355
+ - local-only/no-AI/no-cloud fields;
356
+ - `ocr_processing=False`.
357
+
358
+ ### Determinism and locality
359
+
360
+ A text-based PDF extraction helper can be deterministic and local if it uses a local parser and no OCR/cloud calls.
361
+
362
+ However, deterministic does not mean reliable. The same parser may consistently extract the wrong order. The audit and UI must describe this clearly.
363
+
364
+ ### Why PDF output reconstruction is much harder
365
+
366
+ PDF output reconstruction is hard because PDF is layout-oriented. Text may be stored in small positioned fragments, not semantic paragraphs.
367
+
368
+ Reconstructing a restored PDF safely would require:
369
+
370
+ - preserving or recreating coordinates;
371
+ - font and glyph handling;
372
+ - line wrapping decisions;
373
+ - table and column reconstruction;
374
+ - annotation and form handling;
375
+ - metadata decisions;
376
+ - page-break behavior;
377
+ - checks for hidden text layers;
378
+ - possibly OCR.
379
+
380
+ That is outside the current safe helper model.
381
+
382
+ ### Why scanned PDFs remain out of scope
383
+
384
+ Scanned PDFs require OCR. OCR introduces:
385
+
386
+ - recognition errors;
387
+ - missed placeholders;
388
+ - altered characters;
389
+ - additional dependencies;
390
+ - more compute cost;
391
+ - new privacy questions if not strictly local;
392
+ - stronger user warnings.
393
+
394
+ OCR should remain out of scope unless separately approved in a dedicated workpackage.
395
+
396
+ ---
397
+
398
+ ## 6. UX recommendation
399
+
400
+ If PDF text extraction is later added, the UI should communicate limitations before the user clicks restore.
401
+
402
+ Required UX language should cover:
403
+
404
+ ```text
405
+ PDF-tekstextractie is niet gegarandeerd volledig.
406
+ Opmaak wordt niet behouden.
407
+ De uitvoer is herstelde TXT, geen herstelde PDF.
408
+ Controleer het volledige resultaat handmatig.
409
+ ```
410
+
411
+ The UI should also show:
412
+
413
+ - not-found placeholders;
414
+ - unknown placeholders;
415
+ - duplicate placeholders;
416
+ - extraction warnings;
417
+ - whether OCR was used, expected to be `nee`;
418
+ - local-only/no-AI/no-cloud;
419
+ - warning that restored output contains confidential or personal values again.
420
+
421
+ Recommended UI position if implemented later:
422
+
423
+ ```text
424
+ Originele waarden terugzetten
425
+ -> PDF tekst-extractie, experimenteel / beperkt
426
+ -> Herstelde TXT-preview
427
+ -> Download herstelde TXT (.txt)
428
+ -> Controleverslag PDF-tekstextractie
429
+ ```
430
+
431
+ The UI should not offer `Download herstelde PDF`.
432
+
433
+ The UI should not imply that formatting, page layout, tables, headers, footers or legal document fidelity are preserved.
434
+
435
+ ---
436
+
437
+ ## 7. Test strategy for future work
438
+
439
+ No tests are implemented by this review. Future tests should be added before any PDF extraction helper is introduced.
440
+
441
+ Recommended future synthetic tests:
442
+
443
+ 1. Text-based PDF with intact placeholders.
444
+ 2. Text-based PDF with two columns.
445
+ 3. Text-based PDF with table-like layout.
446
+ 4. Text-based PDF with header/footer placeholders.
447
+ 5. Text-based PDF with split placeholder.
448
+ 6. Text-based PDF with duplicated hidden text layer if feasible.
449
+ 7. Text-based PDF with unknown placeholder.
450
+ 8. Text-based PDF where key mappings are not found.
451
+ 9. Scanned/image-only PDF should be rejected or clearly marked unsupported.
452
+ 10. OCR-dependent PDF should not trigger OCR automatically.
453
+ 11. Audit includes local-only/no-AI/no-cloud fields.
454
+ 12. Audit includes OCR not used.
455
+ 13. Restored output is TXT, not PDF.
456
+ 14. Existing TXT/DOCX reinsert behavior remains unchanged.
457
+ 15. Existing scrubbed export/download behavior remains unchanged.
458
+
459
+ Future test fixtures must use synthetic data only.
460
+
461
+ ---
462
+
463
+ ## Required recommendation answers
464
+
465
+ ### 1. Should Scrub implement full PDF reinsert now?
466
+
467
+ No.
468
+
469
+ Full PDF reinsert should not be implemented now because it would imply layout and completeness guarantees that Scrub cannot safely provide at this stage.
470
+
471
+ ### 2. Should Scrub implement PDF text extraction only?
472
+
473
+ Not immediately in this review package.
474
+
475
+ It may be considered as a future helper-only workpackage for text-based PDFs, producing restored TXT output with strong warnings and audit.
476
+
477
+ ### 3. Should restored PDF output remain out of scope?
478
+
479
+ Yes.
480
+
481
+ Restored PDF output should remain out of scope.
482
+
483
+ ### 4. Should OCR remain out of scope?
484
+
485
+ Yes.
486
+
487
+ OCR should remain out of scope unless separately approved after a dedicated reliability, privacy and dependency review.
488
+
489
+ ### 5. What is the safest next implementation workpackage, if any?
490
+
491
+ Recommended next implementation workpackage:
492
+
493
+ ```text
494
+ WP16 — Text-based PDF extraction helper spike, restored TXT output only
495
+ ```
496
+
497
+ Suggested scope for WP16:
498
+
499
+ - choose and justify a local PDF text extraction dependency;
500
+ - add a pure helper only;
501
+ - support text-based PDFs only;
502
+ - reject or clearly mark scanned/image-only PDFs as unsupported;
503
+ - feed extracted text into existing deterministic reinsert logic;
504
+ - output restored TXT only;
505
+ - add synthetic tests;
506
+ - do not add UI yet;
507
+ - do not add OCR;
508
+ - do not output PDF.
509
+
510
+ ### 6. What should explicitly not be built yet?
511
+
512
+ Do not build yet:
513
+
514
+ - full restored PDF output;
515
+ - PDF-to-DOCX reconstruction;
516
+ - OCR;
517
+ - cloud PDF conversion;
518
+ - AI-based extraction;
519
+ - UI upload controls for PDF reinsert;
520
+ - automatic PDF rehydration;
521
+ - promises of layout preservation;
522
+ - batch PDF processing;
523
+ - real-data PDF test cases.
524
+
525
+ ---
526
+
527
+ ## Final decision
528
+
529
+ PDF should stay out of the main reinsert workflow until a limited text-extraction helper proves safe enough.
530
+
531
+ The next safe step is not UI. It is a pure helper spike for text-based PDF extraction to restored TXT output, with strict tests, local-only processing and clear audit.
532
+
533
+ DOCX remains the preferred document-level reinsert path.