I am working my way through implementing a report generation solution using TCPDF. Some of my reports are small (2-3 pages), but the user has the option to select many reports at once, and request them all.
Currently I generate a single PDF containing all the reports, with each report starting on a new page, and using page grouping so the page numbering is restarted for each report. When it works, it work great, but after I select too many reports, the code churns away and I end up with an empty PDF.
UPDATE: I should have mentioned that creating a single PDF is a requirement from the client. They want to have a PDF table of contents to easily switch between reports within a single PDF when they have selected many reports.
My questions are:
-
What is the most efficient way to produce this PDF without ending up with the blank PDF? I can’t seem to find if there is a limit on the size TCPDF can handle.
-
Should I be using ob_start() in the PHP or is building up a big string as I am doing okay?
-
My reports were originally HTML, so I am sending that TCPDF. However, would TCPDF’s performance be better if I used the other methods to output the info (e.g. Cell, MultiCell, etc.)?
Here is the piece of my code that outputs the PDF. The $pdf object is set up as per the relevant TCPDF examples included with the library:
foreach ($students_info as $student_info) {
$info = $student_info->fetch_object(); // get query result object
// put in the student information
$pdf->set_student_info($info->lastName, $info->firstName, $info->rank, $info->idNum);
$pdf->startPageGroup(); // start a page group to handle paging for multiple students
$pdf->AddPage(); // add a page
$html = "<style>";
$html .= file_get_contents(/*some style sheet*/);
$html .= file_get_contents(/*some other style sheet*/);
$html .= "</style>";
$html .= start_report_div($i);
$html .= '<table class="report_table">'.
'<tbody>'.
'<tr><td>';
$html .= display_report_title($report);
$html .= display_student_info($db, $info);
$html .= display_academic_comments_body($db, $info->studentID, $info->sessionID);
$html .= display_signature_block($report);
$html .= '</td></tr>'.
'</tbody>'.
'</table>';
$html .= end_report_div();
$pdf->writeHTML($html, // the content
true, // put a newline after text
false, // paint background, false = transparent
true, // reset last cell height
false, // add left padding
'' // align
);
$html = ''; // reset for next student
$pdf->lastPage(); // pointer to last page in case we are doing more than one student
}
Answer to question 1:
Well, this is another one caused by my inexperience with PHP. The blank PDF was resulting from the default max_execution_time for PHP set to 30 sec. I used
ini_setto set the value to 300 for the PDF generation script, and my report was generated in full.For a 16 page PDF, it took 1 min 30 sec to generate. Does that seem long to anyone with more TCPDF experience?
Answer to question 3:
I have done some experimenting, and using the direct PDF functions from TCPDF to generate the PDF is definitely faster than passing the HTML and CSS to TCPDF and then having it create the PDF. Makes sense, but still nice to see the actual performance difference. So although creating the layout for the PDF calls is tedious, if you have big PDFs, the performance gain is likely worth it.
Questions 2:
I would still appreciate input from more experienced PHP users as to the use of ob_start.