def to_str(obj) -> bytes:
    """Transform a Python basic type into bytes."""
    if type(obj) == bool:
        if obj == True:
            return b'true'
        else:
            return b'false'    
    elif type(obj) == int:
        return str(obj).encode('ascii')
    elif type(obj) == float:
        return str(obj).encode('ascii')
    elif type(obj) == str:
        return obj.encode('ascii')
    elif type(obj) == complex:
        s = f'{int(obj.imag)} {int(obj.real)} R'
        return s.encode('ascii')
    elif type(obj) == bytes:
        return obj
    else:
        return obj.encode('ascii')
Transform a Python basic type into bytes.
pdfsyntax/objects.py