PHP, fpdf, 损坏的文件下载

PHP, fpdf, damaged file download

我尝试将一些数据从 MYSQLi 保存到 pdf。文件是通过浏览器下载的,但是当我尝试打开它时,Adobe 对我大喊"It's damaged"。这是我的代码:

ob_start();
require('fpdf.php');
$pdf = new FPDF();
$pdf->AddPage('A4');
$pdf->SetFont('Arial','',16);
$line="Użytkownik: ".$login." Miesiąc: ".$miesiac." Dzień: ".$dzien;
$pdf->Cell(0,5,$line);
$query="select * from ".$prefix."_Przychody where Login='$login'";
$start=mysqli_query($link, $query);
while($dane=mysqli_fetch_assoc($start))
{
    $data=date("Y-m-").$dzien;
    if($dane['Data']==$data)
    {
        $line="Opis: ".$dane['Opis']." Kategoria: ".$dane['Kategoria_przychodu']." Kwota: ".$dane['Kwota'].".";
        $pdf->Cell(0,5,$line);
        $y = $pdf->GetY();
        $pdf->SetXY(0,$y+10);
    }
}

$query="select * from ".$prefix."_Wydatki where Login='$login'";
$start=mysqli_query($link, $query);
while($dane=mysqli_fetch_assoc($start))
{
    $data=date("Y-m-").$dzien;
    if($dane['Data']==$data)
    {
        $line="Opis: ".$dane['Opis']." Kategoria: ".$dane['Kategoria_przychodu']." Kwota: ".$dane['Kwota'].".";
        $pdf->Cell(0,5,$line);
        $y = $pdf->GetY();
        $pdf->SetXY(0,$y+10);

    }
}
$name=$login.".pdf";
$pdf->Output('D',$name,true);   
ob_end_flush(); 

顺便说一句。我尝试将数据保存到某个日期,很抱歉使用波兰语名称,但这不是我的数据库 :D

现在我创建 pdf 文件并将其保存到我的服务器。这个文件没问题,我可以打开它并在 Adob​​e 中阅读,但是当我尝试将它保存在本地计算机上并打开时,adobe 再次告诉我它坏了。在上面的 post 中是我创建 pdf 文件的代码,这是我将它从服务器下载到本地计算机的代码:

if($zapis==1)
{
$file = $login.".pdf";
if(!$file){
header('Location: index.php?w=laczne_podsumowanie');
}
else
{
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$file");
header("Content-Type: application/pdf");
header("Content-Transfer-Encoding: binary");
readfile($file);
}
}

现在我如何编辑代码来下载未损坏的 pdf 文件。 我将不胜感激任何答案。 和平:)

编辑:我通过使用修复这个:

flush();
ob_clean();

之前:

readfile($file);