如何使用FPDF生成fpdf(单个文件)

时间:2016-08-06 13:37:15

标签: php fpdf

使用FPDF生成pdf文件,我想在不同的页面上输出每一行。我怎样才能实现这一切我的所有记录都是从数据库写入文件然后从文件中读取。这是我的剧本

<?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();
?>

1 个答案:

答案 0 :(得分: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();

?>