需要在tcpdf中使用for或if循环添加多个页面以获取动态内容

时间:2017-10-26 06:06:13

标签: php arrays pdf tcpdf

我正在使用tcpdf,需要生成pdf, 我需要帮助显示多个页面。 第一页将包含客户详细信息, 在第一页之后,将根据for循环添加其他页面。 以及如何在此pdf中添加修复页眉和页脚。 我需要在页眉中显示以下徽标,并在页脚中显示代理的电话和电子邮件。 这是我的代码

if(isset($_GET['packid']))
{
$id=$_GET['packid'];
include_once 'db_connect.php';
include_once 'functions.php';
sec_session_start();
/*if(login_check($mysqli) == false)
{
header('location:../index.php');
exit();
}*/


require_once('tcpdf/tcpdf.php');
$obj_pdf = new TCPDF('P', PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$obj_pdf->SetProtection(array('copy','modify'), '', 'RAVIjordan!@#$)(*&15789537447344789', 0, null);
$obj_pdf->SetCreator(PDF_CREATOR);
$obj_pdf->SetTitle('Export HTML Table data to PDF using TCPDF in PHP');
$obj_pdf->SetHeaderData('', '', PDF_HEADER_TITLE, PDF_HEADER_STRING);
$obj_pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$obj_pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
$obj_pdf->SetDefaultMonospacedFont('helvetica');
$obj_pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
$obj_pdf->SetMargins(PDF_MARGIN_LEFT, '5', PDF_MARGIN_RIGHT);
$obj_pdf->setPrintHeader(false);
$obj_pdf->setPrintFooter(false);
$obj_pdf->SetAutoPageBreak(TRUE, 10);
$obj_pdf->SetFont('helvetica', '', 12);
$obj_pdf->AddPage();
$content = '';

$content .='
<html>
<head>';

$content .='
<style>
table, td, th {
border: 1px solid #ddd;
text-align: left;
}

table {
width: 100%;
}

th, td {
padding: 15px;
}
</style>';

$content .='
</head>
<body>';
//this is the agent details
$agent = $_SESSION['username'];
$pic= mysqli_query($mysqli,"SELECT * FROM rmt_users where rmt_uname = '$agent'");
//page 1 agent with client details
while($pp=mysqli_fetch_array($pic))
{
    $image = '../'.$pp['rmt_ulogo'];
    $phone = $pp['rmt_uemail'];
    $email = $pp['rmt_uphone'];
}
$content .= '<h3 align="center">TOUR DETAILS</h3><br>
<table border="1"  cellspacing="0" cellpadding="5">';
$content .='<tr align="center"><td colspan="2"><img src="'.$image.'" height="150px" width="auto" ></td><td colspan="2">Email : '.$email.' <br>Phone : '.$phone.'</td></tr>';
//agent detials ends

$queryagent= mysqli_query($mysqli,"SELECT * FROM rmt_bookings where b_id='$id'");

//ineed this section here to be page 1
while($rs=mysqli_fetch_array($queryagent))
{

$content .= '
<tr><td>Client Name</td><td colspan="3"> '.$rs["b_cname"].'</td></tr>
<tr><td>Client Address</td><td colspan="3"> '.$rs["b_caddress"].'</td></tr>
<tr><td>Client Phone</td><td colspan="3"> '.$rs["b_cphone"].'</td></tr>
<tr><td>Client Email</td><td colspan="3"> '.$rs["b_cemail"].'</td></tr>
<tr><td>No of Persons</td><td>No of Adults :'.$rs["b_cadult"].'</td>
<td>No of Child : '.$rs["b_cchild"].'</td>
<td>No of Infants : '.$rs["b_cinfant"].'</td></tr>
<tr><td>Package Name</td><td colspan="3"> '.$rs["b_pname"].'</td></tr>
<tr><td>Tour Location</td><td colspan="3"> '.$rs["b_location"].'</td></tr>
<tr><td>Depature Date</td><td colspan="3"> '.$rs["b_date"].'</td></tr>
<tr><td colspan="4"><b>Destination Details</b></td></tr>
';

$b_destinations = unserialize( base64_decode( $rs['b_destinations'] ) );
$b_nights = unserialize( base64_decode( $rs['b_nights'] ) );

foreach (array_combine($b_destinations[0], $b_nights[0]) as $desti=>$nights)
{
$content .='<tr><td colspan="2"> '.$desti.'</td><td colspan="2">Nights : '.$nights.'</td></tr></table>';
}
//page1 ends here




$b_hdest = unserialize($rs['b_hdest']);
$b_hname = unserialize($rs['b_hname']);
$b_hadd = unserialize($rs['b_hadd'] );
$b_hphone = unserialize($rs['b_hphone']);
$b_hin = unserialize($rs['b_hin']);
$b_hout = unserialize( $rs['b_hout']);
$b_hroom = unserialize( $rs['b_hroom'] );
$b_hmeal = unserialize( $rs['b_hmeal'] );
$b_haextra = unserialize( $rs['b_haextra'] );
$b_hcextra = unserialize( $rs['b_hcextra'] );
$b_hspecial = unserialize( $rs['b_hspecial'] );
$b_hincl = unserialize( $rs['b_hincl'] );


for($i=0;$i<count($b_hdest[0]);$i++){
//here multiple page will be add bcoz, the page will be added according this for loop.
$content .= '<table border="1"  cellspacing="0" cellpadding="5">';
$content .='<tr align="center"><td colspan="2"><img src="'.$image.'" height="150px" width="auto" ></td><td colspan="2">Email : '.$email.' <br>Phone : '.$phone.'</td></tr>';
$content .='<tr><td colspan="4"><h4>Hotel details</h4></td></tr>';
$content .='<tr><td colspan="4"><b>Hotel Details '.($i+1).' : '.$b_hname[0][$i].' </b></td></tr>';
$content .='<tr><td><b>Destination</b></td><td><b>Address</b></td><td><b>Phone</b></td><td><b>Check Out Time</b></td></tr>';
$content .='<tr><td>'.$b_hdest[0][$i].'</td><td>'.$b_hadd[0][$i].'</td><td>'.$b_hphone[0][$i].'</td><td>10 AM</td></tr>';
$content .='<tr><td><b>Chekc In</b></td><td><b>Check Out</b></td><td><b>Rooms</b></td><td><b>Meals</b></td></tr>';
$content .='<tr><td>'.$b_hin[0][$i].'</td><td>'.$b_hout[0][$i].'</td><td>'.$b_hroom[0][$i].'</td><td>'.$b_hmeal[0][$i].'</td></tr>';
$content .='<tr><td><b>Extra Adult</b></td><td><b>Extra Child</b></td><td><b>Special</b></td><td><b>Inclusions</b></td></tr>';
$content .='<tr><td>'.$b_haextra[0][$i].'</td><td>'.$b_hcextra[0][$i].'</td><td>'.$b_hspecial[0][$i].'</td><td>'.$b_hincl[0][$i].'</td></tr>';
//page ends here
}


$content .='</table>';

$content .='</body></html>';
$obj_pdf->writeHTML($content);
ob_end_clean();
$obj_pdf->Output('tourdetails.pdf', 'I');
}
}
拜托,我需要帮助。

1 个答案:

答案 0 :(得分:0)

<br pagebreak="true"/> 标记可帮助您从HTML代码中添加新页面。但你必须自己创造条件。

@ComponentScan

我从下面的链接得到了这个答案。

https://developer.android.com/topic/libraries/architecture/paging.html

希望它有所帮助。