Skip to content

Commit

Permalink
issue #75: added new class for {?sign } tag
Browse files Browse the repository at this point in the history
  • Loading branch information
SibrenJacobs committed May 30, 2022
1 parent f63a67d commit 70fd8b4
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 0 deletions.
78 changes: 78 additions & 0 deletions cloudofficeprint/elements/pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -417,3 +417,81 @@ def as_dict(self) -> Dict:

return {self.name: result}


class PDFFormSignature(PDFFormElement):
"""
Class for a PDF form unsigned signature field element.
"""
def __init__(self,
name: str,
width: int = None,
height: int = None,
):
"""
Args:
name (str): The name for this element.
width (int): The width in px. Optional.
height (int): The height in px. Optional.
"""
super().__init__(name, width, height)

@property
def type(self) -> str:
return "signaturefieldunsigned"

@property
def as_dict(self) -> Dict:
return {self.name: super()._inner_dict}

@property
def available_tags(self) -> FrozenSet[str]:
return frozenset({"{?sign " + self.name + "}"})


class PDFFormSignatureSigned(PDFFormSignature):
"""
Class for a PDF form signed signature field element.
"""
def __init__(self,
name: str,
value: str,
password: str = None,
size: str = None,
background_image: str = None,
width: int = None,
height: int = None,
):
"""
Args:
name (str): The name for this element.
value (str): The signing certificate as a base64 string, URL, FTP location or a server path.
password (str): The password for if the certificate is encrypted. Optional.
size (str): The size must either be "sm" for small, "md" for medium or "lg" for large. Optional.
background_image (str): The background image as a base64 string, URL, FTP location or a server path. Optional.
width (int): The width in px. Optional.
height (int): The height in px. Optional.
"""
super().__init__(name, width, height)
self.value: str = value
self.password: str = password
self.size: str = size
self.background_image: str = background_image

@property
def type(self) -> str:
return "signaturefieldsigned"

@property
def as_dict(self) -> Dict:
result = super()._inner_dict

if self.value is not None:
result['value'] = self.value
if self.password is not None:
result['password'] = self.password
if self.size is not None:
result['size'] = self.size
if self.background_image is not None:
result['background_image'] = self.background_image

return {self.name: result}
44 changes: 44 additions & 0 deletions tests/test_pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,13 +290,57 @@ def test_pdf_form_radio_button():
assert radio_button.as_dict == radio_button_expected


def test_pdf_form_signature():
signature = cop.elements.PDFFormSignature(
name="text1",
width=150,
height=50,
)
signature_expected = {
"text1": {
"type": "signaturefieldunsigned",
"name": "text1",
"width": 150,
"height": 50
}
}
assert signature.as_dict == signature_expected


def test_pdf_form_signature_signed():
signature = cop.elements.PDFFormSignatureSigned(
name="text2",
value="base64 encoded certificate",
password="certificate password",
size="md",
background_image="base64 encoded image",
width=200,
height=50,
)
signature_expected = {
"text2": {
"type": "signaturefieldsigned",
"name": "text2",
"size": "md",
"value": "base64 encoded certificate",
"background_image": "base64 encoded image",
"password": "certificate password",
"width": 200,
"height": 50
}
}
assert signature.as_dict == signature_expected


def run():
test_cop_pdf_texts()
test_cop_pdf_images()
test_cop_pdf_forms()
test_pdf_form_text_box()
test_pdf_form_check_box()
test_pdf_form_radio_button()
test_pdf_form_signature()
test_pdf_form_signature_signed()


if __name__ == '__main__':
Expand Down

0 comments on commit 70fd8b4

Please sign in to comment.