无法使用FPDF和php下载PDF文件

时间:2019-05-22 13:12:27

标签: php mysql fpdf

我需要将数据从MySQL表写入PDF文件,然后使用PHP and MySQL下载。为此,我使用FPDF创建PDF文件,但在我看来,该文件无法正常工作并抛出以下错误。

错误:

<b>Warning</b>:  file_put_contents(report_22-05-2019 18:36:24.pdf): failed to open stream: Permission denied in <b>/var/www/hlwcab.com/public_html/cab/include/libs/fpdf.php</b> on line <b>1021</b><br />
<br />
<b>Fatal error</b>:  Uncaught exception 'Exception' with message 'FPDF error: Unable to create output file: report_22-05-2019 18:36:24.pdf'

我在下面提供我的代码。

require_once '../../../include/libs/fpdf.php';
class PDF extends FPDF{
    // Page header
    function Header(){
        // Logo
        $this->Image('../../../admin/img/HLW-CabManagementLogo.png',10,-1,70);
        $this->SetFont('Arial','B',13);
        // Move to the right
        $this->Cell(80);
        // Title
        $this->Cell(80,10,'Report List',1,0,'C');
        // Line break
        $this->Ln(20);
    }
    // Page footer
    function Footer(){
        // Position at 1.5 cm from bottom
        $this->SetY(-15);
        // Arial italic 8
        $this->SetFont('Arial','I',8);
        // Page number
        $this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'C');
    }

}
if ($_REQUEST["action"]=="downloadMonthlyReportData") {
        $data=array();
        $start_time=$_POST['start_time'];
        $end_time=$_POST['end_time'];
        $sql="select * from cb_wallet_driver_logs order by id desc";
        $stmt = $db->prepare($sql);
        if ($stmt->execute()) {
            $row = $stmt->fetchAll();
            $number_of_rows= count($row);
            if ($number_of_rows > 0) {
                $slno=1;
                foreach ($row as $key => $value) {
                    $last_update=$value['last_update'];
                    $owner_id=$value['owner_id'];
                    $sql="select * from cb_owner_info where owner_id=:owner_id";
                    $stmt = $db->prepare($sql);
                    $stmt->bindParam(':owner_id', htmlspecialchars(strip_tags($owner_id)));
                    if ($stmt->execute()) {
                        $orow = $stmt->fetchAll();
                        $number_of_orows= count($orow);
                        if ($number_of_orows > 0) {
                            foreach ($orow as $key => $val) {
                                $owner_name=$val['name'];
                            }
                        }
                    }
                    $dt = new DateTime($last_update);
                    $odate=$dt->format('d-m-Y');
                    $ndate=date('d-m-Y',strtotime($odate));
                    $stdate=date('d-m-Y',strtotime($start_time));
                    $endate=date('d-m-Y',strtotime($end_time));
                    if ($ndate >= $stdate && $ndate <= $endate) {
                        $data[]=array("sl_no"=>$slno,"owner_name"=>$owner_name,"last_update"=>$last_update,"book_id"=>$value['book_id'],"operator_total"=>$value['operator_total'],"ride_total"=>$value['ride_total']);
                    }
                    $slno++;
                }
            }
        }
        $result1=array();
        if (count($data) > 0) {
            $pdf = new PDF();
            //header
            $pdf->AddPage();
            //foter page
            $pdf->AliasNbPages();
            $pdf->SetFont('Arial','B',12);
            $column[]=array("Sl No","Owner name","Date","Booking ID","Operator Total","Ride Total");
            //print_r($column);exit;
            foreach ($column as $key => $value) {
                $pdf->Cell(40,12,$value,1);
            }
            foreach ($data as $key => $value) {
                $pdf->Ln();
                foreach ($value as $column) {
                    $pdf->Cell(40,12,$column,1);
                }
            }
            $today=date('d-m-Y H:i:s');
            $filename='report_'.$today.'.pdf';
            $pdf->Output($filename, "F");
            $result1["msg"] = "Succesfully Downloaded...";
        }else{
            header("HTTP/1.0 401 Unauthorized");
            $result1["msg"] = "No record to download...";
        }
        //print_r($result1);exit;
        echo json_encode($result1);
    }

在这里,我正在从发生的MySQL中获取数据,并将这些数据写入PDF文件。同样,根据错误,我设置了777权限,但是这些错误仍然会出现。在这里,我需要创建PDF文件并将其下载到浏览器中。

0 个答案:

没有答案