how to create endpoint for downloading pdf files?

A good action to hook in would be template_redirect. There you can query for your endpoint like this:

add_action('template_redirect', function() {
    global $wp_query;
    
    // Not your endpoint and not a page/post, we do nothing.
    if (!isset( $wp_query->query_vars['download']) || ! is_singular()) {
        return;
    }
    
    // Here goes your magic!
    […]
    
    // And probably a good idea to exit if you serve files.
    exit;
});

There’s also a great tutorial about endpoints on Make WordPress.