DOMPDF with CODEIGNITER

DOMPDF with CODEIGNITER

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Resolving Class 'DOMPDF' Not Found Errors in CodeIgniter

Getting DOMPDF to work inside a CodeIgniter application often trips developers over namespace issues, specifically when using older releases like version 0.7.0. The error message Fatal error: Class 'DOMPDF' not found is the classic symptom of an outdated autoloading strategy clashing with modern PHP standards or incorrect class name references.

When you download DOMPDF from the GitHub releases page, you are pulling a version that predates full namespace adoption in many third-party libraries. In this older release, the main controller class is named DOMPDF, but the autoloader expects specific namespaces to resolve correctly. Simply dropping the autoload.inc.php file into your third_party folder is not enough if your controller code explicitly references the wrong class name or fails to initialize the library properly.

The root cause of the error in your scenario lies in how CodeIgniter loads classes versus how DOMPDF initializes itself. When you instantiate a new object using new DOMPDF(), PHP looks for a class definition matching that exact name. In older versions, this class was often defined within a specific namespace, or the file structure required the autoloader to be invoked before any other logic ran. If the autoloader fails to map the global DOMPDF constant to the actual file path because of namespace collisions or missing configuration, PHP throws the fatal error you are seeing on line 2495.

To fix this immediately without upgrading the library (which might break legacy dependencies), you need to ensure the autoloader is active and that the instantiation matches the library's expectations. The autoload.inc.php file provided in the release contains a DOMPDF_autoload() function or an Autoload class that must be called first.

Update your controller method to explicitly initialize the loader before creating the instance:

public function generate_pdf() {
    $this->load->helper('file');
    
    // Ensure the autoloader is active before any other logic
    require_once(APPPATH . 'third_party/dompdf/autoload.inc.php');

    // In older versions, you might need to explicitly call the loader or use the correct class alias
    // However, for version 0.7.0, the standard instantiation usually requires checking the namespace
    // If 'DOMPDF' is not found, try referencing the specific class name defined in the library
    
    // Correct approach for older DOMPDF versions:
    $dompdf = new \DOMPDF\DOMPDF(); 
    
    $html_content = '<h1>HELLO WORLD</h1>';
    
    $dompdf->loadHtml($html_content);
    $dompdf->setPaper('A4', 'Landscape');
    $dompdf->render();

    $dompdf->stream("document.pdf", array("Attachment" => false));
}

The critical change here is using the fully qualified class name \DOMPDF\DOMPDF() if the global DOMPDF constant isn't automatically defined by the autoloader. In CodeIgniter, you might also encounter issues if the third_party directory isn't registered in the config file or if the path constants like APPPATH resolve to a location that doesn't match where you extracted the library files.

Another common pitfall with DOMPDF 0.7.0 is the handling of the stream() method. In some configurations, calling render() without flushing output or ensuring no headers are sent beforehand can cause silent failures, though this usually manifests as blank PDFs rather than fatal errors. Ensure your controller action has clean headers before rendering:

header('Content-Type: application/pdf');

If you find that upgrading to a newer version of DOMPDF is an option for your project, consider moving away from the monolithic autoload.inc.php approach in favor of Composer. Composer manages dependencies and namespaces reliably across PHP versions. You would simply run composer require dompdf/dompdf and use the autoloader generated by Composer, which handles the namespace resolution automatically:

use Dompdf\Dompdf;

$dompdf = new Dompdf();

This modernizes your dependency management while eliminating the "class not found" headaches associated with legacy files. However, if you must stay on 0.7.0 for compatibility reasons, always verify that the class instantiation matches the namespace structure defined in the source code of that specific release.

Our network