You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel 8商城使用Dompdf生成订单PDF失败跳转至订单页求助

Laravel 8 订单转PDF时直接跳转到订单列表页问题排查

我正在开发Laravel 8商城项目,尝试将指定订单转换为PDF文件,但未生成PDF页面,而是直接被重定向到订单列表页。view_pdf方法的try块本应成功生成PDF并输出,只有异常时才会进入catch块重定向,但目前直接触发了catch逻辑。

控制器代码

class PdfController extends Controller
{
    public function view_pdf($id){
        Session::put('id', $id);
        try{
            $pdf = \App::make('dompdf.wrapper')->setPaper('a4', 'landscape');
            $pdf->loadHTML($this->convert_orders_data_to_html());

            return $pdf->stream();
        }
        catch(\Exception $e){
            return redirect('/orders')->with('error', $e->getMessage());
        }
    }

    public function convert_orders_data_to_html()
    {
        $orders = Order::where('id',Session::get('id'))->get();

        foreach($orders as $order){
            $first_name = $order->first_name;
            $last_name = $order->last_name;
            $tel = $order->tel;
            $region = $order->region;
            $city = $order->city;
            $hood = $order->hood;
            $street = $order->street;
            $date = $order->created_at;
        }

        $orders->transform(function ($order, $key ) {
                $order->cart = unserialize($order->cart);
                return $order;
            });

        $output = '<link rel="stylesheet" href="frontend/css/style.css">
                        <table class="table">
                            <thead class="thead">
                                <tr class="text-left">
                                    <th>Client Name : '.$first_name && $last_name.'<br> Client Address : '.$region && $city && $hood && $street.' <br> Date : '.$date.'</th>
                                </tr>
                            </thead>
                        </table>
                        <table class="table">
                            <thead class="thead-primary">
                                <tr class="text-center">
                                    <th>Image</th>
                                    <th>Product name</th>
                                    <th>Product size</th>
                                    <th>Price</th>
                                    <th>Quantity</th>
                                    <th>Total</th>
                                </tr>
                            </thead>
                            <tbody>';

        foreach($orders as $order){
            foreach($order->cart->items as $item){

                $output .= '<tr class="text-center">
                                <td class="image-prod"><img src="storage/product_images/'.$item['prod_image'].'" alt="" style = "height: 80px; width: 80px;"></td>
                                <td class="product-name">
                                    <h3>'.$item['prod_name'].'</h3>
                                </td>
                                <td class="product-name">
                                    <h3>'.$item['size'].'</h3>
                                </td>
                                <td class="price">$ '.$item['prod_price'].' XAF</td>
                                <td class="qty">'.$item['prod_qty'].'</td>
                                <td class="total">$ '.$item['prod_price']*$item['prod_qty'].'</td>
                            </tr><!-- END TR-->
                            </tbody>';

            }

            $totalPrice = $order->cart->totalPrice;

        }

        $output .='</table>';

        $output .='<table class="table">
                        <thead class="thead">
                            <tr class="text-center">
                                    <th>Total</th>
                                    <th>$ '.$totalPrice.'</th>
                            </tr>
                        </thead>
                    </table>';

        return $output;
    }
}

路由配置

// PDF conversion
Route::get('/view_pdf/{id}', [PdfController::class, 'view_pdf']);

排查关键点

  • 先获取具体异常信息:临时修改catch块逻辑,直接输出异常内容,比如return response()->json(['error' => $e->getMessage()]);,明确触发异常的具体原因,是数据查询、HTML解析还是DOMpdf本身的问题。
  • 优化订单数据查询逻辑:
    • 无需用Session传递订单ID,直接将$id作为参数传给convert_orders_data_to_html方法更可靠。
    • 单个订单查询改用Order::findOrFail($id),直接获取单个模型,避免集合为空导致后续变量未定义的错误,同时订单不存在时会直接抛出异常便于排查。
  • 修复HTML拼接错误:
    • 字符串拼接时用.替代&&,比如'Client Name : '.$first_name . ' ' . $last_name,&&是逻辑运算符,会导致语法或逻辑错误。
    • 将</tbody>移到商品循环外部,避免HTML结构嵌套错误导致DOMpdf解析失败。
  • 处理资源路径问题:
    • DOMpdf无法直接加载相对路径的CSS文件,建议将CSS样式内联到HTML中,或使用asset()生成绝对URL。
    • 产品图片路径改用asset('storage/product_images/'.$item['prod_image'])生成绝对URL,确保DOMpdf能正确获取图片资源。

内容的提问来源于stack exchange,提问作者C.J

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 09:05:59