Laravel 5.4 pdf generate:下载pdf后,在dompdf中找不到图像或输入未知图像

问题描述 投票:1回答:2

我正在尝试使用laravel dom pdf生成pdf。当我下载我的PDF时,它不会显示我的图像。它显示未找到图像或键入未知。但在浏览器中它显示正确。我已经在堆栈中关注了其他相应的问题。我启用了$isRemoteEnabled = true;我该如何解决这个问题?

这是我的pdfview:

<html>
    <head>
    {{--<link rel="stylesheet" href="{{ URL::to('css/style.css') }}">--}}
        <style>
            /*pdf view start*/
            .pdf-content{
                /*pdf body*/
                width: 100%;
                height: 100%;
                background-color: #d3d3d3;
            }
            .pdf-doctor{
                /*for doctor*/
                border-bottom: 1px dashed gray;
            }
            .rx{
                float: left;
                margin-top: 5px;
                width: 100px;
                height:100px;
            }
            .rx img{
                width: 100px;
                height:100px;
            }
            .doctor-info p{
                text-align: center;
            }
            /*{{--for patient start--}}*/
            .pdf-patient{
                margin-top: 7px;
                border-top: 1px dashed gray;
                height: 30px;
                width: 100%;
            }
            .pdf-patient h4{
                float: left;
                display: inline;
                color: #0000cc;
            }
            .pdf-patient-name-date{
                float: left;
                margin-left: 76px;
                margin-top: 7px;
                width: 45%;
            }

            .pdf-test{
                /*for test*/
                margin-top: 50px;
                height: 20em;
                width: 100%;
            }
            .pdf-test h4{
                margin-left: 76px;
                /*display: inline;*/
                color: #0000cc;
            }
            .pdf-test-list{
                border: 1px solid black;
                float: left;
                margin-left: 116px;
                width: 40%;
            }
            .pdf-test-list p{
                margin-left: 15px;
                padding-top: 5px;
            }
            .pdf-test-done{
                border-top: 1px solid black;
                border-right: 1px solid black;
                border-bottom: 1px solid black;
                float: left;
                /*margin-left: 76px;*/
                /*margin-top: 15px;*/
                height: 37px;
                width: 10%;
            }
            .pdf-comments{
                /*for comment*/
                margin-top: 50px;
                height: 30px;
                width: 100%;
            }
            .pdf-comments h3{
                /*for comment*/
                margin-left: 76px;
                /*display: inline;*/
                color: #0000cc;
            }

            /*pdf view end*/
        </style>
    </head>
    <body>
        <div class="pdf-content">
            <div class="pdf-doctor">
                <div class="rx">
                    <img src=" {{ url("/images/rx1.png") }}">
                    {{--{{URL::asset('/image/propic.png')}}--}}
                </div>
                {{--for doctor--}}
                <div class="doctor-info">
                    <p>Doctor's name here</p>
                    <p>Doctor's degree here</p>
                    <p>Doctor's Address here</p>
                    <p>Doctor's Mob here</p>
                </div>

            </div>

            <div class="pdf-patient">
                {{--for patient--}}
                <div class="pdf-patient-name" style="float: left;">
                    <h4 style="display: inline;"><i>Patient name</i>:&nbsp&nbsp</h4>
                    <h4 style="display: inline;">{{ $patients->name }}</h4>
                    {{--style="text-decoration: underline; text-decoration-style: dotted;"--}}
                </div>
                <div class="pdf-patient-date" style="margin-left: 175px;">
                    <h4 style="display: inline;"><i>Date</i>:&nbsp&nbsp</h4>
                    <h4 style="display: inline;">{{ $patients->created_at }}</h4>
                </div>
            </div>
            <div class="pdf-test">
                {{--for test--}}
                <h4>Tests</h4>
                <div style="height: 5px;"></div>
                {{--@foreach($patients as $patient)--}}
                <div class="pdf-test-list">
                    <p>{!! nl2br($patients->test) !!}</p>
                </div>
                {{--<div class="pdf-test-done">--}}
                    {{--<p>tik</p>--}}
                {{--</div>--}}
                {{--@endforeach--}}

            </div>
            <div class="pdf-comments">
                {{--for comment--}}
                <h3 >Comments</h3>
            </div>
        </div>
    </body>
</html>

我的控制器:

public function PdfView(Request $request, $patient_id)
{
   $patients = Patient::where('id', $patient_id)->first();
   $pdf = PDF::loadView('pdfview', ['patients'=>$patients]);
   return $pdf->download('pdfview.pdf');
}

我的图片在public/images/rx1.png,pdfview在views文件夹中。我该如何解决这个问题?

php pdf laravel-5.4
2个回答
1
投票

尝试使用图像的绝对路径,可能还有其他资源,如css和javascripts,如果它们没有加载。

为此,最好有一个专用于pdf导出的布局,其中包含加载资源的绝对路径。

作为替代方案,您可以尝试设置:

isRemoteEnabled: true

在config / dompdf.php中并使用您的普通视图布局。


0
投票

如果图像名称或URL中有空格,则会出现此问题

你可以做到这一点

isRemoteEnabled: true

要么

$image = Image name.jpg; (causing error)
$image = Image%20name.jpg; (is okay)

因此,替换图像名称,其中包含%20的空格,它将被解决。

$image = str_replace(' ', '%20', $items->image);
© www.soinside.com 2019 - 2024. All rights reserved.