Commit d6bcf58
authored
Load watermark font once per document (#7820)
## Description of Changes
`addTextWatermark` is called once per page, and every call ran
`PDType0Font.load(document, tempFile)`. That call embeds a **new font
object into the document each time**, so a watermark job held one full
copy of the TTF per page for the life of the request.
For a 1000-page document with the default Latin face
(`NotoSans-Regular.ttf`, 582 KB) that is ~570 MB of raw font bytes
before PDFBox's parsed glyph tables and subsetting structures — which is
what pushes a large watermark past the heap and OOM-kills containers
under 8 GB. The CJK faces make it much worse: `NotoSansKR-Regular.ttf`
is 10.4 MB, so the same document needs an order of magnitude more again.
Each call also wrote the font to a temp file and deleted it, so a
1000-page job did 1000 create/write/delete cycles.
### What changed
- `loadWatermarkFont` extracted, called **once** before the page loop;
the loaded `PDFont` is passed into `addTextWatermark`.
- Loads via `PDType0Font.load(PDDocument, InputStream)` straight from
the classpath resource, so the temp file is gone entirely.
- `addTextWatermark` takes the `PDFont` instead of the `alphabet`
string, and no longer needs the `PDDocument`.
- Dropped the dead `PDType1Font` initialiser that was overwritten before
use, plus the imports that went with it.
The page loop itself was already correct — it streams one page at a
time. The allocation was purely inside it.
## Verification
Added `testAddTextWatermark_EmbedsFontOnce`, which watermarks a 12-page
document and counts the distinct embedded `FontFile2` streams reachable
from page resources.
Confirmed it catches the regression — reintroducing the per-page load
fails it:
```
expected: <1> but was: <12>
```
All 23 existing watermark tests pass unchanged.
## Checklist
- [x] I have read the [Contribution
Guidelines](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/CONTRIBUTING.md)
- [x] My changes generate no new warnings
- [x] I have added tests that prove my fix is effective
- [x] New and existing unit tests pass locally with my changes1 parent 6ebbbc2 commit d6bcf58
2 files changed
Lines changed: 95 additions & 32 deletions
File tree
- app/core/src
- main/java/stirling/software/SPDF/controller/api/security
- test/java/stirling/software/SPDF/controller/api/security
Lines changed: 27 additions & 32 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
6 | | - | |
7 | | - | |
8 | 6 | | |
9 | 7 | | |
10 | | - | |
11 | 8 | | |
12 | 9 | | |
13 | 10 | | |
14 | | - | |
15 | 11 | | |
16 | 12 | | |
17 | 13 | | |
18 | 14 | | |
19 | 15 | | |
20 | | - | |
21 | | - | |
22 | 16 | | |
23 | 17 | | |
24 | 18 | | |
| |||
113 | 107 | | |
114 | 108 | | |
115 | 109 | | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
116 | 115 | | |
117 | 116 | | |
118 | 117 | | |
| |||
133 | 132 | | |
134 | 133 | | |
135 | 134 | | |
136 | | - | |
137 | 135 | | |
138 | 136 | | |
139 | 137 | | |
140 | 138 | | |
141 | 139 | | |
142 | | - | |
| 140 | + | |
143 | 141 | | |
144 | 142 | | |
145 | 143 | | |
| |||
175 | 173 | | |
176 | 174 | | |
177 | 175 | | |
178 | | - | |
179 | | - | |
180 | | - | |
181 | | - | |
182 | | - | |
183 | | - | |
184 | | - | |
185 | | - | |
186 | | - | |
187 | | - | |
188 | | - | |
189 | | - | |
190 | | - | |
191 | | - | |
192 | | - | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
193 | 183 | | |
194 | 184 | | |
195 | 185 | | |
| |||
199 | 189 | | |
200 | 190 | | |
201 | 191 | | |
202 | | - | |
203 | | - | |
204 | | - | |
205 | | - | |
206 | | - | |
207 | | - | |
208 | | - | |
209 | | - | |
210 | | - | |
| 192 | + | |
| 193 | + | |
211 | 194 | | |
| 195 | + | |
212 | 196 | | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
213 | 208 | | |
214 | 209 | | |
215 | 210 | | |
| |||
app/core/src/test/java/stirling/software/SPDF/controller/api/security/WatermarkControllerTest.java
Lines changed: 68 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
13 | 16 | | |
14 | 17 | | |
| 18 | + | |
| 19 | + | |
15 | 20 | | |
16 | 21 | | |
17 | 22 | | |
| 23 | + | |
18 | 24 | | |
| 25 | + | |
19 | 26 | | |
20 | 27 | | |
21 | 28 | | |
| |||
387 | 394 | | |
388 | 395 | | |
389 | 396 | | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
| 436 | + | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
390 | 458 | | |
391 | 459 | | |
392 | 460 | | |
| |||
0 commit comments