Skip to main content

AssemblyLine.sign

Image​

ImageDraw​

ImageFont​

os​

Optional​

List​

Union​

log​

find_font_file_by_name​

def find_font_file_by_name(font_name: str,
search_dirs: List[str]) -> Optional[str]

Recursively search for a font file by name in the specified directories.

This function appends '.ttf' to the font name if it is not already present and searches through each directory in search_dirs recursively for a file that matches the font name (case-insensitive).

Arguments​

  • font_name str - The name of the font to search for (without path).
  • search_dirs List[str] - A list of directories to search.

Returns​

  • Optional[str] - The full path to the font file if found; otherwise, None.

Example​

In an interview code block:

code: |
from docassemble.AssemblyLine.sign import find_font_file_by_name
signature_font_path = find_font_file_by_name("DejaVuSans", ["/usr/share/fonts/truetype"])

get_font​

def get_font(
font_name: Optional[str] = None,
font_size: int = 48
) -> Union[ImageFont.ImageFont, ImageFont.FreeTypeFont]

Loads a font by name from candidate directories and returns an ImageFont instance.

If font_name is provided, it may be either a full path to a font file or a font name (without a path). In the latter case, the function will search for the font in /var/www/.fonts and /usr/share/fonts/truetype/, automatically appending '.ttf' if necessary. If no font is found, the function falls back to the default Pillow font.

Arguments​

  • font_name Optional[str] - The desired font's full path or name. Defaults to None.
  • font_size int - The size of the font to be used. Defaults to 48.

Returns​

  • ImageFont.ImageFont - The loaded font instance.

Example​

In an interview code block:

code: |
from docassemble.AssemblyLine.sign import get_font
signature_font = get_font(font_name="DejaVuSans", font_size=48)

create_signature​

def create_signature(name: str,
output_file: str,
signature_prefix: str = "/s/",
font_name: Optional[str] = None,
font_size: int = 48) -> None

Creates an image file that simulates a signature.

The signature image is generated by combining the provided signature_prefix (for example, "/s/" or "s/") with the person's name. The text size is measured to create an image with appropriate dimensions, and then the text is drawn onto a white background. The final image is saved to the specified output file path.

Arguments​

  • name str - The person's name to be signed.
  • output_file str - The full file path where the image will be written.
  • signature_prefix str - The signature prefix to use (e.g., "/s/" or "s/"). Defaults to "/s/".
  • font_name Optional[str] - The font's full path or name (without path). Defaults to None.
  • font_size int - The size of the font to be used. Defaults to 48.

Example​

After the user chooses to use a typed signature, create its DAFile in an interview code block:

code: |
from docassemble.AssemblyLine.sign import create_signature
users[0].signature = DAFile("users[0].signature", filename="signature.png")
users[0].signature.initialize()
create_signature(users[0].name_full(), users[0].signature.path())
users[0].signature.commit()