Mohamed Q
Mohamed Q

Reputation: 15

PHP/HTML Export table as pdf?

I have a HTML table that displays data from a db using PHP, I'd like to give an option to print the table out (as a button). I thought that exporting as a pdf would be the simplest option to do this. This is how I have the table laid out.

<table id="tfhover" class="tftable" border="1">

  <tr><th>Refund ID</th><th>Customer ID</th><th>Manager ID</th><th>Customer Name</th><th>Amount Refunded</th><th>Refund Date/Time</th><th>Details</th></tr>

  <?php 
     //some php code...

     echo '<tr>'; 
     echo '<td>'. $idRefund[$i] .'</td>'; 
     echo '<td>'. $idCustomer[$i] .'</td>'; 
     echo '<td>'. $idManager[$i] .'</td>'; 
     echo '<td>'. $customerName[$i] .'</td>'; 
     echo '<td>'. $amount[$i] .'</td>'; 
     echo '<td>'. $dateTime[$i] .'</td>'; 
     echo '<td>'. $details[$i] .'</td>'; 
     echo '</tr>';
     $i++; 
 ?>

</table>

How would I go about printing this table using php or javascript or even exporting as a pdf if thats more simplistic.

edit: I ended up using mPDF which is very similar to FPDF in terms of the methods available to use. The help is much appreciated, thanks.

Upvotes: 0

Views: 5321

Answers (2)

Abdulla Nilam
Abdulla Nilam

Reputation: 38672

The PDF Generator

    $pdf=new FPDF();
    $pdf->AddPage();
    $pdf->SetFont('Arial','B',16);
    $pdf->Cell(40,10,'Hello World!');
    $pdf->Output();
?

To generate a pdf file, first we need to include library file fpdf.php. Then we need to create an FPDF object using the default constructor FPDF(). This constructor can be passed three values namely page orientation (portrait or landscape), measure unit, and page size (A4, A5, etc.,). By default pages are in A4 portrait and the measure unit is millimeter. It could have been specified explicitly with:

$pdf=new FPDF('P','mm','A4');

It is possible to use landscape (L), other page formats (such as Letter and Legal) and measure units (pt, cm, in).

Then we have added a page to our pdf document with AddPage(). The origin is at the upper-left corner and the current position is by default placed at 1 cm from the borders; the margins can be changed with the function SetMargins().

To print a text, we need to first select a font with SetFont(). Let us select Arial bold 16

$pdf->SetFont('Arial','B',16);

We use Cell() function to output a text. A cell is a rectangular area, possibly framed, which contains some text. It is output at the current position. We specify its dimensions, its text (centered or aligned), if borders should be drawn, and where the current position moves after it (to the right, below or to the beginning of the next line). To add a frame, we would do this:

$pdf->Cell(40,10,'Hello World !',1);

Finally, the document is closed and sent to the browser with Output(). We could have saved it in a file by passing the desired file name.

To learn Fpdf: Click Here To refer about this code : Click Here

Upvotes: 1

Limon Monte
Limon Monte

Reputation: 54449

Take a look at KnpLabs/snappy, it generates PDF from a url or a html page.

Upvotes: 0

Related Questions