2016-08-06 234 views
1

我正在使用FPDF生成PDF文件,并且想要在不同页面上输出每行。我怎么能实现这一切我的记录是从数据库写入文件,然后从文件中读取。这里是我的脚本如何使用FPDF生成fpdf(单个文件)

<?php 
require 'include/function.php'; 

error_reporting(1); 
$filename = "other_file/uploadsum .txt"; 
$File = $filename; 
$Handle = fopen($File, 'w'); 
$sn = 1; 
$name = ''; 
$sn = 1; 
$fetch = mysql_query("SELECT * FROM lga ORDER BY lga_id ASC"); 

while ($row = mysql_fetch_row($fetch)) 
    { 
    $center_num = array(
     $row[1] 
    ); 
    $sch_name = $row[2]; 
    $total_forSubject1 = number_format($total_); 
    $name.= $sn++ . "* " . $sch_name . "* $total_forSubject1" . "* $uploaded" . "* $balance" . "\r\n"; 
    } 

fwrite($Handle, $name); 
fclose($Handle); 
class pdf extends FPDF 

    { 
    function Header() 
     { 
     $this->SetFont('Arial', 'B', 15); 
     $this->Cell(80); 
     $this->Ln(10); 
     $this->Ln(20); 
     } 

    function Footer() 
     { 
     $this->SetY(-15); 
     $this->SetFont('Arial', 'I', 9); 
     $this->Cell(0, 10, 'Page ' . $this->PageNo() , 0, 0, 'C'); 
     } 

    function LoadData($file) 
     { 
     $lines = file($file); 
     $data = array(); 
     foreach($lines as $line) 
      { 
      $data[] = explode('*', trim($line)); 
      } 

     return $data; 
     } 

    function CTable($header, $data) 
     { 
     $this->SetFillColor(255); 
     $this->SetTextColor(0); 
     $this->SetDrawColor(0, 0, 0); 
     $this->SetLineWidth(.3); 
     $this->SetFont('Arial', 'B', 9); 
     $w = array(
      10, 
      120 
     ); 
     for ($i = 0; $i < count($header); $i++) $this->Cell($w[$i], 7, $header[$i], 1, 0, 'C', true); 
     $this->Ln(); 
     $this->SetFillColor(224, 235, 255); 
     $this->SetTextColor(0); 
     $this->SetFont(''); 

     // Data 

     $fill = false; 
     $a = 1; 
     foreach($data as $row) 
      { 
      $b = $a++; 
      $this->Cell($w[0], 6, $row[0], 1, 0, 'C'); 
      $this->Cell($w[1], 6, $row[1], 1, 'C'); 
      $this->Ln(); 
      $fill = !$fill; 
      } 

     $this->Cell(array_sum($w) , 0, '', 'T'); 
     } 
    } 

$pdf = new PDF(); 
$pdf->SetAutoPageBreak(true, 10); 
$header = array(
    'S/N', 
    'Local Govt Area.' 
); 
$data = $pdf->LoadData($filename); 
$pdf->SetFont('Arial', '', 12); 
$pdf->AddPage(); 
$pdf->CTable($header, $data); 
$pdf->Output(); 
?> 

回答

0
<?php 
require 'include/function.php'; 

error_reporting(1); 
$filename = "other_file/uploadsum .txt"; 
$File = $filename; 
$Handle = fopen($File, 'w'); 
$sn = 1; 
$name = ''; 
$sn = 1; 
$fetch = mysql_query("SELECT * FROM lga ORDER BY lga_id ASC"); 

while ($row = mysql_fetch_row($fetch)) 
    { 
    $center_num = array(
     $row[1] 
    ); 
    $sch_name = $row[2]; 
    $total_forSubject1 = number_format($total_); 
    $name.= $sn++ . "* " . $sch_name . "* $total_forSubject1" . "* $uploaded" . "* $balance" . "\r\n"; 
    } 

fwrite($Handle, $name); 
fclose($Handle); 
class pdf extends FPDF 

    { 
    function Header() 
     { 
     $this->SetFont('Arial', 'B', 15); 
     $this->Cell(80); 
     $this->Ln(10); 
     $this->Ln(20); 
     } 

    function Footer() 
     { 
     $this->SetY(-15); 
     $this->SetFont('Arial', 'I', 9); 
     $this->Cell(0, 10, 'Page ' . $this->PageNo() , 0, 0, 'C'); 
     } 

    function LoadData($file) 
     { 
     $lines = file($file); 
     $data = array(); 
     foreach($lines as $line) 
      { 
      $data[] = explode('*', trim($line)); 
      } 

     return $data; 
     } 

    function CTable($header, $data) 
     { 
     $this->SetFillColor(255); 
     $this->SetTextColor(0); 
     $this->SetDrawColor(0, 0, 0); 
     $this->SetLineWidth(.3); 
     $this->SetFont('Arial', 'B', 9); 
     $w = array(
      10, 
      120 
     ); 
     for ($i = 0; $i < count($header); $i++) $this->Cell($w[$i], 7, $header[$i], 1, 0, 'C', true); 
     $this->Ln(); 
     $this->SetFillColor(224, 235, 255); 
     $this->SetTextColor(0); 
     $this->SetFont(''); 

     // Data 

     $fill = false; 
     $a = 1; 
     foreach($data as $row) 
      { 
      $b = $a++; 
      $this->Cell($w[0], 6, $row[0], 1, 0, 'C'); 
      $this->Cell($w[1], 6, $row[1], 1, 'C'); 
      $this->Ln(); 
      $fill = !$fill; 
      } 

     $this->Cell(array_sum($w) , 0, '', 'T'); 
     } 
    } 


$textfile = fopen($File, 'w'); 
// Foreach new line in the text file. 
$pdf = new PDF(); 
$pdf->SetAutoPageBreak(true, 10); 
$header = array(
    'S/N', 
    'Local Govt Area.' 
); 
$pdf->SetFont('Arial', '', 12); 

foreach(preg_split("/((\r?\n)|(\r\n?))/", $textfile) as $line){ 

    $pdf->AddPage(); 
    $pdf->CTable($header, $line); 

} 

$pdf->Output(); 

?> 
+0

我尝试过,但似乎不工作,可你想试试我吗? –