How to create a page template using custom plugin

If what you want is to actually generate a page template when the plugin is activated, you can use something like this:

<?php
/**
 * Plugin Name: Testtest
 * Description: Ignore
 * Version: 0.1
 * Author: windyjonas
 */
function myplugin_activate() {
    $buf = "<?php\n"
        . "/*\n"
        . " * Template Name: generated template\n"
        . " */\n"
        . "?>\n"
        . "Hello world\n";

    $handle = fopen( get_stylesheet_directory() . '/tpl-plugin.php', 'w' );
    fwrite( $handle, $buf );
    fclose( $handle );
}
register_activation_hook( __FILE__, 'myplugin_activate' );

This only works if the web server user has enough privileges to write to theme directory.

error code: 523