Error in pdf generating plugin using FPDF

I’m trying to create a plugin using FPDF to create an options page to print custom post types.

Here is my code:

<?php
/*
* Plugin Name: RT FPDF Tutorial
* Description: A plugin created to demonstrate how to build a PDF document from WordPress posts.
* Version: 1.0
*/

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

include( 'pdf-helper-functions.php');
$pdf = new PDF_HTML();

if( isset($_POST['generate_posts_pdf'])){
    output_pdf();
}

add_action( 'admin_menu', 'rt_fpdf_create_admin_menu' );
function rt_fpdf_create_admin_menu() {
    $hook = add_submenu_page(
        'tools.php',
        'PDF Generator',
        'PDF Generator',
        'manage_options',
        'rt-fdpf-tutorial',
        'rt_fpdf_create_admin_page'
    );
}
function rt_fpdf_create_admin_page() {
echo '<div class="wrap">';
echo '  <form method="post" id="as-fdpf-form"><button class="button button-primary" type="submit" name="generate_posts_pdf" value="generate">Generate the Directory</button></form>';
echo '</div>';
}
function output_pdf() {
    
    global $pdf;
    $title_line_height = 10;
    $content_line_height = 8;

    $args = array(
        'post_type' => 'cc_family',
    );

    // The Query
    //$the_query = new WP_Query( array ('post_type' => 'cc_family') );    

        $pdf->AddPage();
        $pdf->SetFont( 'Arial', '', 42 );
        $pdf->Write(20, 'No families to make a directory yet');

        
    $pdf->Output('D','new-directory.pdf');
    exit;
}

As the code is right now it works ($the_query is commented out.) A pdf is created with the content "no families…".

If I un-comment the line above to start adding the query, I get a critical error on my site. If I add an entire generic query I get an error as well.

I’ve tried with get_posts() and this works, but it doesn’t seem to allow me to pull custom fields. Is there something special to adding a new query I’ve missed?

Update to add the error:

> [13-Sep-2021 15:30:51 UTC] PHP Notice:  ob_end_flush(): failed to send
> buffer of zlib output compression (0) in
> /home/whn/public_html/wp-includes/functions.php on line 5107
> [13-Sep-2021 15:30:53 UTC] PHP Fatal error:  Uncaught Error: Call to
> undefined function is_user_logged_in() in
> /home/whn/public_html/wp-includes/class-wp-query.php:2544 Stack
> trace:
> #0 /home/whn/public_html/wp-includes/class-wp-query.php(3465): WP_Query->get_posts()
> #1 /home/whn/public_html/wp-includes/class-wp-query.php(3576): WP_Query->query(Array)
> #2 /home/whn/public_html/wp-content/plugins/pdfer/rt_pdf.php(48): WP_Query->__construct(Array)
> #3 /home/whn/public_html/wp-content/plugins/pdfer/rt_pdf.php(18): output_pdf()
> #4 /home/whn/public_html/wp-settings.php(409): include_once('/home/wh3n/publ...')
> #5 /home/whn/public_html/wp-config.php(91): require_once('/home/wh3n/publ...')
> #6 /home/whn/public_html/wp-load.php(50): require_once('/home/wh3n/publ...')
> #7 /home/whn/public_html/wp-admin/admin.php(34): require_once('/home/wh3n/publ...')
> #8 /home/whn/public_html/wp-admin/tools.php(40): require_once('/home/wh3n/publ...')
> #9 {main}   thrown in /home/whn/public_html/wp-includes/class-wp-query.php on line 2544
> [13-Sep-2021 15:30:53 UTC] PHP Notice:  is_embed was called
> <strong>incorrectly</strong>. Conditional query tags do not work
> before the query is run. Before then, they always return false. Please
> see <a
> href="https://wordpress.org/support/article/debugging-in-wordpress/">Debugging
> in WordPress</a> for more information. (This message was added in
> version 3.1.0.) in /home/whn/public_html/wp-includes/functions.php on
> line 5663 [13-Sep-2021 15:30:53 UTC] PHP Notice:  is_search was called
> <strong>incorrectly</strong>. Conditional query tags do not work
> before the query is run. Before then, they always return false. Please
> see <a
> href="https://wordpress.org/support/article/debugging-in-wordpress/">Debugging
> in WordPress</a> for more information. (This message was added in
> version 3.1.0.) in /home/whn/public_html/wp-includes/functions.php on
> line 5663 [13-Sep-2021 15:30:54 UTC] PHP Notice:  ob_end_flush():
> failed to send buffer of zlib output compression (0) in
> /home/whn/public_html/wp-includes/functions.php on line 5107

$299 Affordable Web Design WordPress

This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

Error in pdf generating plugin using FPDF

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.