Create a PDF file taking huge data from MySQL using PHP

I want to make a PDF file that will contain huge data, such as 12-month data. Each month contains 500 rows from a MySQL table. I tried with FPDF, but it takes too much time, which is unacceptable. If there is any other better PHP library or class to make it easy and not to make the system busy?

If there is any script that will take data for 12 months, one month for one month and write them to PDF one at a time and finally create a PDF file?

+6
source share
3 answers

I regularly create very large PDF files from PHP using the (usually) same method. First, I create a version of the document on the web page and output it to a file. Then I run the file through wkhtmltopdf. With this method, I have done better than any other. HTML should generate quickly, and wkhtmltopdf is surprisingly fast and efficient. In addition, you can use CSS3 methods for complex formatting.

For extremely large PDF files, I also created smaller ones, and then combined them with pdfmeld. It also gives you the ability to add bookmarks.

+2
source

Building a PDF file is always very large. If you have a huge amount of resources, it will be a very long time; you cannot avoid it. I see one way to make this softer for your users:

Sending them the html-rendering, and then using Ajax do asynchronous pdf creation. This way you can display the status bar. and let your users know that they are fulfilling their requirements.

Then generation can be technically performed in three ways:

  • using fpdf and hard location coding
  • using dom2pdf or a library such that your html displays the pdf file.
  • create a latex string, and then use proc_open ask the latex compiler to create a PDF file. This is a really fast and reliable tool, but you need a dedicated server, not a shared host.
+1
source
  // you can used fpdf like this. <?php require('fpdf.php'); class PDF extends FPDF { // Page header function Header() { // Logo $this->Image('logo.png',10,6,30); // Arial bold 15 $this->SetFont('Arial','B',15); // Move to the right $this->Cell(80); // Title $this->Cell(30,10,'Title',1,0,'C'); // Line break $this->Ln(20); } // Page footer function Footer() { // Position at 1.5 cm from bottom $this->SetY(-15); // Arial italic 8 $this->SetFont('Arial','I',8); // Page number $this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'C'); } } // Instanciation of inherited class $pdf = new PDF(); $pdf->AliasNbPages(); $pdf->AddPage(); $pdf->SetFont('Times','',12); for($i=1;$i<=40;$i++) $pdf->Cell(0,10,'Printing line number '.$i,0,1); $pdf->Output(); ?> 
+1
source

All Articles