gpt4 book ai didi

php - 使用 FPDF 库生成 PDF 的问题

转载 作者:行者123 更新时间:2023-11-30 21:26:15 25 4
gpt4 key购买 nike

我正在尝试使用以下代码生成 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();


?>

最佳答案

下面这段代码对我有用:

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

关于php - 使用 FPDF 库生成 PDF 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58872727/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com