首页 > 解决方案 > 使用 FPDF 库生成 PDF 的问题

问题描述

我正在尝试使用以下代码生成 PDF 它生成 PDF 但不显示任何内容。完整的文档是空白的。我的查询是正确的,我通过运行检查了它,它给了我正确的数据。非常感谢任何帮助。

 <?php
    require_once("includes/config.php");
    //

    require('C:\xampp\htdocs\geochronology\vendor\setasign\fpdf\fpdf.php');
    require('C:\xampp\htdocs\geochronology\vendor\setasign\fpdi\src\autoload.php');
    require('C:\xampp\htdocs\geochronology\vendor\setasign\fpdi\src\fpdi.php');
        class mypdf extends FPDF{

        function viewTable(){
            $this->setfont('Arial','B',12);
            $search=$_POST['search1'];
            $option=$_POST['option1'];
            $period=$_POST['period1'];
            $datefrom=$_POST['datefrom1'];
            $dateto=$_POST['dateto1'];
            $dbh = new PDO("mysql:host=".DB_HOST.";dbname=".DB_NAME,DB_USER, DB_PASS,array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'"));


                if($period ==null){
                $sql = "SELECT tbluser.user,tbluser.affiliation,tblfacility.type,tblfacility.sampleid,tblfacility.time,DATE_FORMAT(tblfacility.time, '%d-%m-%y') AS formatted_date 
                FROM tblfacility 
                JOIN tbluser on tbluser.id=tblfacility.user 
                where ".$search." ='".$option."' ";}
                else{
                $sql="SELECT * FROM tblfacility JOIN tbluser on tbluser.id=tblfacility.user where ".$search." ='".$option."' AND time between '".$datefrom."' and '".$dateto."' ";
                }
                $query = $dbh->prepare($sql);
                $query->execute();
                $results=$query->fetchAll(PDO::FETCH_OBJ);
                //echo "<prep>";
                //print_r($sql);

                while($results=$query->fetchAll(PDO::FETCH_OBJ)){
                    $this->cell(50,10,$results->user ,1,0,'L');
                    $this->cell(50,10,$results->affiliation,1,0,'L');
                    $this->cell(40,10,$results->type,1,0,'L');
                    $this->cell(30,10,$results->sampleid,1,0,'L');
                    $this->cell(40,10,$results->time,1,0,'L');
                    $this->Ln();
                }

        }

        }
    $pdf = new mypdf();
    $pdf->AliasNbPages();
    $pdf->AddPage('L','A4',0);
    $pdf->viewTable();
    $pdf->output();


    ?>

标签: phphtmlmysqlfpdf

解决方案


下面的代码对我有用:

<?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) {
    // ...
}

推荐阅读