0

所以我想将一个pdf文件保存到我本地服务器上的一个目录中,但它一直说该目录不存在。

因此,首先,您将在哪里存储外部无法访问的 PDF 文件(因此不在公共文件夹中)。

所以这是我的代码。下载工作完美。

public function generatePDF()
    {
        $this->mailorder = Session::get('order');
        $this->cart = Session::get('cart');
        $data = [
                    'id' => $this->mailorder->id,
                    'client' => $this->mailorder->Contact,
                    'country' => $this->mailorder->country,
                    'city' => $this->mailorder->city,
                    'street' => $this->mailorder->street,
                    'postal' => $this->mailorder->postal,
                    'phone' => $this->mailorder->phone,
                    'email' => $this->mailorder->email,
                    'dateIn' => $this->mailorder->dateIn,
                    'dateOut' => $this->mailorder->dateOut,
                    'subtotal' => $this->mailorder->subtotal,
                    'tax' => $this->mailorder->tax,
                    'total' => $this->mailorder->total,
                    'cart' => $this->mailorder->cart,
                    'delivery' => $this->mailorder->delivery,
                ];
        $path = "order_{$this->mailorder->id}_{$this->mailorder->Contact}";       
        $pdf = PDF::loadView('pdf.orderConfirmationPdf', $data)->save('storage/app/public/'.$path.'.pdf');
        ;

        return $pdf->download(''.$path.'.pdf');
    }

4

2 回答 2

1

首先,您应该使用 File 门面检查目录是否存在。如果不存在,则必须创建目录。

if(!File::exists($directory_path)) {
    File::makeDirectory($directory_path);
}

如果仍然出现错误,则必须强制它使目录:

if(!File::exists($directory_path)) {

   File::makeDirectory($directory_path, $mode = 0755, true, true);    
}

之后,您可以将文件保存在该目录中。

其次,如果您不想将文件保存在公共目录中。您必须将其保存在存储中。只需调用 storage_path($file_path)。这样,laravel 将文件保存在 storage/app/public 目录下。

之后,您可以根据此答案获取文件的URL 。

于 2018-11-10T12:21:44.457 回答
0

我想通了,谢谢你的回答。

这是我的代码:

public function generatePDF()
    {
        $this->mailorder = Session::get('order');
        $this->cart = Session::get('cart');
        $data = [
                    'id' => $this->mailorder->id,
                    'client' => $this->mailorder->Contact,
                    'country' => $this->mailorder->country,
                    'city' => $this->mailorder->city,
                    'street' => $this->mailorder->street,
                    'postal' => $this->mailorder->postal,
                    'phone' => $this->mailorder->phone,
                    'email' => $this->mailorder->email,
                    'dateIn' => $this->mailorder->dateIn,
                    'dateOut' => $this->mailorder->dateOut,
                    'subtotal' => $this->mailorder->subtotal,
                    'tax' => $this->mailorder->tax,
                    'total' => $this->mailorder->total,
                    'cart' => $this->mailorder->cart,
                    'delivery' => $this->mailorder->delivery,
                ];
        $filename = "order_{$this->mailorder->id}_{$this->mailorder->Contact}";
        $path = storage_path('pdf/orders');

        if(!File::exists($path)) {
            File::makeDirectory($path, $mode = 0755, true, true);

        } 
        else {}

        $pdf = PDF::loadView('pdf.orderConfirmationPdf', $data)->save(''.$path.'/'.$filename.'.pdf');
        ;

        return $pdf->download(''.$filename.'.pdf');
    }

于 2018-11-10T13:42:56.727 回答