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 the requested font is missing or cannot be loaded, the function tries common scalable fonts at font_size, then Pillow's default font at that size (when supported by the installed Pillow version).

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,
resolution_multiplier: int = 4) -> 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. By default, the font and padding are rendered at four times the base resolution for clearer DOCX and PDF output. Matching PNG DPI metadata preserves the image's natural physical size. 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 base font size before multiplying the resolution. Defaults to 48.
  • resolution_multiplier int - Multiplier for font size, padding, and PNG DPI. Defaults to 4. Use 1 to render at the base resolution.

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()