added fn to write doc to output stream