简体   繁体   English

如何在 Laravel 7 上正确使用 dompdf?

[英]How to use dompdf on Laravel 7 correctly?

I am using barryvdh dompdf for my project, I am also new with laravel framework.我正在为我的项目使用 barryvdh dompdf,我也是 laravel 框架的新手。 I am trying to generate a pdf based on a query that will return one row.我正在尝试根据将返回一行的查询生成 pdf。 The error message is: "Trying to get property 'schedule_date' of non-object".错误消息是:“尝试获取非对象的属性 'schedule_date'”。

This is my function in my controller:这是我的 controller 中的 function:

public function downloadPAasPDF($id)
    {
        $decoded_id = base64_decode($id);

            $data = DB::select('SELECT
             p.appointment_id, 
             d.schedule_date,
             d.time_start,
             d.time_end,
             o.oncology_name,
             pt.procedure_name
            , CONCAT(u1.first_name," ",u1.last_name) AS doctor, CONCAT(u2.first_name," ",u2.last_name) AS patient FROM doctor_schedule d
            LEFT JOIN patient_appointment p ON d.schedule_id = p.schedule_id
            LEFT JOIN users u1 ON u1.id = d.user_id
            LEFT JOIN users u2 ON u2.id = p.user_id
            LEFT JOIN oncology o ON p.oncology_id = o.oncology_id
            LEFT JOIN procedure_type pt ON p.procedure_id = pt.procedure_id
            WHERE p.appointment_id = '.$decoded_id.';');

            $pdf = PDF::loadView('Appointments.PatientAppointmentPDF', ['data' => $data]);
            return $pdf->download('Appointment.pdf');
    }

And this is my view:这是我的观点:

 <table class="table">
                    <thead>
                        <tr>
                        <th style="font-size:21px;">Reference No. {{preg_replace('/[^A-Za-z0-9. ]/', '',$data->schedule_date)}}{{$data->appointment_id}} </th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr>
                            <td style="font-size:23px;width:606px;height:46px;">Patient Name: {{$data->patient}}</td>
                            <td>Date: {{ date_format(date_create($data->schedule_date), 'm-d-Y') }}</td>
                        </tr>
                        <tr style="width:1050px;">
                            <td style="width:345px;">Time Start: {{ date('g:i A',strtotime($data->time_start)) }}</td>
                            <td>Time End: {{date('g:i A',strtotime($data->time_end))}}</td>
                        </tr>
                        <tr style="width:1050px;height:-118px;">
                            <td style="width:321px;height:98px;">Oncology: {{$data->oncology_name}}</td>
                            <td>Procedure: {{$data->procedure_name}}</td>
                        </tr>
                        <tr style="width:1050px;height:-118px;">
                            <td style="width:321px;height:98px;font-size:20px;">Doctor: {{$data->doctor}}</td>
                        </tr>
                    </tbody>
                </table>

Here there are several things to keep in mind, the problem is possible that the query is not returning a result, first you should query directly by inserting that statement in the database manager (navicat or postgresql or phpmyadmin) to avoid errors in the query and verify that returns some tuple, second, you should somewhere in the code verify if the query returns any result, that is, if $data is not null or if it exists for validation reasons in your system and if there are no results, notify the user, on the other side I don't understand why you decode from base64?这里有几件事要记住,问题可能是查询没有返回结果,首先您应该通过在数据库管理器(navicat 或 postgresql 或 phpmyadmin)中插入该语句来直接查询,以避免查询中的错误和验证返回一些元组,其次,您应该在代码中的某处验证查询是否返回任何结果,也就是说,如果 $data 不是 null 或者它是否出于验证原因在您的系统中存在,如果没有结果,请通知用户,另一方面,我不明白您为什么要从 base64 解码? I imagine that the parameter is base64 encoded but I don't know why?我想参数是 base64 编码但我不知道为什么? any security reason?任何安全原因? although occultism is not security... but it could be that in the decoding process you did not obtain the expected result... last but not least I do not remember if it is necessary in the query to add the;虽然神秘主义不是安全...但可能是在解码过程中您没有获得预期的结果...最后但并非最不重要的是我不记得是否有必要在查询中添加; in the case of this type of query with query builder... PD: You could use phpstorm and install the debug to follow the process step by step.在这种使用查询生成器的查询的情况下... PD:您可以使用 phpstorm 并安装调试以逐步执行该过程。 But I guess the problem is in the query.但我想问题出在查询中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM