Make sure that document conversions produce a UTF-8 string.

Closes #1393.