\AtEndDocument
Synopsis:
\AtEndDocument{code}
Save code and execute it near the end of the document. Specifically, it is executed when \end{document}
is executed, before the final page is finished and before any leftover floating environments are processed. If you want some of the code to be executed after these two processes then include a \clearpage
at the appropriate point in code.
You can issue this command more than once; the successive code lines will be executed in the order that you gave them.
© 2007–2018 Karl Berry
Public Domain Software
http://latexref.xyz/_005cAtEndDocument.html