如何使用从MySQL数据库生成的fpdf在pdf表中进行换行

时间:2019-05-22 10:03:21

标签: php mysql fpdf

我在较旧的帖子中找到了此代码。除了一个问题,它对我的​​项目来说绝对正常。如果来自mysql db的字符串太长,它将使单元溢出。我希望它自动中断并在特定单元格中开始新行。

我从以下网址获得了原始帖子:Basic table creation fpdf

<?php
require('fpdf.php');

class People {
    public function all() {
        try {
            $db = new PDO('mysql:host=localhost;dbname=test;charset=UTF-8', 'user', 'password');
            $query = $db->prepare("SELECT first_name, middle_name, last_name, age, email FROM people ");
            $query->execute();
            $people = $query->fetchAll(PDO::FETCH_ASSOC);
        } catch (PDOException $e) {
            //echo "Exeption: " .$e->getMessage();
            $result = false;
        }
        $query = null;
        $db = null;
        return $people;        
    }
}

class PeoplePDF extends FPDF {
    // Create basic table
    public function CreateTable($header, $data)
    {
        // Header
        $this->SetFillColor(0);
        $this->SetTextColor(255);
        $this->SetFont('','B');
        foreach ($header as $col) {
            //Cell(float w [, float h [, string txt [, mixed border [, int ln [, string align [, boolean fill [, mixed link]]]]]]])
            $this->Cell($col[1], 10, $col[0], 1, 0, 'L', true);
        }
        $this->Ln();
        // Data
        $this->SetFillColor(255);
        $this->SetTextColor(0);
        $this->SetFont('');
        foreach ($data as $row)
        {
            $i = 0;
            foreach ($row as $field) {
                $this->Cell($header[$i][1], 6, $field, 1, 0, 'L', true);
                $i++;
            }
            $this->Ln();
        }
    }
}

// Column headings
$header = array(
             array('First Name',  30), 
             array('Middle Name', 30), 
             array('Last Name',   30),
             array('Age',         12),
             array('Email',       47)
          );
// Get data
$people = new People();
$data = $people->all();

$pdf = new PeoplePDF();
$pdf->SetFont('Arial', '', 12);
$pdf->AddPage();
$pdf->CreateTable($header,$data);
$pdf->Output();

0 个答案:

没有答案