Reference
playa
PLAYA ain't a LAYout Analyzer... but it can help you get stuff out of PDFs.
Basic usage:
with playa.open(path) as pdf:
for page in pdf.pages:
print(f"page {page.label}:")
for obj in page:
print(f" {obj.object_type} at {obj.bbox}")
if obj.object_type == "text":
print(f" chars: {obj.chars}")
open(path, *, password='', space='screen', max_workers=1, mp_context=None)
Open a PDF document from a path on the filesystem.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
Union[PathLike, str]
|
Path to the document to open. |
required |
space
|
DeviceSpace
|
Device space to use ("screen" for screen-like coordinates, "page" for pdfminer.six-like coordinates, "default" for default user space with no rotation or translation) |
'screen'
|
max_workers
|
Union[int, None]
|
Number of worker processes to use for parallel processing of pages (if 1, no workers are spawned) |
1
|
mp_context
|
Union[BaseContext, None]
|
Multiprocessing context to use for worker processes, see Contexts and Start Methods for more information. |
None
|
Source code in playa/__init__.py
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
|
parse(buffer, *, password='', space='screen', max_workers=1, mp_context=None)
Read a PDF document from binary data.
Potential slowness
When using multiple processes, this results in the entire
buffer being copied to the worker processes for the moment,
which may cause some overhead. It is preferable to use open
on a filesystem path if possible, since that uses
memory-mapped I/O.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
buffer
|
bytes
|
Buffer containing PDF data. |
required |
space
|
DeviceSpace
|
Device space to use ("screen" for screen-like coordinates, "page" for pdfminer.six-like coordinates, "default" for default user space with no rotation or translation) |
'screen'
|
max_workers
|
Union[int, None]
|
Number of worker processes to use for parallel processing of pages (if 1, no workers are spawned) |
1
|
mp_context
|
Union[BaseContext, None]
|
Multiprocessing context to use for worker processes, see Contexts and Start Methods for more information. |
None
|
Source code in playa/__init__.py
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
|
playa.document
Basic classes for PDF document parsing.
Destinations
Mapping of named destinations.
These either come as a NameTree or a dict, depending on the version of the PDF standard, so this abstracts that away.
Source code in playa/document.py
969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 |
|
doc
property
Get associated document if it exists.
__getitem__(name)
Get a named destination.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
Union[bytes, str, PSLiteral]
|
The name of the destination. |
required |
Raises:
Type | Description |
---|---|
KeyError
|
If no such destination exists. |
TypeError
|
If the PDF is damaged and the destinations tree contains something unexpected or missing. |
Source code in playa/document.py
1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 |
|
__iter__()
Iterate over names of destinations.
Beware of corrupted PDFs
This simply iterates over the names listed in the PDF, and does not attempt to actually parse the destinations (because that's pretty slow). If the PDF is broken, you may encounter exceptions when actually trying to access them by name.
Source code in playa/document.py
1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 |
|
items()
Iterate over named destinations.
Source code in playa/document.py
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 |
|
Document
Representation of a PDF document.
Since PDF documents can be very large and complex, merely creating
a Document
does very little aside from verifying that the
password is correct and getting a minimal amount of metadata. In
general, PLAYA will try to open just about anything as a PDF, so
you should not expect the constructor to fail here if you give it
nonsense (something else may fail later on).
Some metadata, such as the structure tree and page tree, will be loaded lazily and cached. We do not handle modification of PDFs.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
fp
|
Union[BinaryIO, bytes]
|
File-like object in binary mode, or a buffer with binary data.
Files will be read using |
required |
password
|
str
|
Password for decryption, if needed. |
''
|
space
|
DeviceSpace
|
the device space to use for interpreting content ("screen" or "page") |
'screen'
|
Raises:
Type | Description |
---|---|
TypeError
|
if |
PDFEncryptionError
|
if the PDF has an unsupported encryption scheme |
PDFPasswordIncorrect
|
if the password is incorrect |
Source code in playa/document.py
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 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 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 |
|
destinations
property
Named destinations as an iterable/addressable Destinations
object.
fonts
property
Get the mapping of font names to fonts for this document.
Note that this can be quite slow the first time it's accessed as it must scan every single page in the document.
Font names may collide.
Font names are generally understood to be globally unique
in the neighbourhood in the document, but there's no
guarantee that this is the case. In keeping with the
"incremental update" philosophy dear to PDF, you get the
last font with a given name.
Do not rely on this being a dict
.
Currently this is implemented eagerly, but in the future it may return a lazy object which only loads fonts on demand.
names
property
PDF name dictionary (PDF 1.7 sec 7.7.4).
Raises:
Type | Description |
---|---|
KeyError
|
if nonexistent. |
objects
property
Iterate over all indirect objects (including, then expanding object streams)
outline
property
Document outline, if any.
page_labels
property
Generate page label strings for the PDF document.
If the document includes page labels, generates strings, one per page. If not, raise KeyError.
The resulting iterator is unbounded (because the page label
tree does not actually include all the pages), so it is
recommended to use pages
instead.
Raises:
Type | Description |
---|---|
KeyError
|
No page labels are present in the catalog |
pages
property
Pages of the document as an iterable/addressable PageList
object.
structure
property
Logical structure of this document, if any.
In the case where no logical structure tree exists, this will
be None
. Otherwise you may iterate over it, search it, etc.
We do this instead of simply returning an empty structure
tree because the vast majority of PDFs have no logical
structure.
tokens
property
Iterate over tokens.
__getitem__(objid)
Get an indirect object from the PDF.
Note that the behaviour in the case of a non-existent object
(raising IndexError
), while Pythonic, is not PDFic, as PDF
1.7 sec 7.3.10 states:
An indirect reference to an undefined object shall not be considered an error by a conforming reader; it shall be treated as a reference to the null object.
Raises:
Type | Description |
---|---|
ValueError
|
if Document is not initialized |
IndexError
|
if objid does not exist in PDF |
Source code in playa/document.py
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 |
|
__iter__()
Iterate over top-level IndirectObject
(does not expand object streams)
Source code in playa/document.py
352 353 354 355 356 357 358 359 |
|
_find_xref()
Internal function used to locate the first XRef.
Source code in playa/document.py
709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 |
|
_get_page_objects()
Iterate over the flattened page tree in reading order, propagating inheritable attributes. Returns an iterator over (objid, dict) pairs.
Raises:
Type | Description |
---|---|
KeyError
|
if there is no page tree. |
Source code in playa/document.py
636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 |
|
_get_pages_from_xrefs()
Find pages from the cross-reference tables if the page tree is missing (note that this only happens in invalid PDFs, but it happens.)
Returns:
Type | Description |
---|---|
Iterator[Tuple[int, PageType]]
|
an iterator over (objid, dict) pairs. |
Source code in playa/document.py
619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 |
|
_initialize_password(password='')
Initialize the decryption handler with a given password, if any.
Internal function, requires the Encrypt dictionary to have been read from the trailer into self.encryption.
Source code in playa/document.py
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 |
|
_read_xref_from(start, xrefs)
Reads XRefs from the given location.
Source code in playa/document.py
737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 |
|
PageLabels
Bases: NumberTree
PageLabels from the document catalog.
See Section 12.4.2 in the PDF 1.7 Reference.
Source code in playa/document.py
906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 |
|
_format_page_label(value, style)
staticmethod
Format page label value in a specific style
Source code in playa/document.py
948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 |
|
PageList
List of pages indexable by 0-based index or string label.
Attributes:
Name | Type | Description |
---|---|---|
have_labels |
bool
|
If pages have explicit labels in the PDF. |
Source code in playa/document.py
789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 |
|
doc
property
Get associated document if it exists.
by_id(objid)
Get a page by its indirect object ID.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
objid
|
int
|
Indirect object ID for the page object. |
required |
Returns:
Type | Description |
---|---|
Page
|
the page in question. |
Source code in playa/document.py
873 874 875 876 877 878 879 880 881 882 |
|
map(func)
Apply a function over each page, iterating over its results.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
func
|
Callable[[Page], Any]
|
The function to apply to each page. |
required |
Note
This possibly runs func
in a separate process. If its
return value is not serializable (by pickle
) then you
will encounter errors.
Source code in playa/document.py
884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 |
|
call_page(func, pageref)
Call a function on a page in a worker process.
Source code in playa/document.py
784 785 786 |
|
playa.page
Classes for looking at pages and their contents.
Annotation
dataclass
PDF annotation (PDF 1.7 section 12.5).
Attributes:
Name | Type | Description |
---|---|---|
subtype |
str
|
Type of annotation. |
rect |
Rect
|
Annotation rectangle (location on page) in default user space |
bbox |
Rect
|
Annotation rectangle in device space |
props |
Dict[str, PDFObject]
|
Annotation dictionary containing all other properties (PDF 1.7 sec. 12.5.2). |
Source code in playa/page.py
566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 |
|
bbox
property
Bounding box for this annotation in device space.
contents
property
Text contents of annotation.
mtime
property
String describing date and time when annotation was most recently modified.
The date should be in the format D:YYYYMMDDHHmmSSOHH'mm
but this is in no way required (and unlikely to be implemented
consistently, if history is any guide).
name
property
Annotation name, uniquely identifying this annotation.
page
property
Containing page for this annotation.
Page
An object that holds the information about a page.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
doc
|
Document
|
a Document object. |
required |
pageid
|
int
|
the integer PDF object ID associated with the page in the page tree. |
required |
attrs
|
Dict
|
a dictionary of page attributes. |
required |
label
|
Optional[str]
|
page label string. |
required |
page_idx
|
int
|
0-based index of the page in the document. |
0
|
space
|
DeviceSpace
|
the device space to use for interpreting content |
'screen'
|
Attributes:
Name | Type | Description |
---|---|---|
pageid |
the integer object ID associated with the page in the page tree |
|
attrs |
a dictionary of page attributes. |
|
resources |
Dict[str, PDFObject]
|
a dictionary of resources used by the page. |
mediabox |
the physical size of the page. |
|
cropbox |
the crop rectangle of the page. |
|
rotate |
the page rotation (in degree). |
|
label |
the page's label (typically, the logical page number). |
|
page_idx |
0-based index of the page in the document. |
|
ctm |
coordinate transformation matrix from default user space to page's device space |
Source code in playa/page.py
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 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 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 |
|
annotations
property
Lazily iterate over page annotations.
contents
property
Iterator over PDF objects in the content streams.
doc
property
Get associated document if it exists.
fonts
property
Mapping of resource names to fonts for this page.
This is not the same as playa.Document.fonts
.
The resource names (e.g. F1
, F42
, FooBar
) here are
specific to a page (or Form XObject) resource dictionary
and have no relation to the font name as commonly
understood (e.g. Helvetica
,
WQERQE+Arial-SuperBold-HJRE-UTF-8
). Since font names are
generally considered to be globally unique, it may be
possible to access fonts by them in the future.
Do not rely on this being a dict
.
Currently this is implemented eagerly, but in the future it may return a lazy object which only loads fonts on demand.
glyphs
property
Iterator over lazy glyph objects.
height
property
Width of the page in default user space units.
images
property
Iterator over lazy image objects.
mcid_texts
property
Mapping of marked content IDs to Unicode text strings.
For use in text extraction from tagged PDFs.
Do not rely on this being a dict
.
Currently this is implemented eagerly, but in the future it may return a lazy object.
parent_key
property
Parent tree key for this page, if any.
paths
property
Iterator over lazy path objects.
streams
property
Return resolved content streams.
structure
property
Mapping of marked content IDs to logical structure elements.
This is actually a list of logical structure elements
corresponding to marked content IDs, or None
for indices
which do not correspond to a marked content ID. Note that
because structure elements may contain multiple marked content
sections, the same element may occur multiple times in this
list.
This is not the same as playa.Document.structure
.
PDF documents have logical structure, but PDF pages do not, and it is dishonest to pretend otherwise (as some code I once wrote unfortunately does). What they do have is marked content sections which correspond to content items in the logical structure tree.
Do not rely on this being a list
.
Currently this is implemented eagerly, but in the future it may return a lazy object.
texts
property
Iterator over lazy text objects.
tokens
property
Iterator over tokens in the content streams.
width
property
Width of the page in default user space units.
xobjects
property
Return resolved and rendered Form XObjects.
This does not return any image or PostScript XObjects. You
can get images via the images
property. Apparently you
aren't supposed to use PostScript XObjects for anything, ever.
Note that these are the XObjects as rendered on the page, so
you may see the same named XObject multiple times. If you
need to access their actual definitions you'll have to look at
page.resources
.
This will also return Form XObjects within Form XObjects, except in the case of circular reference chains.
__iter__()
Iterator over lazy layout objects.
Source code in playa/page.py
243 244 245 |
|
extract_text()
Do some best-effort text extraction.
This necessarily involves a few heuristics, so don't get your hopes up. It will attempt to use marked content information for a tagged PDF, otherwise it will fall back on the character displacement and line matrix to determine word and line breaks.
Source code in playa/page.py
434 435 436 437 438 439 440 441 442 443 444 445 |
|
extract_text_tagged()
Get text from a page of a tagged PDF.
Source code in playa/page.py
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 |
|
extract_text_untagged()
Get text from a page of an untagged PDF.
Source code in playa/page.py
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 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 |
|
flatten(filter_class=None)
flatten() -> Iterator[ContentObject]
flatten(filter_class: Type[CO]) -> Iterator[CO]
Iterate over content objects, recursing into form XObjects.
Source code in playa/page.py
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 |
|
playa.content
PDF content objects created by the interpreter.
ContentObject
dataclass
Any sort of content object.
Attributes:
Name | Type | Description |
---|---|---|
gstate |
GraphicState
|
Graphics state. |
ctm |
Matrix
|
Coordinate transformation matrix (PDF 1.7 section 8.3.2). |
mcstack |
Tuple[MarkedContent, ...]
|
Stack of enclosing marked content sections. |
Source code in playa/content.py
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 |
|
bbox
property
The bounding box in device space of this object.
doc
property
The document containing this content object.
mcid
property
The marked content ID of the nearest enclosing marked content section with an ID.
This is notably what you should use (and what parent
uses)
to find the parent logical structure element, because (PDF
14.7.5.1.1):
A marked-content sequence corresponding to a structure content item shall not have another marked-content sequence for a structure content item nested within it though non-structural marked-content shall be allowed.
mcs
property
The immediately enclosing marked content section.
object_type
property
Type of this object as a string, e.g. "text", "path", "image".
page
property
The page containing this content object.
parent
property
The enclosing logical structure element, if any.
__len__()
Return the number of children of this object (generic implementation).
Source code in playa/content.py
247 248 249 |
|
DashPattern
Bases: NamedTuple
Line dash pattern in PDF graphics state (PDF 1.7 section 8.4.3.6).
Attributes:
Name | Type | Description |
---|---|---|
dash |
Tuple[float, ...]
|
lengths of dashes and gaps in user space units |
phase |
float
|
starting position in the dash pattern |
Source code in playa/content.py
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
|
GlyphObject
dataclass
Bases: ContentObject
Individual glyph on the page.
Attributes:
Name | Type | Description |
---|---|---|
font |
Font
|
Font for this glyph. |
size |
float
|
Effective font size for this glyph. |
cid |
int
|
Character ID for this glyph. |
text |
Union[str, None]
|
Unicode mapping of this glyph, if any. |
matrix |
Matrix
|
Rendering matrix |
origin |
Point
|
Origin of this glyph in device space. |
displacement |
Point
|
Vector to the origin of the next glyph in device space. |
bbox |
Rect
|
glyph bounding box in device space. |
Source code in playa/content.py
742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 |
|
__iter__()
Possibly iterate over paths in a glyph.
For Type3 fonts, you can iterate over paths (or anything else) inside a glyph, in the coordinate space defined by the text rendering matrix.
Otherwise, you can't do that, and you get nothing.
Source code in playa/content.py
766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 |
|
GraphicState
dataclass
PDF graphics state (PDF 1.7 section 8.4) including text state (PDF 1.7 section 9.3.1), but excluding coordinate transformations.
Contrary to the pretensions of pdfminer.six, the text state is for
the most part not at all separate from the graphics state, and can
be updated outside the confines of BT
and ET
operators, thus
there is no advantage and only confusion that comes from treating
it separately.
The only state that does not persist outside BT
/ ET
pairs is
the text coordinate space (line matrix and text rendering matrix),
and it is also the only part that is updated during iteration over
a TextObject
.
For historical reasons the main coordinate transformation matrix, though it is also part of the graphics state, is also stored separately.
Attributes:
Name | Type | Description |
---|---|---|
clipping_path |
None
|
The current clipping path (sec. 8.5.4) |
linewidth |
float
|
Line width in user space units (sec. 8.4.3.2) |
linecap |
int
|
Line cap style (sec. 8.4.3.3) |
linejoin |
int
|
Line join style (sec. 8.4.3.4) |
miterlimit |
float
|
Maximum length of mitered line joins (sec. 8.4.3.5) |
dash |
DashPattern
|
Dash pattern for stroking (sec 8.4.3.6) |
intent |
PSLiteral
|
Rendering intent (sec. 8.6.5.8) |
stroke_adjustment |
bool
|
A flag specifying whether to compensate for possible rasterization effects when stroking a path with a line width that is small relative to the pixel resolution of the output device (sec. 10.7.5) |
blend_mode |
Union[PSLiteral, List[PSLiteral]]
|
The current blend mode that shall be used in the transparent imaging model (sec. 11.3.5) |
smask |
Union[None, Dict[str, PDFObject]]
|
A soft-mask dictionary (sec. 11.6.5.1) or None |
salpha |
float
|
The constant shape or constant opacity value used for stroking operations (sec. 11.3.7.2 & 11.6.4.4) |
nalpha |
float
|
The constant shape or constant opacity value used for non-stroking operations |
alpha_source |
bool
|
A flag specifying whether the current soft mask and alpha constant parameters shall be interpreted as shape values (true) or opacity values (false). This flag also governs the interpretation of the SMask entry, if any, in an image dictionary |
black_pt_comp |
PSLiteral
|
The black point compensation algorithm that shall be used when converting CIE-based colours (sec. 8.6.5.9) |
flatness |
float
|
The precision with which curves shall be rendered on the output device (sec. 10.6.2) |
scolor |
Color
|
Colour used for stroking operations |
scs |
ColorSpace
|
Colour space used for stroking operations |
ncolor |
Color
|
Colour used for non-stroking operations |
ncs |
ColorSpace
|
Colour space used for non-stroking operations |
font |
Union[Font, None]
|
The current font. |
fontsize |
float
|
The "font size" parameter, which is not the font size in points as you might understand it, but rather a scaling factor applied to text space (so, it affects not only text size but position as well). Since most reasonable people find that behaviour rather confusing, this is often just 1.0, and PDFs rely on the text matrix to set the size of text. |
charspace |
float
|
Extra spacing to add after each glyph, expressed in
unscaled text space units, meaning it is not affected by
|
wordspace |
float
|
Extra spacing to add after a space glyph, defined
very specifically as the glyph encoded by the single-byte
character code 32 (SPOILER: it is probably a space). Also
expressed in unscaled text space units, but modified by
|
scaling |
float
|
The horizontal scaling factor as defined by the PDF standard (that is, divided by 100). |
leading |
float
|
The leading as defined by the PDF standard, in unscaled text space units. |
render_mode |
int
|
The PDF rendering mode. The really important one here is 3, which means "don't render the text". You might want to use this to detect invisible text. |
rise |
float
|
The text rise (superscript or subscript position), in unscaled text space units. |
knockout |
bool
|
The text knockout flag, shall determine the behaviour of overlapping glyphs within a text object in the transparent imaging model (sec. 9.3.8) |
Source code in playa/content.py
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 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 |
|
ImageObject
dataclass
Bases: ContentObject
An image (either inline or XObject).
Attributes:
Name | Type | Description |
---|---|---|
xobjid |
Union[str, None]
|
Name of XObject (or None for inline images). |
srcsize |
Tuple[int, int]
|
Size of source image in pixels. |
bits |
int
|
Number of bits per component, if required (otherwise 1). |
imagemask |
bool
|
True if the image is a mask. |
stream |
ContentStream
|
Content stream with image data. |
colorspace |
Union[ColorSpace, None]
|
Colour space for this image, if required (otherwise None). |
Source code in playa/content.py
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 |
|
buffer
property
Binary stream content for this image
parent
property
The enclosing logical structure element, if any.
__len__()
Even though you can getitem from an image you cannot iterate over its keys, sorry about that. Returns zero.
Source code in playa/content.py
403 404 405 406 |
|
MarkedContent
Bases: NamedTuple
Marked content point or section in a PDF page or Form XObject.
Attributes:
Name | Type | Description |
---|---|---|
mcid |
Union[int, None]
|
Marked content section ID, or |
tag |
str
|
Name of tag for this marked content. |
props |
Dict[str, PDFObject]
|
Marked content property dictionary. |
Source code in playa/content.py
201 202 203 204 205 206 207 208 209 210 211 212 213 |
|
PathObject
dataclass
Bases: ContentObject
A path object.
Attributes:
Name | Type | Description |
---|---|---|
raw_segments |
List[PathSegment]
|
Segments in path (in user space). |
stroke |
bool
|
True if the outline of the path is stroked. |
fill |
bool
|
True if the path is filled. |
evenodd |
bool
|
True if the filling of complex paths uses the even-odd winding rule, False if the non-zero winding number rule is used (PDF 1.7 section 8.5.3.3) |
Source code in playa/content.py
684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 |
|
bbox
property
Get bounding box of path in device space as defined by its points and control points.
segments
property
Get path segments in device space.
PathSegment
Bases: NamedTuple
Segment in a PDF graphics path.
Source code in playa/content.py
219 220 221 222 223 224 225 |
|
TagObject
dataclass
Bases: ContentObject
A marked content tag..
Source code in playa/content.py
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 |
|
bbox
property
A tag has no content and thus no bounding box.
To avoid needlessly complicating user code this returns
BBOX_NONE
instead of None
or throwing a exception.
Because that is a specific object, you can reliably check for
it with:
if obj.bbox is BBOX_NONE:
...
mcid
property
The marked content ID of the nearest enclosing marked content section with an ID.
mcs
property
The marked content tag for this object.
__len__()
A tag has no contents, iterating over it returns nothing.
Source code in playa/content.py
341 342 343 |
|
TextObject
dataclass
Bases: ContentObject
Text object (contains one or more glyphs).
Attributes:
matrix: Initial rendering matrix T_rm
for this text object,
which transforms text space coordinates to device space
(PDF 2.0 section 9.4.4).
origin: Origin of this text object in device space.
displacement: Vector to the origin of the next text object in
device space.
size: Effective font size for this text object.
text_matrix: Text matrix T_m
for this text object, which
transforms text space coordinates to user space.
line_matrix: Text line matrix T_lm
for this text object, which
is the text matrix at the beginning of the "current
line" (PDF 2.0 section 9.4.1). Note that this is
not reliable for detecting line breaks.
scaling_matrix: The anonymous but rather important matrix which
applies font size, horizontal scaling and rise to
obtain the rendering matrix (PDF 2.0 sec 9.4.4).
args: Strings or position adjustments.
bbox: Text bounding box in device space.
Source code in playa/content.py
856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 |
|
chars
property
Get the Unicode characters (in stream order) for this object.
__iter__()
Generate glyphs for this text object
Source code in playa/content.py
892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 |
|
__len__()
Return the number of glyphs that would result from iterating over this object.
Important: this is the number of glyphs, not the number of Unicode characters.
Source code in playa/content.py
1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 |
|
XObjectObject
dataclass
Bases: ContentObject
An eXternal Object, in the context of a page.
There are a couple of kinds of XObjects. Here we are only concerned with "Form XObjects" which, despite their name, have nothing at all to do with fillable forms. Instead they are like little embeddable PDF pages, possibly with their own resources, definitely with their own definition of "user space".
Image XObjects are handled by ImageObject
.
Attributes:
Name | Type | Description |
---|---|---|
xobjid |
str
|
Name of this XObject (in the page resources). |
stream |
ContentStream
|
Content stream with PDF operators. |
resources |
Union[None, Dict[str, PDFObject]]
|
Resources specific to this XObject, if any. |
group |
Union[None, Dict[str, PDFObject]]
|
Transparency group, if any. |
Source code in playa/content.py
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 |
|
bbox
property
Get the bounding box of this XObject in device space.
buffer
property
Raw stream content for this XObject
contents
property
Iterator over PDF objects in the content stream.
mcid_texts
property
Mapping of marked content IDs to Unicode text strings.
For use in text extraction from tagged PDFs.
Do not rely on this being a dict
.
Currently this is implemented eagerly, but in the future it may return a lazy object.
parent
property
The enclosing logical structure element, if any.
structure
property
Mapping of marked content IDs to logical structure elements.
As with pages, Form XObjects can also contain their own mapping of marked content IDs to structure elements.
Do not rely on this being a list
.
Currently this is implemented eagerly, but in the future it may return a lazy object.
tokens
property
Iterate over tokens in the XObject's content stream.
from_stream(stream, page, xobjid, gstate, ctm, mcstack)
classmethod
Create a new XObjectObject from a content stream.
Source code in playa/content.py
626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 |
|
playa.structure
Lazy interface to PDF logical structure (PDF 1.7 sect 14.7).
ContentItem
dataclass
Content item in logical structure tree.
This corresponds to an individual marked content section on a specific page, and can be used to (lazily) find that section if desired.
Source code in playa/structure.py
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
|
bbox
property
Find the bounding box, if any, of this item, which is the smallest rectangle enclosing all objects in its marked content section.
Note that this is currently quite inefficient as it involves interpreting the entire page.
If the page
attribute is None
, then bbox
will be
BBOX_NONE
.
doc
property
The document containing this content object.
page
property
Specific page for this structure tree, if any.
text
property
Unicode text contained in this structure element.
ContentObject
dataclass
Content object in logical structure tree.
This corresponds to a content item that is an entire PDF (X)Object (PDF 1.7 section 14.7.43), and can be used to (lazily) get that
The standard is very unclear on what this could be aside from an
Annotation
or an XObject
(presumably either a Form XObject or
an image). An XObject must be a content stream, so that's clear
enough... Otherwise, since the Type
key is not required in an
annotation dictionary we presume that this is an annotation if
it's not present.
Not to be confused with playa.page.ContentObject
. While you
can get there from here with the obj
property, it may not be a
great idea, because the only way to do that correctly in the case
of an XObject
(or image) is to interpret the containing page.
Sometimes, but not always, you can nonetheless rapidly access the
bbox
, so this is also provided as a property here.
Source code in playa/structure.py
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 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 |
|
bbox
property
Find the bounding box, if any, of this object.
If there is no bounding box (very unlikely) this will be
BBOX_NONE
.
doc
property
The document containing this content object.
obj
property
Return an instantiated object, if possible.
page
property
Containing page for this content object.
type
property
Type of this object, usually LITERAL_ANNOT or LITERAL_XOBJECT.
Element
dataclass
Bases: Findable
Logical structure element.
Attributes:
Name | Type | Description |
---|---|---|
props |
Dict[str, PDFObject]
|
Structure element dictionary (PDF 1.7 table 323). |
Source code in playa/structure.py
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 |
|
bbox
property
The bounding box, if any, of this element.
Elements may explicitly define a BBox
in default user space,
in which case this is used. Otherwise, the bounding box is
the smallest rectangle enclosing all of the content items
contained by this element (which may take some time to compute).
Note that this is currently quite inefficient as it involves interpreting the entire page.
Elements may span multiple pages!
In the case of an element (such as a Document
for
instance) that spans multiple pages, the bounding box
cannot exist, and BBOX_NONE
will be returned. If the
page
attribute is None
, then bbox
will be
BBOX_NONE
.
contents
property
Iterate over all content items contained in an element.
doc
property
Containing document for this element.
page
property
Containing page for this element, if any.
role
property
Standardized structure type.
Roles are always mapped
Since it is common for documents to use standard types
directly for some of their structure elements (typically
ones with no content) and thus to omit them from the role
map, role
will always return a string in order to
facilitate processing. If you must absolutely know
whether an element's type has no entry in the role map
then you will need to consult it directly.
type
property
Structure type for this element.
Raw and standard structure types
This type is quite likely idiosyncratic and defined by
whatever style sheets the author used in their word
processor. Standard structure types (PDF 1.7 section
14.8.4) are accessible through the role_map
attribute of
the structure root, or, for convenience (this is slow) via
the role
attribute on elements.
from_dict(doc, obj)
classmethod
Construct from PDF structure element dictionary.
Source code in playa/structure.py
311 312 313 314 |
|
Findable
Bases: Iterable
find() and find_all() methods that can be inherited to avoid repeating oneself
Source code in playa/structure.py
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 |
|
find(matcher=None)
Find the first matching element in subtree.
The matcher
argument is either a string or a regular
expression to be matched against the role
attribute, or a
function taking a Element
and returning True
if the
element matches, or None
(default) to just get the first
child element.
Source code in playa/structure.py
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 |
|
find_all(matcher=None)
Iterate depth-first over matching elements in subtree.
The matcher
argument is either a string, a regular
expression, or a function taking a Element
and returning
True
if the element matches, or None
(default) to return
all descendants in depth-first order.
For compatibility with pdfplumber
and consistent behaviour
across documents, names and regular expressions are matched
against the role
attribute. If you wish to match the "raw"
structure type from the type
attribute, you can do this with
a matching function.
Source code in playa/structure.py
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 |
|
Tree
Bases: Findable
Logical structure tree.
A structure tree can be iterated over in the same fashion as its
elements. Note that even though it is forbidden for structure
tree root to contain content items, PLAYA is robust to this
possibility, thus you should not presume that iterating over it
will only yield Element
instances.
The various attributes (role map, class map, pronunciation
dictionary, etc, etc) are accessible through props
but currently
have no particular interpretation aside from the role map which is
accessible in normalized form through role_map
.
Attributes:
Name | Type | Description |
---|---|---|
props |
Dict[str, PDFObject]
|
Structure tree root dictionary (PDF 1.7 table 322). |
role_map |
Dict[str, str]
|
Mapping of structure element types (as strings) to standard structure types (as strings) (PDF 1.7 section 14.8.4) |
parent_tree |
NumberTree
|
Parent tree linking marked content sections to structure elements (PDF 1.7 section 14.7.4.4) |
Source code in playa/structure.py
587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 |
|
contents
property
Iterate over all content items in the tree.
doc
property
Document with which this structure tree is associated.
parent_tree
property
Parent tree for this document.
This is a somewhat obscure data structure that links marked content sections to their corresponding structure elements. If you don't know what that means, you probably don't need it, but if you do, here it is.
Unlike the structure tree itself, if there is no parent tree, this will be an empty NumberTree. This is because the parent tree is required by the spec in the case where structure elements contain marked content, which is nearly all the time.
role_map
property
Dictionary mapping some (not necessarily all) element types to their standard equivalents.
playa.outline
Lazy interface to PDF document outline (PDF 1.7 sect 12.3.3).
Action
dataclass
PDF actions (PDF 1.7 sect 12.6)
Source code in playa/outline.py
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
|
destination
property
Destination of this action, if any.
doc
property
Get associated document if it exists.
Destination
dataclass
PDF destinations (PDF 1.7 sect 12.3.2)
Source code in playa/outline.py
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
|
Outline
PDF document outline (PDF 1.7 sect 12.3.3)
Source code in playa/outline.py
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 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 |
|
destination
property
Destination for this outline item.
Special case of GoTo
actions.
Since internal GoTo
actions (PDF 1.7 sect 12.6.4.2) in
outlines and links are entirely equivalent to
destinations, if one exists, it will be returned here as
well.
Returns:
Type | Description |
---|---|
Union[Destination, None]
|
destination, if one exists. |
doc
property
Get associated document if it exists.
element
property
The structure element associated with this outline item, if any.
Returns:
Type | Description |
---|---|
Union[Element, None]
|
structure element, if one exists. |
playa.font
Font metrics and descriptors
API subject to change.
These APIs are unstable and subject to revision before PLAYA 1.0.
CIDFont
Bases: Font
Source code in playa/font.py
497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 |
|
char_bbox(cid)
Get the standard bounding box for a character from its CID.
This is the standard bounding box in text space units based on width, descent and ascent, translated by the position vector.
Not the actual bounding box of the glyph.
This is a standardized bounding box for use in text extraction and layout analysis. It does not correspond to the actual bounding box of an individual glyph as specified by the font program.
Source code in playa/font.py
710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 |
|
get_cmap_from_spec(spec)
Get cmap from font specification
For certain PDFs, Encoding Type isn't mentioned as an attribute of Encoding but as an attribute of CMapName, where CMapName is an attribute of spec['Encoding']. The horizontal/vertical modes are mentioned with different name such as 'DLIdent-H/V','OneByteIdentityH/V','Identity-H/V'.
Source code in playa/font.py
612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 |
|
position(cid)
Get position vector for vertical writing mode, in text space units.
This is quite ill-defined in the PDF standard (PDF 2.0 Figure
55), but basically it specifies a translation of the glyph
with respect to the origin. It is subtracted from that
origin to give the glyph position. So if your text matrix is
[1 0 0 1 100 100]
, and your font size is 10
, a position
vector of [500 200]
will place the origin of the glyph in
glyph space at [-500 -200]
, which becomes [-.5 -.2]
in
text space, then [-5 -2]
after applying the font size, thus
the glyph is painted with its origin at [95 98]
.
Yes, the horizontal scaling factor does apply to the horizontal component of the position vector, even if some PDF viewers don't think so.
For horizontal writing, it is obviously (0, 0).
Source code in playa/font.py
684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 |
|
vdisp(cid)
Get vertical displacement for vertical writing mode, in text space units.
Returns 0 for horizontal writing, for obvious reasons.
Source code in playa/font.py
676 677 678 679 680 681 682 |
|
Font
Source code in playa/font.py
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
|
char_bbox(cid)
Get the standard bounding box for a character from its CID.
This is, very specifically, [0 descent width ascent]
in text
space units.
Not the actual bounding box of the glyph.
This is a standardized bounding box for use in text extraction and layout analysis. It does not correspond to the actual bounding box of an individual glyph as specified by the font program.
Source code in playa/font.py
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
|
hdisp(cid)
Get the horizontal displacement (so-called "width") of a character from its CID.
Source code in playa/font.py
129 130 131 132 133 |
|
position(cid)
Get position vector for vertical writing mode, in text space units.
This is always [0 0]
for simple fonts as they have no
vertical writing mode.
Source code in playa/font.py
145 146 147 148 149 150 151 152 |
|
vdisp(cid)
Get vertical displacement for vertical writing mode, in text space units.
This is always 0 for simple fonts as they have no vertical writing mode.
Source code in playa/font.py
135 136 137 138 139 140 141 142 143 |
|
Type1Font
Bases: SimpleFont
Source code in playa/font.py
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 |
|
char_bbox(cid)
Get the standard bounding box for a character from its CID.
This is, very specifically, [0 descent width ascent]
in text
space units.
Not the actual bounding box of the glyph.
This is a standardized bounding box for use in text extraction and layout analysis. It does not correspond to the actual bounding box of an individual glyph as specified by the font program.
Source code in playa/font.py
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 |
|
hdisp(cid)
Get the horizontal displacement (so-called "width") of a character from its CID.
Source code in playa/font.py
321 322 323 324 |
|
Type3Font
Bases: SimpleFont
Source code in playa/font.py
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 |
|
char_bbox(cid)
Get the standard bounding box for a character from its CID.
This is the smallest rectangle enclosing [0 descent width ascent] after the font matrix has been applied.
Not the actual bounding box of the glyph (but almost).
The descent and ascent here are from the font and not from the individual glyph so this will be somewhat larger than the actual bounding box.
Source code in playa/font.py
423 424 425 426 427 428 429 430 431 432 433 434 435 |
|
playa.parser
PDF lexer and parser
API subject to change.
These APIs are unstable and subject to revision before PLAYA 1.0.
ContentParser
Bases: ObjectParser
Parse the concatenation of multiple content streams, as described in the spec (PDF 1.7, p.86):
...the effect shall be as if all of the streams in the array were concatenated, in order, to form a single stream. Conforming writers can create image objects and other resources as they occur, even though they interrupt the content stream. The division between streams may occur only at the boundaries between lexical tokens (see 7.2, "Lexical Conventions") but shall be unrelated to the page’s logical content or organization.
Source code in playa/parser.py
888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 |
|
nexttoken()
Override nexttoken() to continue parsing in subsequent streams.
TODO: If we want to avoid evil implementation inheritance, we should do this in the lexer instead.
Source code in playa/parser.py
912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 |
|
IndirectObjectParser
IndirectObjectParser fetches indirect objects from a data stream. It holds a weak reference to the document in order to resolve indirect references. If the document is deleted then this will obviously no longer work.
Note that according to PDF 1.7 sec 7.5.3, "The body of a PDF file
shall consist of a sequence of indirect objects representing the
contents of a document." Therefore unlike the base ObjectParser
,
IndirectObjectParser
returns only indrect objects and not bare
keywords, strings, numbers, etc.
However, unlike ObjectParser
, it will also read and return
ContentStream
s, as these must be indirect objects by definition.
Typical usage
parser = IndirectObjectParser(fp, doc) for object in parser: ...
Source code in playa/parser.py
636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 |
|
doc
property
Get associated document if it exists.
reset()
Clear internal parser state.
Source code in playa/parser.py
842 843 844 |
|
seek(pos)
Seek to a position.
Source code in playa/parser.py
838 839 840 |
|
Lexer
Lexer for PDF data.
Source code in playa/parser.py
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 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 |
|
__iter__()
Iterate over tokens.
Source code in playa/parser.py
185 186 187 |
|
__next__()
Get the next token in iteration, raising StopIteration when done.
Source code in playa/parser.py
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 |
|
nextline()
Get the next line ending either with \r, \n, or \r\n, starting at the current position.
Source code in playa/parser.py
174 175 176 177 178 179 180 181 182 183 |
|
read(objlen)
Read data from current position, advancing to the end of this data.
Source code in playa/parser.py
167 168 169 170 171 172 |
|
seek(pos)
Seek to a position and reinitialize parser state.
Source code in playa/parser.py
156 157 158 159 160 161 |
|
tell()
Get the current position in the buffer.
Source code in playa/parser.py
163 164 165 |
|
ObjectParser
ObjectParser is used to parse PDF object streams (and
content streams, which have the same syntax). Notably these
consist of, well, a stream of objects without the surrounding
obj
and endobj
tokens (which cannot occur in an object
stream).
They can contain indirect object references (so, must be
initialized with a Document
to resolve these) but for perhaps
obvious reasons (how would you parse that) these cannot occur at
the top level of the stream, only inside an array or dictionary.
Source code in playa/parser.py
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 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 |
|
doc
property
Get associated document if it exists.
__iter__()
Iterate over (position, object) tuples.
Source code in playa/parser.py
326 327 328 |
|
__next__()
Get next PDF object from stream (raises StopIteration at EOF).
Source code in playa/parser.py
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 |
|
get_inline_image(pos, token)
Get an inline image upon finding an "ID" token.
Returns a tuple of the position of the target in the data and the image data. Advances the file pointer to a position after the "EI" token that (we hope) ends the image.
WELCOME IN THE HELL!!!
If you're lucky enough to have PDF 2.0 documents, then you can skip this, you aren't actually in (the) hell. Otherwise read on to know why you might be missng images or reading a lot of garbage in your logs:
- The PDF 1.7 standard only specifies that image data must be
delimited by
ID
andEI
, and that "The bytes between theID
andEI
operators shall be treated the same as a stream object’s data, even though they do not follow the standard stream syntax." What does that even mean? - And, that must be "a single whitespace character"
following
ID
(floating in perfume, served in a man's hat), except in the case ofASCIIHexDecode
orASCII85Decode
(in which case there can just be any whitespace you like, or none at all). - It's obviously impossible to determine what a "conforming implementation" of this should do.
In the easiest case, if it's ASCIIHexDecode
data then we
can just look for the first instance of b"EI"
, ignoring all
whitespace, since b"EI"
is thankfully not a valid hex
sequence.
Otherwise, the stream data can, and inevitably will,
contain the literal bytes b"EI"
, so no, we can't just
search for that. In the case of ASCII85Decode
, however,
you can look for the b"~>"
end-of-data sequence but note
that sometimes it... contains whitespace!
So, we try for b"\sEI\b"
, which is not foolproof since
you could have the pixel values (32, 69, 73)
in your
image followed by some other byte... so in that case,
expect a bunch of nonsense in the logs and possible data
loss. Also in the rare case where b"EI"
was preceded by
b"\r\n"
, there will be an extra \r
in the image
data. Too bad.
And finally if that doesn't work then we will try to salvage something by just looking for "EI", somewhere, anywhere. We take the most distant one, and if this causes you to lose data, well, it's definitely Adobe's fault.
There is an absolutely foolproof way to parse inline images, but it's ridiculous so we won't do it:
- Find the very first instance of
b"EI"
. - Extract the image itself (which could be in various formats).
- If it's a valid image, congratulations! Otherwise try again.
The moral of the story is that the author of this part of the PDF specification should have considered a career in literally anything else.
Source code in playa/parser.py
462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 |
|
get_object_reference(pos, token)
Get an indirect object reference upon finding an "R" token.
Source code in playa/parser.py
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 |
|
newstream(data)
Continue parsing from a new data stream.
Source code in playa/parser.py
318 319 320 |
|
nextline()
Read (and do not parse) next line from underlying data.
Source code in playa/parser.py
617 618 619 |
|
nexttoken()
Get the next token in iteration, raising StopIteration when done.
Source code in playa/parser.py
621 622 623 624 |
|
pop_to(token)
Pop everything from the stack back to token.
Source code in playa/parser.py
425 426 427 428 429 430 431 432 433 434 |
|
read(objlen)
Read data from a specified position, moving the current position to the end of this data.
Source code in playa/parser.py
612 613 614 615 |
|
reset()
Clear internal parser state.
Source code in playa/parser.py
322 323 324 |
|
seek(pos)
Seek to a position.
Source code in playa/parser.py
604 605 606 |
|
tell()
Get the current position in the file.
Source code in playa/parser.py
608 609 610 |
|
ObjectStreamParser
Parse indirect objects from an object stream.
Source code in playa/parser.py
847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 |
|
reverse_iter_lines(buffer)
Iterate backwards over lines starting at the current position.
This is used to locate the trailers at the end of a file.
Source code in playa/parser.py
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
|
playa.worker
Worker subprocess related functions and data.
in_worker()
Are we currently in a worker process?
Source code in playa/worker.py
25 26 27 |
|