2014-04-28 48 views
0

if语句无法正常工作,它将正确打印一页,然后打印另外17页没有列标题和仅一个数据行。我一直无法弄清楚这种方式无法正常工作。有一个更好的方法吗。我正在做FPDF网站的休息处理。我的if语句无法正常工作

链接到一个PDF文件,以显示这是什么代码产生http://notaryaccounting.com/testpdf.pdf

$pdf = new PDF(); 
$pdf->AddPage ($orientation='', $format='', $keepmargins=false, $tocpage=false); 
$pdf->SetPrintHeader(true); 
$pdf->SetPrintFooter(true); 
$pdf->setFontSubsetting(true); 
// set margins 
$pdf->SetMargins(10, PDF_MARGIN_TOP, 10); 

$pdf->SetFont('Arial', 'BI', 20, '', 'false'); 
$year = date("Y"); ; 
$html = 'Mileage for signing1 dates from 1/1/' .$year. ' to 12/31/' .$year. ''; 
$pdf->SetXY(2, 15); 
$pdf->SetFont('times', '', 8, '', true); 
$pdf->writeHTML ($html, $ln=true, $fill=false, $reseth=false, $cell=false, $align='R'); 


//set initial y axis position per page 
$y_axis_initial = 20; 

//print column titles 
$pdf->SetFillColor(232,232,232); 
$pdf->SetFont('Arial','B',12); 
$pdf->SetY($y_axis_initial); 
$pdf->SetX(10); 
$pdf->Cell(20,6,'Invoice #:',1,0,'L',1); 
$pdf->Cell(27,6,'Signing Date:',1,0,'L',1); 
$pdf->Cell(30,6,'Name:',1,0,'L',1); 
$pdf->Cell(60,6,'Location:',1,0,'L',1); 
$pdf->Cell(15,6,'Start:',1,0,'L',1); 
$pdf->Cell(15,6,'End:',1,0,'L',1); 
$pdf->Cell(20,6,'Mileage:',1,0,'L',1); 

//Go to next row 
$y_axis = 26; 
$y_axis = $y_axis + $row_height; 

//initialize counter 
$i = 0; 

//Set maximum rows per page 
$max = 25; 

//Set Row Height 
$row_height = 6; 





$result = $db->query("SELECT invoicenumber,signstart,fname,lname,street1,city,state,mstart,mend FROM signings1 WHERE pid = $pid ORDER by signstart"); 
foreach($result as $key => $sig) { 
    $totalcont = $totalcont + 1; 
    echo $i; 
    //If the current row is the last one, create new page and print column title 
     if ($i == $max){ 

     $pdf->AddPage(); 

     //print column titles for the current page 
     $pdf->SetY($y_axis_initial); 
     $pdf->SetFillColor(232,232,232); 
     $pdf->SetFont('Arial','B',12); 
     $pdf->SetX(05); 
     $pdf->Cell(20,6,'Invoice #:',1,0,'L',1); 
     $pdf->Cell(27,6,'Signing Date:',1,0,'L',1); 
     $pdf->Cell(30,6,'Name:',1,0,'L',1); 
     $pdf->Cell(60,6,'Location:',1,0,'L',1); 
     $pdf->Cell(15,6,'Start:',1,0,'L',1); 
     $pdf->Cell(15,6,'End:',1,0,'L',1); 
     $pdf->Cell(20,6,'Mileage:',1,0,'L',1); 

     //Go to next row 
     $y_axis = $y_axis + $row_height; 

     //Set $i variable to 0 (first row) 
     $i = 0; 


    } 



     $invoicenumber = $sig[invoicenumber]; 
     $signdate=phpdate($sig[signstart]); 
     $name = $sig[fname] . ' ' . $sig[lname]; 
     $location = $sig[street1] . " " . $sig[city] . " " . $sig[state]; 
     $mstart = $sig[mstart]; 
     $mend = $sig[mend]; 
     $miles = $sig[miles]; 
     $miles1 = $mend - $mstart; 
     $subTotal += $miles1; 
     $totalmoney = $subTotal * .56; 

     $pdf->SetFont('Arial','B',9); 
     $pdf->SetFillColor(255,255,255); 
     $pdf->SetY($y_axis); 
     $pdf->SetX(10); 
     $pdf->Cell(20,6,$invoicenumber,1,0,'L',1); 
     $pdf->Cell(27,6,$signdate,1,0,'L',1); 
     $pdf->Cell(30,6,$name,1,0,'L',1); 
     $pdf->Cell(60,6,$location,1,0,'L',1); 
     $pdf->Cell(15,6,$mstart,1,0,'L',1); 
     $pdf->Cell(15,6,$mend,1,0,'L',1); 
     $pdf->Cell(20,6,$miles1,1,0,'L',1); 

    //Go to next row 
    $y_axis = $y_axis + $row_height; 
    $i = $i + 1; 
} 

ob_end_clean(); 
$pdf->Output(); 
+0

我不是从移除错误除了触摸原始代码和检查分页符是否正常工作。我会去看看它,看看我能否理解它。我会以某种关于它的信息回复你。它可能是明天,因为它在这里傍晚。我之前没有玩过pdf输出,所以我正在学习新的东西。 –

回答

0

变化if ($i == $max){if ($i != 0 && $i % $max == 0) { 这将执行新的,,页'每25记录$i != 0在那里,以确保新的一页难道不被添加到第1条记录。

1

这没什么大错。一些未定义的变量。我已经改变了一些变量的名称,但没有什么不对的,我可以发现原来的。

我没有安装'Arial'字体,因为我刚刚下载并安装了原始的'tcpdf'软件。

它现在分页正确。

<?php 
require __DIR__ .'/tcpdf/tcpdf.php'; 
require __DIR__ .'/tcpdf/tcpdf_autoconfig.php'; 
$pid = 1; 

$dsn = 'mysql:host=localhost;dbname=testmysql'; 
$username = 'test'; 
$password = 'test'; 
$options = array(
    PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8', 
); 
$db = new PDO($dsn, $username, $password, $options); 
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 


// $db = new mysqli('localhost', 'test', 'test', 'testmysql'); 

$pdf = new TCPDF(); 
$pdf->AddPage ($orientation='', $format='', $keepmargins=false, $tocpage=false); 
$pdf->SetPrintHeader(true); 
$pdf->SetPrintFooter(true); 
$pdf->setFontSubsetting(true); 
// set margins 
$pdf->SetMargins(10, PDF_MARGIN_TOP, 10); 

$pdf->SetFont('arial', 'BI', 20, '', 'false'); 
$year = date("Y"); ; 
$html = 'Mileage for signing1 dates from 1/1/' .$year. ' to 12/31/' .$year. ''; 
$pdf->SetXY(2, 15); 
$pdf->SetFont('times', '', 8, '', true); 
$pdf->writeHTML ($html, $ln=true, $fill=false, $reseth=false, $cell=false, $align='R'); 


//set initial y axis position per page 
$y_axis_initial = 20; 

//print column titles 
$pdf->SetFillColor(232,232,232); 
$pdf->SetFont('arial','B',12); 
$pdf->SetY($y_axis_initial); 
$pdf->SetX(10); 
$pdf->Cell(20,6,'Invoice #:',1,0,'L',1); 
$pdf->Cell(27,6,'Signing Date:',1,0,'L',1); 
$pdf->Cell(30,6,'Name:',1,0,'L',1); 
$pdf->Cell(60,6,'Location:',1,0,'L',1); 
$pdf->Cell(15,6,'Start:',1,0,'L',1); 
$pdf->Cell(15,6,'End:',1,0,'L',1); 
$pdf->Cell(20,6,'Mileage:',1,0,'L',1); 

//Set Row Height 
$row_height = 6; 

$totalcount = 0; 
$subTotal = 0; 
$miles = 0; 

//Go to next row 
$y_axis = 26; 
$y_axis = $y_axis + $row_height; 

//initialize counter 
$pageLine = 0; 

//Set maximum rows per page 
$itemsPerPage = 3 /*25 */; 

// 
$result = $db->query("SELECT invoicenumber,signstart,fname,lname,street1,city,state,mstart,mend FROM signings1 WHERE pid = $pid ORDER by signstart"); 
foreach($result as $key => $sig) { 
// var_dump($key, $sig); 
    $totalcount = $totalcount + 1; 
    echo $pageLine; 
    //If the current row is the last one, create new page and print column title 
     if ($pageLine >= $itemsPerPage) { 

     $pdf->AddPage(); 

     //print column titles for the current page 
     $pdf->SetY($y_axis_initial); 
     $pdf->SetFillColor(232,232,232); 
     $pdf->SetFont('arial','B',12); 
     $pdf->SetX(05); 
     $pdf->Cell(20,6,'Invoice #:',1,0,'L',1); 
     $pdf->Cell(27,6,'Signing Date:',1,0,'L',1); 
     $pdf->Cell(30,6,'Name:',1,0,'L',1); 
     $pdf->Cell(60,6,'Location:',1,0,'L',1); 
     $pdf->Cell(15,6,'Start:',1,0,'L',1); 
     $pdf->Cell(15,6,'End:',1,0,'L',1); 
     $pdf->Cell(20,6,'Mileage:',1,0,'L',1); 

     $subTotal = 0; 
     //Go to next row 
     $y_axis = $y_axis + $row_height; 

     //Set $pageLine variable to 0 (first row) 
     $pageLine = 0; 
    } 

     $invoicenumber = $sig['invoicenumber']; 
     $signdate=date($sig['signstart']); 
     $name = $sig['fname'] . ' ' . $sig['lname']; 
     $location = $sig['street1'] . " " . $sig['city'] . " " . $sig['state']; 
     $mstart = $sig['mstart']; 
     $mend = $sig['mend']; 
     $miles = $mend - $mstart; 
     // $miles = $sig['miles']; 
     $subTotal += $miles; 
     $totalmoney = $subTotal * .56; 

     $pdf->SetFont('arial','B',9); 
     $pdf->SetFillColor(255,255,255); 
     $pdf->SetY($y_axis); 
     $pdf->SetX(10); 
     $pdf->Cell(20,6,$invoicenumber,1,0,'L',1); 
     $pdf->Cell(27,6,$signdate,1,0,'L',1); 
     $pdf->Cell(30,6,$name,1,0,'L',1); 
     $pdf->Cell(60,6,$location,1,0,'L',1); 
     $pdf->Cell(15,6,$mstart,1,0,'L',1); 
     $pdf->Cell(15,6,$mend,1,0,'L',1); 
     $pdf->Cell(20,6,$miles,1,0,'L',1); 

    //Go to next row 
    $y_axis = $y_axis + $row_height; 
    $pageLine = $pageLine + 1; 
} 

ob_end_clean(); 
$pdf->Output(); 
+0

它比以前更好用了,现在不用做了。但它仍然不会在第二页上正确分裂,它看起来像添加了25个空行,然后打印下一行25行。然后它会创建第3页,因为是第二组线出现。每创建一个新页面,它也会向左移动一点点。这是一个链接,所以你可以看到http://notaryaccounting.com/expencetest1.pdf –

0

我的第一个地方后,把所有的数据一路下滑的页面上所有其他网页的页面想出办法。 在Ryan Vincent答案的帮助下,他帮助我用上面的例子打印了17页脚本。 我仍然有一个问题,它是把所有的数据方式放在页面上。所以,正确的后

if ($pageLine >= $itemsPerPage) { 

     $pdf->AddPage();\ 

我加

$y_axis = 20; //reset Y_axis to move the data back to the top of the NEW page 
         //under the columns 

这是所有花了现在的脚本工作完美

$pdf = new PDF(); 
$pdf->AddPage ($orientation='', $format='', $keepmargins=false, $tocpage=false); 
$pdf->SetPrintHeader(true); 
$pdf->SetPrintFooter(true); 
$pdf->setFontSubsetting(true); 
$pdf->SetAutoPageBreak(FALSE, PDF_MARGIN_BOTTOM); 
// set margins 
$pdf->SetMargins(10, PDF_MARGIN_TOP, 10); 



//set initial y axis position per page 
$y_axis = 20; 

//print column titles 
$pdf->addmileagetext(); 
$pdf->SetFillColor(232,232,232); 
$pdf->SetFont('arial','B',12); 
$pdf->SetY($y_axis); 
$pdf->SetX(10); 
$pdf->Cell(20,6,'Invoice #:',1,0,'L',1); 
$pdf->Cell(20,6,'Date:',1,0,'L',1); 
$pdf->Cell(40,6,'Name:',1,0,'L',1); 
$pdf->Cell(60,6,'Location:',1,0,'L',1); 
$pdf->Cell(15,6,'Start:',1,0,'L',1); 
$pdf->Cell(15,6,'End:',1,0,'L',1); 
$pdf->Cell(20,6,'Mileage:',1,0,'L',1); 

//Set Row Height 
$row_height = 6; 


//Go to next row 

$y_axis = $y_axis + $row_height; 

//initialize counter 
$pageLine = 0; 

//Set maximum rows per page 
$itemsPerPage = 25; 

// 
$result = $db->query("SELECT invoicenumber,signstart,fname,lname,street1,city,state,mstart,mend FROM signings1 WHERE pid = $pid ORDER by signstart"); 
foreach($result as $key => $sig) { 
    //var_dump($key, $sig); 




    //If the current row is the last one, create new page and print column title 
     if ($pageLine >= $itemsPerPage) { 

     $pdf->AddPage(); 
     $pdf->addmileagetext(); 

     $y_axis = 20; //reset Y_axis to move the data back to the top of the NEW page 
         //under the columns 
     //print column titles for the current page 
     $pdf->SetY($y_axis); 
     $pdf->SetFillColor(232,232,232); 
     $pdf->SetFont('arial','B',12); 
     $pdf->SetX(10); 
     $pdf->Cell(20,6,'Invoice #:',1,0,'L',1); 
     $pdf->Cell(20,6,'Date:',1,0,'L',1); 
     $pdf->Cell(40,6,'Name:',1,0,'L',1); 
     $pdf->Cell(60,6,'Location:',1,0,'L',1); 
     $pdf->Cell(15,6,'Start:',1,0,'L',1); 
     $pdf->Cell(15,6,'End:',1,0,'L',1); 
     $pdf->Cell(20,6,'Mileage:',1,0,'L',1); 

     $subTotal = 0; 
     //Go to next row 
     $y_axis = $y_axis + $row_height; 

     //Set $pageLine variable to 0 (first row) 
     $pageLine = 0; 

     } 

     $invoicenumber = $sig['invoicenumber']; 
     $signdate=phpdate($sig['signstart']); 
     $name = $sig['fname'] . ' ' . $sig['lname']; 
     $location = $sig['street1'] . " " . $sig['city'] . " " . $sig['state']; 
     $mstart = $sig['mstart']; 
     $mend = $sig['mend']; 
     $miles = $mend - $mstart; 
     $totalcount = $totalcount + 1; 
     $subTotal += $miles; 
     $totalmoney = $subTotal * .56; 

     $pdf->SetFont('arial','B',9); 
     $pdf->SetFillColor(255,255,255); 
     $pdf->SetY($y_axis); 
     $pdf->SetX(10); 
     $pdf->Cell(20,6,$invoicenumber,1,0,'L',0); 
     $pdf->Cell(20,6,$signdate,1,0,'L',0); 
     $pdf->Cell(40,6,$name,1,0,'L',0); 
     $pdf->Cell(60,6,$location,1,0,'L',0); 
     $pdf->Cell(15,6,$mstart,1,0,'L',0); 
     $pdf->Cell(15,6,$mend,1,0,'L',0); 
     $pdf->Cell(20,6,$miles,1,0,'L',0); 


    //Go to next row 
    $y_axis = $y_axis + $row_height; 
    $pageLine = $pageLine + 1; 




} 

ob_end_clean(); 
$pdf->Output();