简体   繁体   中英

PHP: Adobe Reader can't open PDF files created with fpdf

I have generated pdf file using fpdf but i'm getting the error. Adobe reader could not open the 'invoice.pdf' because it is either not a supported file type or because the file has been damaged. Any help would be greatly appreciated!

<?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");
 }
 ?>

use $fpdf->Output($file_location,'F'); and remove 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");
    }
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM