I need to generate PDF page based on PHP foreach result and display 2 columns (A4 PDF Landscape mode). For e.g. Data look like this:
Row data 1
Row data 2
Row data 3
Row data 4
Row data 5
Row data 6
if the height exceeds in PDF column 1 (HTML table), it will move to 2nd column of page and it looks like:
Row data 1 Row data 7
Row data 2 Row data 8
Row data 3 Row data 9
Row data 4
Row data 5
Row data 6
My code:
<table width="100%" cellspacing="0" cellpadding="0" border="0">
<thead>
<tr>
<th width="140px">Subject</th>
<th width="100px">Teacher</th>
<th width="8%">Exam</th>
</tr>
</thead>
<tbody>
<?php
foreach($data as $std)
{
?>
<tr>
<td><?php echo $std->data1; ?></td>
<td><?php echo $std->data2; ?></td>
<td><?php echo $std->data3; ?></td>
</tr>
<?php
}
?>
</tbody>
</table>
try like this
require('fpdf.php');//download fpdf and include
$pdf = new FPDF();
$myarray = array(1,2,3);
$pdf->SetFont('Arial','B',16);
foreach($myarray as $value){
$pdf->AddPage();
$pdf->Cell(40,10,$value);
}
$pdf->Output()
A4 is actually height:297mm and width:210mm for web in Portrait follow this link for more http://www.thecalculatorsite.com/forum/topics/width-and-height-a4-paper.php. So for landscape it will be height:210mm and width:297mm. I have given you an approximate code for your purpose. Modify the $max_rows with how many rows a page can contain without breaking. And style the height width property of the table, columns and rows to support your need.
<!DOCTYPE html>
<html>
<head>
<style>
body {
height: 210mm;
width: 297mm;
/* to centre page on screen*/
margin-left: auto;
margin-right: auto;
}
</style>
</head>
<body>
<table style="float:left;" width="50%" cellspacing="0" cellpadding="0" border="0">
<thead>
<tr>
<th width="140px">Subject</th>
<th width="100px">Teacher</th>
<th width="8%">Exam</th>
</tr>
</thead>
<tbody>
<?php
$max_rows = 6;
$count_row = 0;
foreach($data as $std)
{
$count_row++;
if($count_row > $max_rows)
$count_row = 0;
if($count_row == 0) {
?>
</tbody>
</table>
<table style="float:left;" width="50%" cellspacing="0" cellpadding="0" border="0">
<thead>
<tr>
<th width="140px">Subject</th>
<th width="100px">Teacher</th>
<th width="8%">Exam</th>
</tr>
</thead>
<tbody>
<?php } ?>
<tr>
<td><?php echo $std->data1; ?></td>
<td><?php echo $std->data2; ?></td>
<td><?php echo $std->data3; ?></td>
</tr>
<?php
}
?>
</tbody>
</table>
</body>
</html>