duanfei8149 2018-04-28 07:48
浏览 224
已采纳

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

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");
 }
 ?>
  • 写回答

1条回答 默认 最新

  • duanliexi1052 2018-04-28 08:33
    关注

    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");
        }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)