Document Measurement¶
structx.measurement estimates how much content a PDF holds. It exists for
integrations that need page and text-length estimates before extraction,
such as quoting usage or sizing a queue.
It is separate from extraction on purpose:
- Extraction never calls it, so normal runs pay no measurement cost.
- Measuring never rewrites, converts, or replaces the input file.
- OCR here is local image-to-text recognition, never a model request.
Install the optional dependencies:
pip install "structx[measurement]" # embedded text only
pip install "structx[measurement-ocr]" # adds local OCR for scanned pages
Usage¶
from structx.measurement import DocumentMeasurer
measurer = DocumentMeasurer(ocr_mode="auto")
measurement = measurer.measure("scripts/example_input/S0305SampleInvoice.pdf")
print(measurement.page_count) # 1
print(measurement.character_count) # 1321
print(measurement.status) # complete
for page in measurement.pages:
print(page.page_number, page.method, page.character_count, page.status)
One measurer can measure many documents and reuses its OCR engines across them, so create it once and keep it. Measurement is synchronous. Reading and rendering PDF pages is serialized internally because the PDF backend is not thread-safe, but OCR runs outside that lock, so a slow scanned document does not block other documents measured at the same time.
Parallel OCR¶
OCR costs seconds per page, so a multi-page scan is slow when measured one page
at a time. ocr_workers recognizes several pages at once:
measurer = DocumentMeasurer(ocr_mode="auto", ocr_workers=4)
try:
measurement = measurer.measure("scanned.pdf")
finally:
measurer.close() # release worker threads; measuring again recreates them
Pages are still reported in source order, and only one batch of pages is rendered at a time, so a long document does not hold every page image in memory.
The default is 1. Raise it deliberately:
- Each worker gets its own OCR engine, because a single
RapidOCRinstance updates its own state per call and cannot be shared across threads. - ONNX Runtime already spreads one recognition call across CPU cores, so extra
workers only help when each engine is limited to one inference thread. The
built-in reader applies that automatically for
ocr_workers > 1; passocr_engine_paramsto override it. - More workers is not better. On a 12-core machine measuring 8 scanned pages, 1 worker took 20.8s, 4 workers took 14.2s, and 6 workers took 16.6s.
- Each engine loads its own models, so memory grows with worker count.
A custom ocr_reader is never fanned out with per-thread engines; if you supply
one and raise ocr_workers, it must be safe to call from several threads.
OCR Modes¶
| Mode | Behavior |
|---|---|
never (default) |
Counts the embedded text layer only. Pages without one are reported as partial with ocr_skipped. |
auto |
Adds OCR for pages whose embedded text is missing, empty, or unreadable. |
always |
Reads every page with OCR instead of its embedded text, so mixed pages are not counted twice. |
Supply ocr_reader to use your own recognizer, for example to pin a language or
model. It receives rendered page pixels as an RGB array and returns text. When
it is omitted and OCR is needed, the built-in RapidOCR reader is created on
first use.
Render resolution is not a useful cost lever: the OCR engine resizes pages internally, so 72 DPI and 150 DPI cost about the same per page.
Completeness¶
Counts are estimates, and the result says how they were produced rather than implying certainty:
- A page is
completewhen its text layer was read, or when OCR ran successfully — including a successful OCR pass that found no text. - A page is
partialwhen measurement could not observe its content. Theerror_codeexplains why:ocr_skipped,ocr_unavailable,ocr_failed,native_text_failed,page_render_failed, orpage_load_failed. - A failing page never removes pages from the document. A document keeps its
real page count and reports
partial, so callers can decide whether to fall back to page-based handling. estimator_revisionidentifies the counting algorithm. It does not promise that OCR models or their versions stay fixed.
Invalid, missing, empty, encrypted, non-PDF, and oversized inputs raise
FileError. Invalid settings raise ConfigurationError.
API¶
DocumentMeasurer¶
Estimate page and character counts for PDF documents.
The measurer is reusable: one instance can measure many documents and reuses a single OCR reader when OCR is enabled. Measurement is synchronous and serialized internally.
Attributes:
| Name | Type | Description |
|---|---|---|
ocr_mode |
|
|
max_pages |
Largest document accepted, in pages. |
|
max_input_bytes |
Largest input accepted, in bytes. |
|
render_dpi |
Resolution used when rendering a page for OCR. |
|
max_render_pixels |
Upper bound on rendered pixels per page. Larger pages are rendered at a reduced scale rather than skipped. |
|
ocr_workers |
Pages recognized at once. The default of 1 keeps OCR serial. Higher values give each worker thread its own engine and bound how many page images are held in memory at once. |
|
ocr_engine_params |
Parameters for the built-in OCR engine. Ignored when
|
Source code in structx/measurement.py
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 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 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 | |
close()
¶
Release worker threads held for parallel OCR.
Measuring again after closing recreates them.
Source code in structx/measurement.py
measure(file_path)
¶
Measure one PDF document.
PDF reading and rendering are serialized because the PDF backend is not thread-safe. OCR runs outside that lock, so a slow scanned document does not block other documents measured at the same time.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
Union[str, Path]
|
Path to an existing PDF file. |
required |
Returns:
| Type | Description |
|---|---|
DocumentMeasurement
|
Page and character estimates with per-page completeness. |
Raises:
| Type | Description |
|---|---|
FileError
|
If the file is missing, empty, not a readable PDF, or exceeds the configured size or page limits. |
ConfigurationError
|
If the optional PDF backend is unavailable. |
Source code in structx/measurement.py
DocumentMeasurement¶
Measured content of one document.
Attributes:
| Name | Type | Description |
|---|---|---|
pages |
Tuple[PageMeasurement, ...]
|
Page measurements in source order. |
ocr_mode |
str
|
OCR mode used for this measurement. |
estimator_revision |
str
|
Revision of the counting algorithm. |
Source code in structx/measurement.py
character_count
property
¶
Total characters observed across measured pages.
error_codes
property
¶
Distinct partial-page reasons, in first-seen order.
page_count
property
¶
Number of pages in the measured document.
status
property
¶
complete only when every page of a non-empty document was measured.
PageMeasurement¶
Measured content of one page.
Attributes:
| Name | Type | Description |
|---|---|---|
page_number |
int
|
One-based page position in the source document. |
character_count |
int
|
Unicode code points observed on the page. |
method |
str
|
How the count was produced: |
status |
str
|
|
error_code |
Optional[str]
|
Stable reason for a partial page, otherwise |