使用FPDF库生成PDF的问题

时间:2019-11-15 08:07:42

标签: php html mysql fpdf

我正尝试使用以下代码生成PDF,它将生成PDF,但未显示任何内容。完整文档为空。我的查询是正确的,我已经通过运行对其进行了检查,它为我提供了正确的数据。高度赞赏。

@angular/material

1 个答案:

答案 0 :(得分:0)

下面的代码对我有用:

<?php

require_once 'vendor/autoload.php';

class MyPdf extends FPDF
{
    public function viewTable()
    {
        $this->setFont('Arial', 'B', 12);

        $results = [
            (object) [
                'user' => 'Isaac Skelton',
                'affiliation' => 'None',
                'type' => 'Some Type',
                'sampleid' => 5,
                'time' => '15-11-2019',
            ],
            (object) [
                'user' => 'Joe King',
                'affiliation' => 'None',
                'type' => 'Some Other Type',
                'sampleid' => 6,
                'time' => '10-10-2018',
            ],
        ];

        foreach ($results as $result) {
            $this->cell(50, 10, $result->user, 1, 0, 'L');
            $this->cell(50, 10, $result->affiliation, 1, 0, 'L');
            $this->cell(40, 10, $result->type, 1, 0, 'L');
            $this->cell(30, 10, $result->sampleid, 1, 0, 'L');
            $this->cell(40, 10, $result->time, 1, 0, 'L');
            $this->ln();
        }
    }
}

$pdf = new MyPdf();
$pdf->aliasNbPages();
$pdf->addPage('L', 'A4', 0);
$pdf->viewTable();
$pdf->output();

我刚刚注意到,这可能与您的while循环有关。您已经写过

<?php

$results = $query->fetchAll(PDO::FETCH_OBJ);

// And then right after
while ($results = $query->fetchAll(PDO::FETCH_OBJ)) {
    // ...
}

// Did you want this instead?
$results = $query->fetchAll(PDO::FETCH_OBJ);

foreach ($results as $result) {
    // ...
}