pyhanko.stamp module
Utilities for stamping PDF files.
Here ‘stamping’ loosely refers to adding small overlays (QR codes, text boxes, etc.) on top of already existing content in PDF files.
The code in this module is also used by the sign
module to render
signature appearances.
- class pyhanko.stamp.AnnotAppearances(normal: IndirectObject, rollover: Optional[IndirectObject] = None, down: Optional[IndirectObject] = None)
Bases:
object
Convenience abstraction to set up an appearance dictionary for a PDF annotation.
Annotations can have three appearance streams, which can be roughly characterised as follows:
normal: the only required one, and the default one;
rollover: used when mousing over the annotation;
down: used when clicking the annotation.
These are given as references to form XObjects.
Note
This class only covers the simple case of an appearance dictionary for an annotation with only one appearance state.
See § 12.5.5 in ISO 32000-1 for further information.
- as_pdf_object() DictionaryObject
Convert the
AnnotationAppearances
instance to a PDF dictionary.- Returns
A
DictionaryObject
that can be plugged into the/AP
entry of an annotation dictionary.
- class pyhanko.stamp.BaseStampStyle(border_width: int = 3, background: ~typing.Optional[~pyhanko.pdf_utils.content.PdfContent] = None, background_layout: ~pyhanko.pdf_utils.layout.SimpleBoxLayoutRule = SimpleBoxLayoutRule(x_align=<AxisAlignment.ALIGN_MID: 2>, y_align=<AxisAlignment.ALIGN_MID: 2>, margins=Margins(left=5, right=5, top=5, bottom=5), inner_content_scaling=<InnerScaling.SHRINK_TO_FIT: 4>), background_opacity: float = 0.6)
Bases:
ConfigurableMixin
Base class for stamp styles.
- border_width: int = 3
Border width in user units (for the stamp, not the text box).
- background: PdfContent = None
PdfContent
instance that will be used to render the stamp’s background.
- background_layout: SimpleBoxLayoutRule = SimpleBoxLayoutRule(x_align=<AxisAlignment.ALIGN_MID: 2>, y_align=<AxisAlignment.ALIGN_MID: 2>, margins=Margins(left=5, right=5, top=5, bottom=5), inner_content_scaling=<InnerScaling.SHRINK_TO_FIT: 4>)
Layout rule to render the background inside the stamp’s bounding box. Only used if the background has a fully specified
PdfContent.box
.Otherwise, the renderer will position the cursor at
(left_margin, bottom_margin)
and render the content as-is.
- background_opacity: float = 0.6
Opacity value to render the background at. This should be a floating-point number between 0 and 1.
- classmethod process_entries(config_dict)
This implementation of
process_entries()
processes thebackground
configuration value. This can either be a path to an image file, in which case it will be turned into an instance ofPdfImage
, or the special value__stamp__
, which is an alias forSTAMP_ART_CONTENT
.
- create_stamp(writer: BasePdfFileWriter, box: BoxConstraints, text_params: dict) BaseStamp
- class pyhanko.stamp.TextStampStyle(border_width: int = 3, background: ~typing.Optional[~pyhanko.pdf_utils.content.PdfContent] = None, background_layout: ~pyhanko.pdf_utils.layout.SimpleBoxLayoutRule = SimpleBoxLayoutRule(x_align=<AxisAlignment.ALIGN_MID: 2>, y_align=<AxisAlignment.ALIGN_MID: 2>, margins=Margins(left=5, right=5, top=5, bottom=5), inner_content_scaling=<InnerScaling.SHRINK_TO_FIT: 4>), background_opacity: float = 0.6, text_box_style: ~pyhanko.pdf_utils.text.TextBoxStyle = TextBoxStyle(font=<pyhanko.pdf_utils.font.basic.SimpleFontEngineFactory object>, font_size=10, leading=None, border_width=0, box_layout_rule=None, vertical_text=False), inner_content_layout: ~typing.Optional[~pyhanko.pdf_utils.layout.SimpleBoxLayoutRule] = None, stamp_text: str = '%(ts)s', timestamp_format: str = '%Y-%m-%d %H:%M:%S %Z')
Bases:
BaseStampStyle
Style for text-based stamps.
Roughly speaking, this stamp type renders some predefined (but parametrised) piece of text inside a text box, and possibly applies a background to it.
- text_box_style: TextBoxStyle = TextBoxStyle(font=<pyhanko.pdf_utils.font.basic.SimpleFontEngineFactory object>, font_size=10, leading=None, border_width=0, box_layout_rule=None, vertical_text=False)
The text box style for the internal text box used.
- inner_content_layout: SimpleBoxLayoutRule = None
Rule determining the position and alignment of the inner text box within the stamp.
Warning
This only affects the position of the box, not the alignment of the text within.
- stamp_text: str = '%(ts)s'
Text template for the stamp. The template can contain an interpolation parameter
ts
that will be replaced by the stamping time.Additional parameters may be added if necessary. Values for these must be passed to the
__init__()
method of theTextStamp
class in thetext_params
argument.
- timestamp_format: str = '%Y-%m-%d %H:%M:%S %Z'
Datetime format used to render the timestamp.
- create_stamp(writer: BasePdfFileWriter, box: BoxConstraints, text_params: dict) TextStamp
- class pyhanko.stamp.QRStampStyle(border_width: int = 3, background: ~typing.Optional[~pyhanko.pdf_utils.content.PdfContent] = None, background_layout: ~pyhanko.pdf_utils.layout.SimpleBoxLayoutRule = SimpleBoxLayoutRule(x_align=<AxisAlignment.ALIGN_MID: 2>, y_align=<AxisAlignment.ALIGN_MID: 2>, margins=Margins(left=5, right=5, top=5, bottom=5), inner_content_scaling=<InnerScaling.SHRINK_TO_FIT: 4>), background_opacity: float = 0.6, text_box_style: ~pyhanko.pdf_utils.text.TextBoxStyle = TextBoxStyle(font=<pyhanko.pdf_utils.font.basic.SimpleFontEngineFactory object>, font_size=10, leading=None, border_width=0, box_layout_rule=None, vertical_text=False), inner_content_layout: ~typing.Optional[~pyhanko.pdf_utils.layout.SimpleBoxLayoutRule] = None, stamp_text: str = 'Digital version available at\nthis url: %(url)s\nTimestamp: %(ts)s', timestamp_format: str = '%Y-%m-%d %H:%M:%S %Z', innsep: int = 3, qr_inner_size: ~typing.Optional[int] = None, qr_position: ~pyhanko.stamp.QRPosition = QRPosition.LEFT_OF_TEXT, qr_inner_content: ~typing.Optional[~pyhanko.pdf_utils.content.PdfContent] = None)
Bases:
TextStampStyle
Style for text-based stamps together with a QR code.
This is exactly the same as a text stamp, except that the text box is rendered with a QR code to the left of it.
- innsep: int = 3
Inner separation inside the stamp.
- stamp_text: str = 'Digital version available at\nthis url: %(url)s\nTimestamp: %(ts)s'
Text template for the stamp. The description of
TextStampStyle.stamp_text
still applies, but an additional default interpolation parameterurl
is available. This parameter will be replaced with the URL that the QR code points to.
- qr_inner_size: Optional[int] = None
Size of the QR code in the inner layout. By default, this is in user units, but if the stamp has a fully defined bounding box, it may be rescaled depending on
inner_content_layout
.If unspecified, a reasonable default will be used.
- qr_position: QRPosition = SimpleBoxLayoutRule(x_align=<AxisAlignment.ALIGN_MIN: 1>, y_align=<AxisAlignment.ALIGN_MID: 2>, margins=Margins(left=0, right=0, top=0, bottom=0), inner_content_scaling=<InnerScaling.SHRINK_TO_FIT: 4>)
Position of the QR code relative to the text box.
- qr_inner_content: Optional[PdfContent] = None
Inner graphics content to be included in the QR code (experimental).
- classmethod process_entries(config_dict)
This implementation of
process_entries()
processes thebackground
configuration value. This can either be a path to an image file, in which case it will be turned into an instance ofPdfImage
, or the special value__stamp__
, which is an alias forSTAMP_ART_CONTENT
.
- create_stamp(writer: BasePdfFileWriter, box: BoxConstraints, text_params: dict) QRStamp
- class pyhanko.stamp.StaticStampStyle(border_width: int = 3, background: ~typing.Optional[~pyhanko.pdf_utils.content.PdfContent] = None, background_layout: ~pyhanko.pdf_utils.layout.SimpleBoxLayoutRule = SimpleBoxLayoutRule(x_align=<AxisAlignment.ALIGN_MID: 2>, y_align=<AxisAlignment.ALIGN_MID: 2>, margins=Margins(left=5, right=5, top=5, bottom=5), inner_content_scaling=<InnerScaling.SHRINK_TO_FIT: 4>), background_opacity: float = 1.0)
Bases:
BaseStampStyle
Stamp style that does not include any custom parts; it only renders the background.
- background_opacity: float = 1.0
Opacity value to render the background at. This should be a floating-point number between 0 and 1.
- classmethod from_pdf_file(file_name, page_ix=0, **kwargs) StaticStampStyle
Create a
StaticStampStyle
from a page from an external PDF document. This is a convenience wrapper aroundImportedPdfContent
.The remaining keyword arguments are passed to
StaticStampStyle
’s init method.- Parameters
file_name – File name of the external PDF document.
page_ix – Page index to import. The default is
0
, i.e. the first page.
- create_stamp(writer: BasePdfFileWriter, box: BoxConstraints, text_params: dict) StaticContentStamp
- class pyhanko.stamp.QRPosition(value)
Bases:
Enum
QR positioning constants, with the corresponding default content layout rule.
- LEFT_OF_TEXT = SimpleBoxLayoutRule(x_align=<AxisAlignment.ALIGN_MIN: 1>, y_align=<AxisAlignment.ALIGN_MID: 2>, margins=Margins(left=0, right=0, top=0, bottom=0), inner_content_scaling=<InnerScaling.SHRINK_TO_FIT: 4>)
- RIGHT_OF_TEXT = SimpleBoxLayoutRule(x_align=<AxisAlignment.ALIGN_MAX: 3>, y_align=<AxisAlignment.ALIGN_MID: 2>, margins=Margins(left=0, right=0, top=0, bottom=0), inner_content_scaling=<InnerScaling.SHRINK_TO_FIT: 4>)
- ABOVE_TEXT = SimpleBoxLayoutRule(x_align=<AxisAlignment.ALIGN_MID: 2>, y_align=<AxisAlignment.ALIGN_MAX: 3>, margins=Margins(left=0, right=0, top=0, bottom=0), inner_content_scaling=<InnerScaling.SHRINK_TO_FIT: 4>)
- BELOW_TEXT = SimpleBoxLayoutRule(x_align=<AxisAlignment.ALIGN_MID: 2>, y_align=<AxisAlignment.ALIGN_MIN: 1>, margins=Margins(left=0, right=0, top=0, bottom=0), inner_content_scaling=<InnerScaling.SHRINK_TO_FIT: 4>)
- property horizontal_flow
- classmethod from_config(config_str) QRPosition
Convert from a configuration string.
- Parameters
config_str – A string: ‘left’, ‘right’, ‘top’, ‘bottom’
- Returns
An
QRPosition
value.- Raises
ConfigurationError – on unexpected string inputs.
- class pyhanko.stamp.BaseStamp(writer: BasePdfFileWriter, style, box: Optional[BoxConstraints] = None)
Bases:
PdfContent
- render()
Compile the content to graphics operators.
- register() IndirectObject
Register the stamp with the writer coupled to this instance, and cache the returned reference.
This works by calling
PdfContent.as_form_xobject()
.- Returns
An indirect reference to the form XObject containing the stamp.
- apply(dest_page: int, x: int, y: int)
Apply a stamp to a particular page in the PDF writer attached to this
BaseStamp
instance.- Parameters
dest_page – Index of the page to which the stamp is to be applied (starting at 0).
x – Horizontal position of the stamp’s lower left corner on the page.
y – Vertical position of the stamp’s lower left corner on the page.
- Returns
A reference to the affected page object, together with a
(width, height)
tuple describing the dimensions of the stamp.
- as_appearances() AnnotAppearances
Turn this stamp into an appearance dictionary for an annotation (or a form field widget), after rendering it. Only the normal appearance will be defined.
- Returns
An instance of
AnnotAppearances
.
- class pyhanko.stamp.TextStamp(writer: BasePdfFileWriter, style, text_params=None, box: Optional[BoxConstraints] = None)
Bases:
BaseStamp
Class that renders a text stamp as specified by an instance of
TextStampStyle
.- get_default_text_params()
Compute values for the default string interpolation parameters to be applied to the template string specified in the stamp style. This method does not take into account the
text_params
init parameter yet.- Returns
A dictionary containing the parameters and their values.
- class pyhanko.stamp.QRStamp(writer: BasePdfFileWriter, url: str, style: QRStampStyle, text_params=None, box: Optional[BoxConstraints] = None)
Bases:
TextStamp
- get_default_text_params()
Compute values for the default string interpolation parameters to be applied to the template string specified in the stamp style. This method does not take into account the
text_params
init parameter yet.- Returns
A dictionary containing the parameters and their values.
- apply(dest_page, x, y)
Apply a stamp to a particular page in the PDF writer attached to this
BaseStamp
instance.- Parameters
dest_page – Index of the page to which the stamp is to be applied (starting at 0).
x – Horizontal position of the stamp’s lower left corner on the page.
y – Vertical position of the stamp’s lower left corner on the page.
- Returns
A reference to the affected page object, together with a
(width, height)
tuple describing the dimensions of the stamp.
- class pyhanko.stamp.StaticContentStamp(writer: BasePdfFileWriter, style: StaticStampStyle, box: BoxConstraints)
Bases:
BaseStamp
Class representing stamps with static content.
- pyhanko.stamp.text_stamp_file(input_name: str, output_name: str, style: TextStampStyle, dest_page: int, x: int, y: int, text_params=None)
Add a text stamp to a file.
- Parameters
input_name – Path to the input file.
output_name – Path to the output file.
style – Text stamp style to use.
dest_page – Index of the page to which the stamp is to be applied (starting at 0).
x – Horizontal position of the stamp’s lower left corner on the page.
y – Vertical position of the stamp’s lower left corner on the page.
text_params – Additional parameters for text template interpolation.
- pyhanko.stamp.qr_stamp_file(input_name: str, output_name: str, style: QRStampStyle, dest_page: int, x: int, y: int, url: str, text_params=None)
Add a QR stamp to a file.
- Parameters
input_name – Path to the input file.
output_name – Path to the output file.
style – QR stamp style to use.
dest_page – Index of the page to which the stamp is to be applied (starting at 0).
x – Horizontal position of the stamp’s lower left corner on the page.
y – Vertical position of the stamp’s lower left corner on the page.
url – URL for the QR code to point to.
text_params – Additional parameters for text template interpolation.
- pyhanko.stamp.STAMP_ART_CONTENT = <pyhanko.pdf_utils.content.RawContent object>
Hardcoded stamp background that will render a stylised image of a stamp using PDF graphics operators (see below).