Drupal 9: implementing custom functionality in PHP

I would really appreciate it if somebody would give me some hints about implementing/inserting custom functionality into a drupal 9 page with PHP code.
I am trying to write a PHP function that is generating a file based on some specific properties of Drupal users on a button click, and I do not know where to start.
I am really confused. Is there an easy way to insert PHP code into a Drupal basic page?
I am searching for solutions, but what I always find is custom modules where none of the given examples is working