PHP:Adobe Reader无法打开使用fpdf创建的PDF文件

时间:2018-04-28 07:48:17

标签: php redirect location adobe fpdf

我使用fpdf生成了pdf文件但是我收到了错误。 Adobe Reader无法打开'invoice.pdf',因为它不是受支持的文件类型,或者因为文件已损坏。任何帮助将不胜感激!

<?php 
$f_name=$_POST['first-name'];

if(isset($_POST['submit']))
{   
$f_name=$_POST['name'];
$l_name=$_POST['last-name'];
$email=$_POST['email'];

require("fpdf/fpdf.php");
$fpdf=new FPDF();
$fpdf->AddPage();
$fpdf->setFont("Arial","B",16);
$fpdf->Cell(0,10,"Invoice",1,1,"C");
$fpdf->Cell(95,10,"Name:",1,0,"C");
$fpdf->Cell(95,10,$f_name,1,1,"C");
$fpdf->Cell(95,10,"Last Name:",1,0,"C");
$fpdf->Cell(95,10,$l_name,1,1,"C");
$fpdf->Cell(95,10,"Email:",1,0,"C");
$fpdf->Cell(95,10,$email,1,1,"C");

$file_location = $_SERVER['DOCUMENT_ROOT']."/invoice/up_pdfs/".$f_name.".pdf";
 file_put_contents($file_location,$fpdf); 

ob_clean(); 

$fpdf->output();
 header("Location: index.php");
 }
 ?>

1 个答案:

答案 0 :(得分:1)

使用$fpdf->Output($file_location,'F');并移除file_put_contents($file_location,$fpdf);     

   if(isset($_POST['submit']))
   {   
   $f_name=$_POST['name'];
   $l_name=$_POST['last-name'];
   $email=$_POST['email'];

   require("fpdf/fpdf.php");
   $fpdf=new FPDF();
   $fpdf->AddPage();
   $fpdf->setFont("Arial","B",16);
   $fpdf->Cell(0,10,"Invoice",1,1,"C");
   $fpdf->Cell(95,10,"Name:",1,0,"C");
   $fpdf->Cell(95,10,$f_name,1,1,"C");
   $fpdf->Cell(95,10,"Last Name:",1,0,"C");
   $fpdf->Cell(95,10,$l_name,1,1,"C");
   $fpdf->Cell(95,10,"Email:",1,0,"C");
   $fpdf->Cell(95,10,$email,1,1,"C");

    $file_location = $_SERVER['DOCUMENT_ROOT']."/invoice/up_pdfs/".$f_name.".pdf";

    ob_clean(); 

    $fpdf->Output($file_location,'F');
    header("Location: index.php");
    }
?>
相关问题