Reputation: 1979
I'm creating a pdf file on fly using fpdf of PHP from the database but when there are a lot of data in my db , either the file cant be created completely and just half of it comes out, or nothing will happen or I get run-time error what should I do to be able to create large PDF files without error?
large pdf :
The code is like :
require('lib\fpdf.php');
require ('lib\pdf.php');
include ('lib\bukaDb.php');
$pdf = new PDF('P','pt','A4');
$pdf->SetFont('Arial','',8);
$pdf->AliasNbPages();
$pdf->Open();
$pdf->setY($pdf->tMargin);
for ($i=0; $i<count($var); $i++){
//Do something , show data
//$pdf->Ln(20);
}
// Finally, produce the PDF!!
$pdf->Output();
And my table has hundreds of rows, I cant use set time limit function, any other recommendation?
Upvotes: 0
Views: 5341
Reputation: 21979
Move you script to crontab and set saved pdf as file. Once your script finished, set it to send email telling you that the process finished.
As rough pseudo-code:
require('lib\fpdf.php');
require ('lib\pdf.php');
include ('lib\bukaDb.php');
$pdf = new PDF('P','pt','A4');
$pdf->SetFont('Arial','',8);
$pdf->AliasNbPages();
$pdf->Open();
$pdf->setY($pdf->tMargin);
for ($i=0; $i<count($var); $i++){
//Do something , show data
//$pdf->Ln(20);
}
// Finally, produce the PDF!!
$savefile = "/path/to/output/dir/output.pdf";
$pdf->Output($savefile,'I');
mail('[email protected]', 'PDF Generator', "PDF has been generated at $savefile");
Then, all you have to do is, save it to somewhere on your server and set it's path as crontab:
/path/to/php/cli/php /path/to/your/saved/php/pdf_generator.php
Please note that you have to:
$savefile
directory is writable by script.Upvotes: 1
Reputation: 13141
We create PDF files (using fpdf) with hundreds of pages without any problem, so I guess it's your code that is collecting / displaying data that causes the request to time out.
When your HTTP request takes long to complete (more than 10 seconds) then it's probably your script simply taking too much time to complete.
I really think the problem is what happens inside that for
loop. Try to create a simple dummy PDF with about the same amount of pages. If that works, you know that you have to search the problem inside the for
loop.
Upvotes: 1
Reputation: 19695
Your script is probably timing out. Try disabling the built-in time limit:
set_time_limit(0);
If you host your site or app on a shared-hosting server though, make sure you are allowed to do this. Some web hosters (including mine) frown upon using lots of server resources for tasks like this.
Upvotes: 2