• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Issue when combining Apache2, PHP 7.4 FPM and DomPDF

#1
Bug 
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.
Reply

#2
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'); 
Reply

#3
(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(); 
Reply

#4
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.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.