首页 > 解决方案 > Laravel dompdf 不显示为视图

问题描述

我正在尝试生成书籍封面,在我的刀片视图中看起来不错,但是当我生成 pdf 时,它看起来不像预览

书前预览

PDF 生成

我的刀片视图:

<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" href="css/style.css">
    <title>Cover</title>
</head>
<body class="bg-cover-blue">
    <div>
        <h1 style="margin-left: 880px;margin-top: 290px;"> {{ $title }}</h1>
        <p style="margin-left: 980px;"> {{ $owner }}</p>
    </div>
</body>
</html>

我的CSS:

.bg-cover-blue{
    background-image: url(../img/covers/blue.jpg);
    background-position: center;
    background-size: cover;
    background-repeat: no-repeat;
}
$data = ['title' => $title, 'owner' => $owner, 'color' => $covercolor,]; $pdf = PDF::loadView('pdf.covertest', $data) ->save(storage_path('../public/pdf/cover/') . 'cover'.$id.'.pdf');

标签: phplaravelpdfdompdf

解决方案


嗨 Ramiro,这可能无法真正解决您的问题,但它肯定会帮助您了解正在发生的事情。

根据https://github.com/barryvdh/laravel-dompdf,我想您可能应该将您的 pdf 方向更改为水平以捕获更多屏幕区域。

我发现很难理解为什么图书馆拒绝捕获整个屏幕而不管方向如何,它就像它针对特定的视口左右,或者可能是因为它是一个图像?好吧,这在文档中没有详细说明。

以下是我能够实现的目标,它非常接近您想要的,并且您可能会喜欢它以节省时间。

在此处输入图像描述

刀片示例代码:

<!DOCTYPE html>
<html style="margin: 0; padding: 0">
<head>
    <title>Cover</title>
</head>
<body class="bg-cover-blue" style="margin: -15%; background-size: cover; background: url('https://i.stack.imgur.com/UWJtU.jpg') no-repeat center;">
<div>
    <h1 style="margin-left: 880px; margin-top: 350px;"> {{ $title }}</h1>
    <p style="margin-left: 980px;"> {{ $owner }}</p>
</div>
</body>
</html>

php示例代码:

function returnView(){

        $data = [
            'owner'=>"Ramiro",
            'title'=>"StackOverflow"
        ];

        $path = "/qrcodes/"."stackoverflow.pdf";

       $pdf = PDF::loadView('email.sample', $data)->setPaper('letter', 'landscape')->save(public_path($path));

        return $pdf->stream("Halloa.pdf");

       //return view('email.sample', $data);
    }

注意:负边距实际上并不重要,没有它仍然会很好看。


推荐阅读