pyhanko.pdf_utils.content module

class pyhanko.pdf_utils.content.ResourceType(value)

Bases: enum.Enum

Enum listing resources that can be used as keys in a resource dictionary.

See ISO 32000-1, § 7.8.3 Table 34.

EXT_G_STATE = '/ExtGState'

External graphics state specifications. See ISO 32000-1, § 8.4.5.

COLOR_SPACE = '/ColorSpace'

Colour space definitions. See ISO 32000-1, § 8.6.

PATTERN = '/Pattern'

Pattern definitions. See ISO 32000-1, § 8.7.

SHADING = '/Shading'

Shading definitions. See ISO 32000-1, §

XOBJECT = '/XObject'

External object definitions (images and form XObjects). See ISO 32000-1, § 8.8.

FONT = '/Font'

Font specifications. See ISO 32000-1, § 9.

PROPERTIES = '/Properties'

Marked content properties. See ISO 32000-1, § 14.6.2.

exception pyhanko.pdf_utils.content.ResourceManagementError

Bases: ValueError

Used to signal problems with resource dictionaries.

class pyhanko.pdf_utils.content.PdfResources

Bases: object

Representation of a PDF resource dictionary.

This class implements __getitem__() with ResourceType keys for dynamic access to its attributes. To merge two instances of PdfResources into one another, the class overrides __iadd__(), so you can write.

res1 += res2

Note: Merging two resource dictionaries with conflicting resource names will produce a ResourceManagementError.

Note: This class is currently only used for new resource dictionaries.


Render this instance of PdfResources to an actual resource dictionary.

class pyhanko.pdf_utils.content.PdfContent(resources: Optional[pyhanko.pdf_utils.content.PdfResources] = None, box: Optional[pyhanko.pdf_utils.layout.BoxConstraints] = None, writer=None)

Bases: object

Abstract representation of part of a PDF content stream.


Whether PdfContent instances can be reused or not is left up to the subclasses.

writer = None

The __init__() method comes with an optional writer parameter that can be used to let subclasses register external resources with the writer by themselves.

It can also be set after the fact by calling set_writer().

set_resource(category: pyhanko.pdf_utils.content.ResourceType, name: pyhanko.pdf_utils.generic.NameObject, value: pyhanko.pdf_utils.generic.PdfObject)

Set a value in the resource dictionary associated with this content fragment.

  • category – The resource category to which the resource belongs.

  • name – The resource’s (internal) name.

  • value – The resource’s value.

import_resources(resources: pyhanko.pdf_utils.content.PdfResources)

Import resources from another resource dictionary.


resources – An instance of PdfResources.


ResourceManagementError – Raised when there is a resource name conflict.

property resources

The PdfResources instance associated with this content fragment.

render() → bytes

Compile the content to graphics operators.


Render the object to a form XObject to be referenced by another content stream. See ISO 32000-1, § 8.8.

Note: Even if writer is set, the resulting form XObject will not be registered. This is left up to the caller.


A StreamObject instance representing the resulting form XObject.


Override the currently registered writer object.


writer – An instance of BasePdfFileWriter.

class pyhanko.pdf_utils.content.RawContent(data: bytes, resources: Optional[pyhanko.pdf_utils.content.PdfResources] = None, box: Optional[pyhanko.pdf_utils.layout.BoxConstraints] = None)

Bases: pyhanko.pdf_utils.content.PdfContent

Raw byte sequence to be used as PDF content.

render() → bytes

Compile the content to graphics operators.