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

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 ansys fluent计算闪退
  • ¥15 有关wireshark抓包的问题
  • ¥15 需要写计算过程,不要写代码,求解答,数据都在图上
  • ¥15 向数据表用newid方式插入GUID问题
  • ¥15 multisim电路设计
  • ¥20 用keil,写代码解决两个问题,用库函数
  • ¥50 ID中开关量采样信号通道、以及程序流程的设计
  • ¥15 U-Mamba/nnunetv2固定随机数种子
  • ¥15 vba使用jmail发送邮件正文里面怎么加图片
  • ¥15 vb6.0如何向数据库中添加自动生成的字段数据。