【问题标题】:Output a pdf from php using a query from mysql使用来自 mysql 的查询从 php 输出 pdf
【发布时间】:2014-04-02 14:18:24
【问题描述】:

我们正在做一个项目,我们需要一个经过过滤和改进的报告。使用数据库中的查询我已经完成了select product,(select avg(numberPer) from ( select product,count(*) as numberPer, monthname(orderdate) as orderMonth, extract(day from orderdate) as orderDay from joborders group by product,orderMonth,orderDay having orderMonth = "March" and product = "Year Book")alias)as averagePerMonth ,monthname(orderdate) as orderMonth from joborders group by product,orderMonth having orderMonth = "March" and product = "Year Book"

现在我们需要以 pdf 格式查看报告,其中所有内容都出现了问题。在对 FPDF 模板中的先前代码进行了一些编辑后,这是我的代码:

<?php
define('FPDF_FONTPATH', 'font/');
require('mysql_table.php');

class PDF extends PDF_MySQL_Table
{
function Header()
{
//Title
$this->SetFont('Arial', '', 18);
$this->Cell(0, 6, 'Job Orders', 0, 1, 'C');
$this->Ln(10);
//Ensure table header is output
parent::Header();
}
}

//Connect to database
include("connect.php");

$pdf=new PDF();
$pdf->Open();
$pdf->AddPage();
//First table: put all columns automatically
$pdf->Table('select product,(select avg(numberPer)  from ( select product,count(*) as          numberPer,
monthname(orderdate) as orderMonth, extract(day from orderdate) as orderDay from joborders 
group by product,orderMonth,orderDay having orderMonth = "March" and product = "Year   Book")alias)as 
averagePerMonth ,monthname(orderdate) as orderMonth from joborders group by  product,orderMonth 
having orderMonth = "March" and product = "Year Book"');
$pdf->Output();
?>

现在我想知道它为什么会出错:

FPDF 错误:部分数据已输出,无法发送 PDF 文件。

【问题讨论】:

  • 确保在 mysql_table.php 或 connect.php 上没有回显
  • 在实例化 PDF 之前,您不能打印/显示/回显任何输出到您的流。
  • @RobertRozas mysql_table.php 和 connect.php 没有回显。mysql_table.php 包含 PDF_MySQL_Table 的类,connect.php 包含 MySQL 连接。
  • 顺便说一句,虽然我很欣赏 JOIN 非常过时,但它们可能在这里起到作用!
  • @Strawberry 我还有一个使用 JOIN 的查询。这只是一个测试,看看它是否可以查看 pdf 报告..

标签: php mysql pdf fpdf


【解决方案1】:

尝试添加这一行:

ob_end_clean();

在它们之间:

$pdf->Table('select product,(select avg(numberPer)  from ( select product,count(*) as           numberPer,
monthname(orderdate) as orderMonth, extract(day from orderdate) as orderDay from joborders 
group by product,orderMonth,orderDay having orderMonth = "March" and product = "Year    Book")alias)as 
averagePerMonth ,monthname(orderdate) as orderMonth from joborders group by  product,orderMonth 
having orderMonth = "March" and product = "Year Book"');
ob_end_clean(); // HERE
$pdf->Output();

【讨论】:

  • @BrainAmplifier18,我很乐意提供帮助;)
猜你喜欢
  • 1970-01-01
  • 2021-04-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多