how to create endpoint for downloading pdf files?

I would like the logged-in user to be able to download the PDF file, but not to see the real path to the file. Additionally, in this PDF file, I enter information about the user name and date of download.

I created a new endpoint "download" to make the download address look like this: my-website.com/post-name/download/attached-id

function my_custom_endpoint() {     add_rewrite_endpoint( 'download', EP_PERMALINK | EP_PAGES ); }   add_action( 'init', 'my_custom_endpoint' ); 

but I don’t know what the hook is to not load the page after calling this endpoint but to call my function and return the PDF file to the user.