This question already has an answer here:
How to fix “Headers already sent” error in PHP
The issue is you are
echoing the result of the save operation. If that functions returns nothing, it will still echo something (producing output). This is obviously happening prior to you sending the header information, so try removing the
echo statement and just
$this->dom->save(...) on line 165.