The code snippet provided by OP looks unnecessarily complicated for getting document embeddings.
To get document embeddings, I'd start with using the last hidden state of the [CLS] token. Specifically, OP's text_to_embedding() routine can be replaced with less than 5 lines of code. See this post for how.