What is the most efficient way to create large PDF files using TCPDF?

I am working on implementing a report generation solution using TCPDF. Some of my reports are small (2-3 pages), but the user has the opportunity to select several reports at once and request all of them.

I am currently creating one PDF file containing all reports, each report starts on a new page and uses page grouping so that pagination is restarted for each report. When it works, it works fine, but after I select too many reports, the code is reset and I get an empty PDF file.

UPDATE: I should have mentioned that creating a separate PDF request is a requirement from the client. They want a PDF summary to easily switch between reports within the same PDF when they have selected many reports.

My questions:

  • What is the most efficient way to create this PDF file without ending with an empty PDF file? I cannot find if there is a size limit that TCPDF can handle.

  • Should I use ob_start () in PHP or create a large string as I am fine?

  • My reports were originally HTML, so I am posting this TCPDF. However, would TCPDF performance be better if I used other methods for outputting information (e.g. Cell, MultiCell, etc.)?

Here is a snippet of my code that outputs a PDF. The $ pdf object is configured according to the corresponding TCPDF examples included in 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 } 
+4
source share
2 answers

Answer to question 1:

Well, this is another one due to my inexperience with PHP. A blank PDF was obtained by default max_execution_time for PHP set to 30 seconds. I used ini_set to set the value to 300 to generate a PDF script, and my report was fully generated.

For a 16-page PDF file, it took 1 min 30 seconds to generate. Does this sound like anyone with more TCPDF experience?

Answer to question 3:

I experimented a bit, and using direct PDF functions from TCPDF to create a PDF file is certainly faster than transferring HTML and CSS to TCPDF and then creating a PDF file. It makes sense, but it's nice to see the real difference in performance. Therefore, while creating a layout for PDF queries is tedious if you have large PDF files, the performance gain is probably worth it.

Questions 2:

I would appreciate the contribution of more experienced PHP users to using ob_start.

+3
source

I also had problems with large files and TCPDF. I switched from TCPDF to mPDF. This project is based on FPDF and HTML2PDF and is much faster with HTML parsing.

eg. TCPDF took an overview of the article (150 pages with images, links and some materials) from about 70 to 80 seconds.

The same HTML code and the same PHP script, which was replaced only by the mPDF class, and it took only 6-8 seconds to create the same PDF file.

+4
source

Source: https://habr.com/ru/post/1384119/


All Articles