I am using the TCPDF library to simultaneously download pdf's onto my local
drive.
PHP CODE:
$text = //an array of id's
$id = explode(',', $text);
for ($i=0;$i<count($id);$i++){
if(//id exists in database){
createPDF($id,'test123','F');
}
}
createPDF is where the tcpdf library is used to create pdf's
This works, and the first few pdf's are generated, but soon enough, I get an
error saying
"Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to
allocate 25368 bytes)..."
Is there a way I could fix this? or is it a tcpdf bug?
Thanks in advance guys
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
First be sure you are using the latest TCPDF version.
You are probably creating multiple TCPDF instances without releasing them
(because the are waiting the end of the loop).
Try to reuse the same TCPDF object and/or manually destroy object using the
_destroy() method.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
@nicolaasuni
Hey, thanks for your reply. I do have the latest TCPDF version.
I think you are right about the multiple instances part, but I just want to
clarify.
-so when i call createPDF,
in the loop.....
1)a new instance of MyPdf() class is created "$pdf = new MyPdf($title);"
-the MyPdf class is used to modify the header and footer of the document. it extends the tcpdf class
2)then the content of the pdf is generated and appended to the pdf
3)output() is called : $pdf->Output($id.'.pdf', $outputType);
-then output calls close() which calls _destroy()
so when at the point _destory() is called, shouldnt that instance of the
MyPdf/tcpdf class be deleted?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Unfortunately the PHP garbage collector is not perfect and you have to adapt
your code to minimize the waste of memory.
The _destroy() method doesn't destroy all things.
Try to manually free the memory at each cycle using php functions.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Thanks a lot guys!
It helped. But I believe it may be a bug in the tcpdf library because I still
got the same error after unsetting each instance of the MyPdf object.
The way I fixed the problem was by making a curl request to a method that
generated 5 pdf's at once. That way, each request served as a different
process and this error was not see.
That is, say I have a total of 40 pdf that need to be generated,
I made a curl request to another method that generated 5 pdf's at a time.
Thanks
Yash
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hey guys,
I am using the TCPDF library to simultaneously download pdf's onto my local
drive.
PHP CODE:
$text = //an array of id's
$id = explode(',', $text);
for ($i=0;$i<count($id);$i++){
if(//id exists in database){
createPDF($id,'test123','F');
}
}
createPDF is where the tcpdf library is used to create pdf's
This works, and the first few pdf's are generated, but soon enough, I get an
error saying
"Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to
allocate 25368 bytes)..."
Is there a way I could fix this? or is it a tcpdf bug?
Thanks in advance guys
First be sure you are using the latest TCPDF version.
You are probably creating multiple TCPDF instances without releasing them
(because the are waiting the end of the loop).
Try to reuse the same TCPDF object and/or manually destroy object using the
_destroy() method.
@nicolaasuni
Hey, thanks for your reply. I do have the latest TCPDF version.
I think you are right about the multiple instances part, but I just want to
clarify.
-so when i call createPDF,
in the loop.....
1)a new instance of MyPdf() class is created "$pdf = new MyPdf($title);"
-the MyPdf class is used to modify the header and footer of the document. it extends the tcpdf class
2)then the content of the pdf is generated and appended to the pdf
3)output() is called : $pdf->Output($id.'.pdf', $outputType);
-then output calls close() which calls _destroy()
so when at the point _destory() is called, shouldnt that instance of the
MyPdf/tcpdf class be deleted?
Unfortunately the PHP garbage collector is not perfect and you have to adapt
your code to minimize the waste of memory.
The _destroy() method doesn't destroy all things.
Try to manually free the memory at each cycle using php functions.
Maybee this
will help you, too.
Cheers,
Jan
Thanks a lot guys!
It helped. But I believe it may be a bug in the tcpdf library because I still
got the same error after unsetting each instance of the MyPdf object.
The way I fixed the problem was by making a curl request to a method that
generated 5 pdf's at once. That way, each request served as a different
process and this error was not see.
That is, say I have a total of 40 pdf that need to be generated,
I made a curl request to another method that generated 5 pdf's at a time.
Thanks
Yash