CodeIgniter Forums
Issue when combining Apache2, PHP 7.4 FPM and DomPDF - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Issue when combining Apache2, PHP 7.4 FPM and DomPDF (/showthread.php?tid=78854)



Issue when combining Apache2, PHP 7.4 FPM and DomPDF - mi_ov - 03-18-2021

Hello, the problem is a very specific case, although I am not sure what's causing it.

I have a local server environment which uses:

  • Windows 10 x64
  • Apache 2.4.25 x64
  • PHP 7.4.11 
  • Dompdf 1.0.2
I use the dompdf library (tried both via composer and as a required library) to generate PDFs with user data. A basic example of code would be something like this:
PHP Code:
public function test()
{

    
$dompdf = new \Dompdf\Dompdf();
    
$dompdf->loadHtml('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html;charset=utf-8"><title>Solicitud en PDF</title></head><body><h1>Hola</h1></body></html>');

    
$dompdf->setPaper('Letter''portrait');

    
// Render the HTML as PDF
    
$dompdf->render();

    
// Output the generated PDF to Browser
    
$dompdf->stream("test.pdf",array('Attachment'=>0));



Now on my local server, using both via composer and as a required library, this process works fine and I get this:

[Image: testpdf.png]

On the other hand, my production environment has:
  • Ubuntu 20.04 LTS
  • Apache 2.4.41 - Apache uses FPM and http/2
  • PHP 7.4.3
  • Dompdf 1.0.2



I thought this part was working, but then I started getting reports that users are not getting PDFs but scrambled letters and symbols. So I tried to see what's happening and I got this. 

[Image: testpdferror.png]
This literally comes out as the PDF code.

If I copy this function outside my CodeIgniter instance on the same server, just using a regular php file and requiring the library, there's no problem. So, for some reason I get a problem when those three are combined: 
  • CodeIgniter 4.1.1
  • PHP 7.4 FPM
  • DomPDF 1.0.2
As I said before if I remove CodeIgniter from this combination on the production server it works. 

I also tried with PHP 8, to check if it was a PHP issue, same result.

My theory is that it might be some header issue, because the file is output as text and not PDF.


RE: Issue when combining Apache2, PHP 7.4 FPM and DomPDF - iRedds - 03-18-2021

Theory!

Possibly output buffering is enabled in php.
For this reason, the header set by the library is replaced by the header from the Response class.

Look in the php config for the output_buffering option. And if it's on, turn it off.

Or you can try another solution. Remove header!
PHP Code:
//in controller method
$this->response->removeHeader('Content-Type'); 



RE: Issue when combining Apache2, PHP 7.4 FPM and DomPDF - nfaiz - 03-19-2021

(03-18-2021, 05:25 PM)mi_ov Wrote: Hello, the problem is a very specific case, although I am not sure what's causing it.

I have a local server environment which uses:

  • Windows 10 x64
  • Apache 2.4.25 x64
  • PHP 7.4.11 
  • Dompdf 1.0.2
I use the dompdf library (tried both via composer and as a required library) to generate PDFs with user data. A basic example of code would be something like this:
PHP Code:
public function test()
{

 
$dompdf = new \Dompdf\Dompdf();
 
$dompdf->loadHtml('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html;charset=utf-8"><title>Solicitud en PDF</title></head><body><h1>Hola</h1></body></html>');

 
$dompdf->setPaper('Letter''portrait');

 
// Render the HTML as PDF
 
$dompdf->render();

 
// Output the generated PDF to Browser
 
$dompdf->stream("test.pdf",array('Attachment'=>0));



Now on my local server, using both via composer and as a required library, this process works fine and I get this:

[Image: testpdf.png]

On the other hand, my production environment has:
  • Ubuntu 20.04 LTS
  • Apache 2.4.41 - Apache uses FPM and http/2
  • PHP 7.4.3
  • Dompdf 1.0.2



I thought this part was working, but then I started getting reports that users are not getting PDFs but scrambled letters and symbols. So I tried to see what's happening and I got this. 

[Image: testpdferror.png]
This literally comes out as the PDF code.

If I copy this function outside my CodeIgniter instance on the same server, just using a regular php file and requiring the library, there's no problem. So, for some reason I get a problem when those three are combined: 
  • CodeIgniter 4.1.1
  • PHP 7.4 FPM
  • DomPDF 1.0.2
As I said before if I remove CodeIgniter from this combination on the production server it works. 

I also tried with PHP 8, to check if it was a PHP issue, same result.

My theory is that it might be some header issue, because the file is output as text and not PDF.

Put exit(); after

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

Like this

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



RE: Issue when combining Apache2, PHP 7.4 FPM and DomPDF - mi_ov - 03-19-2021

Wow, thanks for the quick replies guys.

I tried both options and both options worked. Which one is more recommended?

The only thing that didn't actually work was changing the buffer setting in php.ini it didn't change anything.

I find the exit() one easier, but in the end the result works in both versions.


RE: Issue when combining Apache2, PHP 7.4 FPM and DomPDF - owino - 12-04-2022

(03-19-2021, 03:17 PM)mi_ov Wrote: Wow, thanks for the quick replies guys.

I tried both options and both options worked. Which one is more recommended?

The only thing that didn't actually work was changing the buffer setting in php.ini it didn't change anything.

I find the exit() one easier, but in the end the result works in both versions.

I am happy that it has worked for you. I too would like to use this library. In which CI4 folder do we install the domPDF?