PHP,fpdf,文件下载受损

时间:2017-01-23 11:20:01

标签: php file mysqli fpdf

我尝试将一些数据从MYSQLi保存到pdf。文件是通过浏览器下载的,但是当我尝试打开它时,Adobe会喊我"它已经损坏了#34;。这是我的代码:

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

1 个答案:

答案 0 :(得分:1)

现在我创建pdf文件并将其保存到我的服务器。这个文件还可以,我可以打开它并在Adobe中阅读,但是当我尝试将它保存在本地计算机上并打开时,adobe再次喊叫它破碎了。在上面的帖子中是我创建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);