Search code examples
phpwordpressdompdf

Wordpress - Use Dompdf in functions file


I am trying to do some PDF generation on my Wordpress site, I have an AJAX call that calls a function in my functions.php file but I currently can't even get the Dompdf library to load.

The location of my dompdf folder is wp-content/mytheme/inc/dompdf, here's my function:

function generate_pdf() {
  require_once(get_template_directory_uri().'/inc/dompdf/autoload.inc.php');
}
add_action('wp_ajax_generatepdf', 'generate_pdf');

And my AJAX call:

$.ajax({
    type: 'POST',
    url: myobject.ajaxurl + '?action=generatepdf',
    data: pdfHTML
}).done(function(data) {
    console.log(data);
});

When I look in the network tab at the admin-ajax.php call, the response just shows this:

There has been a critical error on your website.

Learn more about debugging in WordPress.

If I comment out the require_once line and instead echo out the require path, it is displaying the correct path to autoload.inc.php in the response. I have WP_DEBUG enabled, but that generic error is all that I get in the response. Any ideas on what the issue might be or even just how I can get a more specific debugging response?


Solution

  • You are using get_template_directory_uri which is an absolute web-based path. Instead, you want to use get_template_directory() which will get you the file-system based path.

    Also, if you are in a child theme, you might instead want get_stylesheet_directory