Search code examples
phpdompdfcodeigniter-4

DOMPdf not generating PDF on production server, only showing code


I have a working DOMPdf generation from HTML on a local server. I uploaded it to my production server and from my first tests it seemed to be working. Now a couple of days ago I started receiving reports that the PDF generation only shows symbols and letters. So, I checked and did some testing and apparently the issue is somewhere between the DOMPdf library, PHP fpm and CodeIgniter.

The production server uses:

  • Ubuntu 20.04.2 LTS
  • Apache 2.4.41
  • PHP 7.4.3 (Apache uses FPM)

The site uses:

  • CodeIgniter 4.1.1
  • DomPDF 1.0.2

So, after playing around a little bit I noticed that if I used the PDF generation code outside of CodeIgniter it works normally, but if I try to use it inside the CodeIgniter instance, I get this:

enter image description here

My theory is that this is basically a header issue, but I am not sure what's causing it.


Solution

  • At the end I checked it with the CodeIgniter forum guys and I got two possible solutions. Evidently it was a header/buffer problem.

    So, there are two options to fix this:

    Option 1 - Removing the specific header for Content-Type

    //in controller method
    $this->response->removeHeader('Content-Type'); 
    

    Option 2 - Adding an exit() command at the end of the code

    $dompdf->stream("test.pdf",array('Attachment'=>0)); 
    exit();