dsa122870 2019-05-22 13:12
浏览 118

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

I need to write data from MySQL table to PDF file and download using PHP and MySQL. For this I am using FPDF to create PDF file but in my case its not working as expected and throwing the below error.

Error:

<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'

I am providing my code below.

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

Here I am fetching the data from MySQL which is happening and writing those data into PDF file. Also as per error I set the 777 permission but still those errors are coming. Here I need to create the PDF file and download it in the browser.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 基于卷积神经网络的声纹识别
    • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
    • ¥100 为什么这个恒流源电路不能恒流?
    • ¥15 有偿求跨组件数据流路径图
    • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
    • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
    • ¥15 CSAPPattacklab
    • ¥15 一直显示正在等待HID—ISP
    • ¥15 Python turtle 画图
    • ¥15 stm32开发clion时遇到的编译问题