FPDF Fatal error: Allowed memory size of 33554432

2019-07-15 13:38发布

This is a often discussed issue but so far no solution seems to fit for my problem. I'm generating a pdf with $pdf = new FPDF(); . This works fine. But now I want to have a footer with the page number. After trying a lot of things I found out, that if you want to set a footer, you need to create an instance with $pdf = new yourPDFclassName(); (which extends the parent FDF class).

Running the whole thing again I receive the error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 77 bytes) in /..blabla/yourPDFclassName.php on line 16

Does anyone have an idea why this error occurs when I call the child class? I mean it works with the parent class... And btw, 77 bytes are much smaller than the 33554432 bytes ... hmm

class REPORTSPDF extends FPDF { .... }

16: $pdf = new REPORTSPDF();

The line 16 is in the constructor of REPORTSPDF. There are no other lines before line 16. It just crashes when $pdf = new REPORTSPDF() is called.

Without the Footer function I have the same error. The weird thing is that when I change line 16 to

$pdf = new FPDF();

everything works fine (with the exception that I don't have a footer).

标签: php fpdf
5条回答
▲ chillily
2楼-- · 2019-07-15 14:07

Change your memory_limit.

try,

ini_set('memory_limit','128M');
查看更多
beautiful°
3楼-- · 2019-07-15 14:08

Increase memory limit

There are 3 ways to increase memory limit

  • using config file

    Change memory limit in php.ini

    memory_limit = 32M

  • using PHP

    ini_set('memory_limit','32M');

  • using htaccess

    php_value memory_limit 32M

Methods in different server

Shared Hosting

php_value memory_limit 32M

Dedicated or VPS Optimized

ssh -lroot domain.com

locate php.ini

vi /usr/local/php/etc/php.ini

edit to 

memory_limit=32M;

save file

httpd restart

/sbin/service httpd restart
查看更多
Deceive 欺骗
4楼-- · 2019-07-15 14:15

sounds like you have an infinite loop in you code. try to do a simple hello-world-test ans see what happens and check all loop in your code.

查看更多
SAY GOODBYE
5楼-- · 2019-07-15 14:16

The error message means that, while attempting to allocate an additional 77 bytes, the memory limit of 33554432 bytes was exceeded.

There are only two ways around this: either optimize the code in your subclass so you don't need as much memory, or increase your memory limit in php.ini (or using equivalent methods for manipulating with the PHP configuration).

查看更多
霸刀☆藐视天下
6楼-- · 2019-07-15 14:17

I've not used it myself, but the FPDF2File extension to FPDF is an attempt to build the PDF page by page to disk rather than purely in memory

查看更多
登录 后发表回答