Data API
Overview
The playa.data
module contains schemas (as TypedDict
) and extractors
for metadata and content from various PLAYA objects, as well as a
single-dispatch function playa.data.asobj
to extract said metadata
from any object. This is an entirely non-lazy API. It is provided
here because the PLAYA CLI uses it, and to discourage users of the
library from reimplementing it themselves.
Many PLAYA objects are already dataclass
or NamedTuple
so they
have a function or method to convert them to dict
, but for a variety
of reasons you shouldn't actually use this function. See here for
dataclasses.asdict
and its many
pitfalls.
The distinction between "metadata" and "content" is admittedly not
very clear for PDF. Metadata, represented by the schemas in
playa.data.metadata
, is:
- attributes of pages, content streams, etc (uncontroversial)
- outline and logical structure nodes and their properties (but not their contents)
- annotations and their properties
- articles and threads (TODO: PLAYA doesn't support these yet, I need to find some PDFs that have them)
Content, represented by the schemas in playa.data.content
, is:
- data in content streams (except for object streams, obviously)
- anything that is a
ContentObject
(so, paths, images, glyphs) - marked content sections (as these cannot be created without actually parsing the content streams)
Note that the CLI doesn't exactly break things down along those lines.
In particular, the default metadata output doesn't include outlines,
logical structure trees, or annotations. In general, if you have
performance concerns you are always better off using the lazy API,
then calling asobj
on specific objects as needed, as extracting all
the metadata (not even the content) may be fairly slow.
Data and Metadata Objects
All of these objects are accessible through the playa.data
module.
Annotation
Bases: TypedDict
Source code in playa/data/metadata.py
160 161 162 163 164 165 166 167 168 169 170 171 |
|
contents
instance-attribute
Text contents.
mtime
instance-attribute
String describing date and time when annotation was most recently modified.
name
instance-attribute
Annotation name, uniquely identifying this annotation.
rect
instance-attribute
Annotation rectangle in default user space.
subtype
instance-attribute
Type of annotation.
Color
Bases: TypedDict
Source code in playa/data/content.py
111 112 113 114 115 |
|
pattern
instance-attribute
Pattern name in page resources, if any.
values
instance-attribute
Component values.
ColorSpace
Bases: TypedDict
Source code in playa/data/content.py
118 119 120 121 122 123 124 |
|
name
instance-attribute
Name of colour space.
ncomponents
instance-attribute
Number of components.
spec
instance-attribute
Specification.
DashPattern
Bases: TypedDict
Source code in playa/data/content.py
104 105 106 107 108 |
|
dash
instance-attribute
Lengths of dashes and gaps in user space units.
phase
instance-attribute
Starting position in the dash pattern, default 0.
Destination
Bases: TypedDict
Destination for an outline entry or annotation.
Source code in playa/data/metadata.py
82 83 84 85 86 87 88 89 90 |
|
coords
instance-attribute
List of coordinates (meaning depends on display).
display
instance-attribute
How to display the destination on that page.
page_idx
instance-attribute
Zero-based index of destination page.
Document
Bases: TypedDict
Metadata for a PDF document.
Source code in playa/data/metadata.py
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
|
destinations
instance-attribute
Named destinations for this document.
encryption
instance-attribute
Encryption information for this document.
is_extractable
instance-attribute
Should the user be allowed to extract text?
is_modifiable
instance-attribute
Should the user be allowed to modify?
is_printable
instance-attribute
Should the user be allowed to print?
objects
instance-attribute
Indirect objects in this document.
outline
instance-attribute
Outline hierarchy for this document.
pages
instance-attribute
Pages in this document.
pdf_version
instance-attribute
Version of the PDF standard this document implements.
structure
instance-attribute
Logical structure for this document..
Encryption
Bases: TypedDict
Encryption information.
Source code in playa/data/metadata.py
62 63 64 65 66 67 68 |
|
encrypt
instance-attribute
Encryption properties.
ids
instance-attribute
ID values for encryption.
Font
Bases: TypedDict
Source code in playa/data/metadata.py
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 |
|
ascent
instance-attribute
Ascent in glyph space units.
avg_width
instance-attribute
Average width of glyphs in glyph space units.
bbox
instance-attribute
Bounding box in glyph space units.
cap_height
instance-attribute
Top of capital letters, in glyph space units.
cidfont
instance-attribute
Descendant CIDFont (Type0 fonts only).
default_width
instance-attribute
Default character width in glyph space units.
descent
instance-attribute
Descent in glyph space units.
flags
instance-attribute
Set of flags (e.g. FixedPitch, Script, etc) for this font.
italic_angle
instance-attribute
Italic angle.
leading
instance-attribute
Leading in glyph space units.
matrix
instance-attribute
Matrix mapping glyph space to text space (Type3 fonts only).
max_width
instance-attribute
Maximum width of glyphs in glyph space units.
name
instance-attribute
Font name.
stem_h
instance-attribute
Thickness of dominant horizontal stems.
stem_v
instance-attribute
Thickness of dominant vertical stems.
stretch
instance-attribute
Font stretch value (e.g. Condensed, Expanded).
type
instance-attribute
Font type (Type1, Type0, TrueType, Type3, etc).
weight
instance-attribute
Numeric weight value (100, 200, 300... 900)
xheight
instance-attribute
Top of flat nonascending lowercase letters, in glyph space units.
Glyph
Bases: TypedDict
Source code in playa/data/content.py
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
|
bbox
instance-attribute
Bounding rectangle for all glyphs in text in default user space.
cid
instance-attribute
Character ID for glyph in font.
ctm
instance-attribute
Coordinate transformation matrix, default if not present is the
identity matrix [1 0 0 1 0 0]
.
gstate
instance-attribute
Graphic state.
mcstack
instance-attribute
Stack of enclosing marked content sections.
text
instance-attribute
Unicode string representation of glyph, if any.
textstate
instance-attribute
Text state.
GraphicState
Bases: TypedDict
Source code in playa/data/content.py
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
|
dash
instance-attribute
Dash pattern for stroking (sec 8.4.3.6), default solid line ((), 0)
flatness
instance-attribute
The precision with which curves shall be rendered on the output device (sec. 10.6.2), default 1
intent
instance-attribute
Rendering intent (sec. 8.6.5.8), default RelativeColorimetric
'
linecap
instance-attribute
Line cap style (sec. 8.4.3.3), default 0
linejoin
instance-attribute
Line join style (sec. 8.4.3.4), default 0
linewidth
instance-attribute
Line width in user space units (sec. 8.4.3.2), default 1
miterlimit
instance-attribute
Maximum length of mitered line joins (sec. 8.4.3.5), default 10
ncolor
instance-attribute
Colour used for non-stroking operations, default black ((0,) None)
ncs
instance-attribute
Colour space used for non-stroking operations, default DeviceGray
scolor
instance-attribute
Colour used for stroking operations, default black ((0,), None)
scs
instance-attribute
Colour space used for stroking operations, default DeviceGray
Image
Bases: TypedDict
Source code in playa/data/content.py
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
|
bbox
instance-attribute
Bounding rectangle for image in default user space.
bits
instance-attribute
Number of bits per component, if required (default 1).
colorspace
instance-attribute
Colour space for this image, if required.
imagemask
instance-attribute
True if the image is a mask.
srcsize
instance-attribute
Size of source image in pixels.
stream
instance-attribute
Content stream dictionary.
xobject_name
instance-attribute
Name of XObject in page resources, if any.
IndirectObject
Bases: TypedDict
Source code in playa/data/metadata.py
204 205 206 207 208 209 210 211 212 |
|
genno
instance-attribute
Generation number.
obj
instance-attribute
Object metadata (for streams) or data (otherwise).
objid
instance-attribute
Indirect object ID.
type
instance-attribute
Name of Python type to which this object was converted.
MarkedContent
Bases: TypedDict
Source code in playa/data/content.py
127 128 129 130 131 132 133 |
|
mcid
instance-attribute
Marked content section ID.
name
instance-attribute
Marked content section tag name.
props
instance-attribute
Marked content property dictionary (without MCID).
Outline
Bases: TypedDict
Outline hierarchy for a PDF document.
Source code in playa/data/metadata.py
71 72 73 74 75 76 77 78 79 |
|
children
instance-attribute
Children of this entry.
destination
instance-attribute
Destination (or target of GoTo action).
title
instance-attribute
Title of this outline entry.
Page
Bases: TypedDict
Metadata for a PDF page.
Source code in playa/data/metadata.py
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
|
annotations
instance-attribute
Page annotations.
contents
instance-attribute
Metadata for content streams.
cropbox
instance-attribute
Extent of visible area, in base units (1/72 inch).
mediabox
instance-attribute
Extent of physical page, in base units (1/72 inch).
page_id
instance-attribute
Indirect object ID.
page_idx
instance-attribute
0-based page number.
page_label
instance-attribute
Page label (could be roman numerals, letters, etc).
resources
instance-attribute
Page resources.
rotate
instance-attribute
Page rotation in degrees.
Path
Bases: TypedDict
Source code in playa/data/content.py
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
|
components
instance-attribute
Subpaths, each of which consists of a list of segments.
evenodd
instance-attribute
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)
fill
instance-attribute
True if the path is filled.
gstate
instance-attribute
Graphic state.
mcstack
instance-attribute
Stack of enclosing marked content sections.
stroke
instance-attribute
True if the outline of the path is stroked.
PathSegment
Bases: TypedDict
Source code in playa/data/content.py
179 180 181 182 183 184 |
|
operator
instance-attribute
Normalized path operator (PDF 1.7 section 8.5.2). Note that "re" will be expanded into its constituent line segments.
points
instance-attribute
Point or control points in default user space for path segment.
Resources
Bases: TypedDict
Source code in playa/data/metadata.py
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 |
|
color_spaces
instance-attribute
Color space descriptors.
ext_gstates
instance-attribute
Extended graphic state dictionaries.
fonts
instance-attribute
Font dictionaries.
patterns
instance-attribute
Pattern objects.
procsets
instance-attribute
Procedure set names.
properties
instance-attribute
property dictionaires.
shadings
instance-attribute
Shading dictionaries.
xobjects
instance-attribute
XObject streams.
StreamObject
Bases: TypedDict
Source code in playa/data/metadata.py
191 192 193 194 195 196 197 198 199 200 201 |
|
filters
instance-attribute
List of filters.
genno
instance-attribute
Generation number.
length
instance-attribute
Length of raw stream data.
params
instance-attribute
Filter parameters.
stream_id
instance-attribute
Indirect object ID.
StructElement
Bases: TypedDict
Node in logical structure tree.
Source code in playa/data/metadata.py
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
|
abbreviation_expansion
instance-attribute
Abbreviation expansion.
actual_text
instance-attribute
Unicode text content.
alternate_description
instance-attribute
Alternate description.
children
instance-attribute
Children of this node.
language
instance-attribute
Language of structure element.
page_idx
instance-attribute
Page on which this structure element's content begins.
title
instance-attribute
Title of structure element.
type
instance-attribute
Type of structure element (or "StructTreeRoot" for root).
StructTree
Bases: TypedDict
Logical structure tree for a PDF document.
Source code in playa/data/metadata.py
130 131 132 133 134 |
|
root
instance-attribute
Root node of the tree.
Tag
Bases: TypedDict
Source code in playa/data/content.py
136 137 138 139 140 141 142 |
|
mcid
instance-attribute
Marked content section ID.
name
instance-attribute
Tag name.
props
instance-attribute
Marked content property dictionary (without MCID).
Text
Bases: TypedDict
Source code in playa/data/content.py
35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
|
bbox
instance-attribute
Bounding rectangle for all glyphs in text in default user space.
chars
instance-attribute
Unicode string representation of text.
ctm
instance-attribute
Coordinate transformation matrix, default if not present is the
identity matrix [1 0 0 1 0 0]
.
gstate
instance-attribute
Graphic state.
mcstack
instance-attribute
Stack of enclosing marked content sections.
textstate
instance-attribute
Text state.
TextState
Bases: TypedDict
Source code in playa/data/content.py
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 |
|
charspace
instance-attribute
Character spacing in unscaled text space units, default if not present is 0.
font
instance-attribute
Descriptor of current font.
fontsize
instance-attribute
Font size in unscaled text space units (not in points, can be
scaled using line_matrix
to obtain user space units), default if
not present is 1.0.
glyph_offset
instance-attribute
Offset of text object in relation to current line, in default text space units, default if not present is (0, 0).
leading
instance-attribute
Leading in unscaled text space units, default if not present is 0.
line_matrix
instance-attribute
Coordinate transformation matrix for start of current line.
render_mode
instance-attribute
Text rendering mode (PDF 1.7 Table 106), default if not present is 0.
rise
instance-attribute
Text rise (for super and subscript) in unscaled text space units, default if not present is 0.
scaling
instance-attribute
Horizontal scaling factor multiplied by 100, default if not present is 100.
wordspace
instance-attribute
Word spacing in unscaled text space units, default if not present is 0.
XObject
Bases: TypedDict
Source code in playa/data/metadata.py
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
|
filters
instance-attribute
List of filters.
genno
instance-attribute
Generation number.
length
instance-attribute
Length of raw stream data.
params
instance-attribute
Filter parameters.
resources
instance-attribute
Resources specific to this XObject, if any.
subtype
instance-attribute
Type of XObject.
xobject_id
instance-attribute
Indirect object ID.