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


Create FPDF by code and call it by method

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

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

createPDF.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放在一个方法中,然后返回truefalse(如果是否创建了pdf)。

编辑:只是澄清一下。如果我能得到Output()的回复,那就容易多了。

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

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

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