Finalises the document and returns its encoded bytes.
After this call the driver releases any underlying native resources and must not be used again.