Error in pdf generating plugin using FPDF

The problem is that your output_pdf() function is being called too early. You have the function running in the root of the main plugin file, meaning that it runs as soon as the plugin is loaded, and before before WordPress has finished loading.

You need to at least hook it to run later, such as on the admin_init hook:

function rt_fpdf_generate_pdf() {
    include( 'pdf-helper-functions.php');
    $pdf = new PDF_HTML();
    
    if( isset($_POST['generate_posts_pdf'])){
        output_pdf();
    }

}
add_action( 'admin_init', 'rt_fpdf_generate_pdf' );

Better yet, you could use the admin_post_ hook: https://developer.wordpress.org/reference/hooks/admin_post_action/

Also note some other issues with your code. They are hopefully just a result of the code being incomplete, but I need to note them in case anybody comes along thinking they can copy the code:

  • You need to use namespaces, or at least prefix your functions. output_pdf() is too generic, and could result in conflicts.
  • There’s no use of nonces or permission checks to ensure only allowed users can generate this PDF. As written anybody could submit a post requests and bring down your site by forcing it to generate thousands of PDFs.