The problem is that Base64 adds the newline in order to be compatible with older systems that have a maximum line width.
It won't happen only at the end of the encoding but rather every 76 characters resulting from the encoding. For many old programs that couldn't handle reading long lines, introducing a newline character was introduced every certain number of characters. In computer science argot this is called text wrapping. I guess that the number 76 of characters comes from the good practice of having code lines of at most 80 characters and having 2 per side as margin (although not sure why exactly two). This choose of 76 characters (or columns) comes from the standards in RFC2045 (page 19 paragraph 5) and is also a standard in the Linux command base64.
More links :
https://mathematica.stackexchange.com/questions/100509/why-base64-ends-with-a-newline-n
https://superuser.com/questions/1225134/why-does-the-base64-of-a-string-contain-n