我正在使用fpdf在php中创建pdf,并且在运行时出现此错误:
Notice: Undefined index: id in C:'xampp1'htdocs'arvin2'public'pdf2'id.php on line 3
FPDF error: Some data has already been output, can't send PDF file
这是我的php代码(报告.php):
<form action="http://localhost/pdf2/id.php" method="post" target="_blank">
<input type="text" name="id">
<input type="submit" name="submitpdf" value="Print"/>
</form>
另一个(ETO.php):
<?php
require('mysql_table.php');
$id=$_POST['id'];
class PDF extends PDF_MySQL_Table
{
function Header()
{
//Title
$this->SetFont('Arial','',18);
$this->Cell(0,6,'Type of Leave',0,1,'C');
$this->Ln(10);
//Ensure table header is output
parent::Header();
}
}
mysql_connect('localhost','root','');
mysql_select_db('auth');
$pdf=new PDF();
//First table: put all columns automatically
$pdf->AddPage();
//Second table: specify 3 columns
$pdf->AddCol('leave_id',20,'','C');
$pdf->AddCol('fullname',40,'Fullname');
$pdf->AddCol('type_of_leave',40,'Type of Leave','R');
$prop=array('HeaderColor'=>array(255,150,100),
'color1'=>array(210,245,255),
'color2'=>array(255,255,210),
'padding'=>2);
$pdf->Table('select leave_id, fullname, type_of_leave from application where id_no="$_POST[id]"',$prop);
$pdf->Output();
?>
当我把:
if(isset($_POST['submitpdf']))
{
$id=$_POST['id'];
//your code goes here
}
错误说:
Parse error: syntax error, unexpected '$pdf' (T_VARIABLE) in C:'xampp1'htdocs'arvin2'public'pdf2'id.php on line 34
输出应该是在pdf表单中打印的文本字段中输入的所有ID号信息。
我遇到了同样的错误。
Data has already been sent to output, unable to output PDF file
这意味着在使用 mPDF 创建 pdf 之前,一些数据存储在发送到浏览器的缓冲区中。因此,它无法创建 PDF。
就这样做..在页面的第一行添加下面的 php 内置函数,您正在为 pdf 准备数据。
op_start();
并在 mPDF 代码之前添加这个 php 内置函数(在你调用 mpdf 之前)
ob_end_flush();
require_once __DIR__ . '/vendor/autoload.php';
$mpdf = new 'Mpdf'Mpdf();
$mpdf->WriteHTML($html);
$mpdf->Output();
这样它将在处理 mPDF 之前清除所有缓冲区输出。
把if isset放在你的代码上。
if(isset($_POST['submitpdf']))
{
//$id=$_POST['id'];
//your code goes here
}
这是为了确保您的代码仅在读取表单数据后运行
编辑:
我想您的新错误来自选择字符串。
$pdf->Table('select leave_id, fullname, type_of_leave from application where id_no="$_POST[id]"',$prop);
将其更改为:
$pdf->Table("select leave_id, fullname, type_of_leave from application where id_no=".$_POST[id],$prop);
您应该在要以pdf格式输出的数据之前使用。
ob_start();
然后使用 ob_end_clean()。
ob_end_clean();
$pdf->Output(time().'.pdf', 'D')
错误: 某些数据已经输出,无法发送 PDF 文件
您可以使用 2 行代码来解决此问题
- 你应该写页面顶部
ob_start()
- 在 PDF 输出行之前 $pdf->Output(); 添加新行
ob_end_flush();
$pdf->输出();
我想你可能会解决这个问题并享受更多!
谢谢
萨拉赫丁汗
兆智科技