代码之家  ›  专栏  ›  技术社区  ›  Andy

通过代码创建FPDF并通过方法调用它

  •  0
  • Andy  · 技术社区  · 10 年前

    在我的项目中,我使用 fpdf 以基于来自数据库的数据生成PDF文件。 然而,我想通过调用一些创建和保存文件的方法来预生成pdf。 保存数据.php

    ($myPDF->createPDF($id)){
     echo 'File was created';
    }else{
     echo 'There was a problem creating the file';
    }
    

    创建PDF.php

    //Code that generate the PDF using FPDF and at end save the file to server
    $pdf->Output('/var/www/html/my_dir/my_pdf.pdf','F');
    

    所以基本上我想放置createPDF。php,然后返回 true false 是否创建了pdf。

    编辑: 只是想澄清一下。如果我能从 Output() 这会容易得多。

    我应该继续并检查文件是否存在吗?

    1 回复  |  直到 10 年前
        1
  •  0
  •   maxhb    10 年前

    如果您只想知道文件是否已成功保存到光盘:

    function createPdf($pdf, $filename) {
      // remove file if it already exists
      if(file_exists($filename)) unlink($filename);
    
      // save pdf
      $pdf->output($filename, 'F');
    
      // return true if successfull, false otherwise
      return file_exists($filename);
    }