【问题标题】:Background image is not showing in pdf file when using Dompdf laravel6使用 Dompdf laravel 6 时,背景图像未显示在 pdf 文件中
【发布时间】:2021-02-03 17:18:54
【问题描述】:

控制器

use PDF;
class PdfController extends Controller
{
    public function index(){
       return view('pdf/web',compact('data'))
    }
    public function generate(){
        $pdf = \App::make('dompdf.wrapper');
        $pdf -> loadView('pdf/web',compact('data'));
        return $pdf->stream();
    }
}

web.blade.php

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="charset=utf-8" />
<meta charset="UTF-8">
<title>100001</title>
<style type="text/css">
   
    @page {
        size: A4;
        margin-top:0.5cm;
        margin-bottom:0;
        margin-left:0;
        margin-right:0;
        padding: 0;
    }
    .firstdiv{
        display: flex;
        flex-direction: column;
        text-align: center;
        justify-content: center;
        align-items: center;
        font-family: Cambria, Georgia, serif;
        position: relative;
        margin: auto;
        width: 1200px;
        height: 900px;
        background-image: url("/img/bgimg.png");
        background-repeat: no-repeat;
        background-size: 100% 100%;
    }
</style>
</head>
<body>
    <div class="firstdiv">
          #CONTENTS
          #CONTENTS
          #CONTENTS
          #CONTENTS
    </div>

我面临两种类型的问题

  1. 当我打开网页时,一切正常。对于 dompdf,背景图像未显示在 pdf 文件中。如需更多许可:我正在尝试查看网页并从中创建 pdf。当我使用控制器的索引功能打开网页时。我设计的一切都运行良好。但 pdf 文件仅以内容打开,不显示背景图片。
  2. 我的设计宽度为 1000 像素,高度为 630 像素。所以我想要横向模式的PDF。但是 pdf 以纵向模式出现。该部分的其余部分显示 pdf 中的空白。

【问题讨论】:

    标签: php html laravel laravel-6 dompdf


    【解决方案1】:

    我建议为此使用 javascript 库。因为他们在渲染所有内容后导出您的页面。我在项目中使用 jspdf+html2canvas 导出认证,效果很好。我最近还发现了我认为很棒的 puppeteer。

    jspdf+html2canvas:

    https://jsfiddle.net/boLxkgj8/14/
    

    木偶师:

    https://github.com/puppeteer/puppeteer
    

    【讨论】:

    猜你喜欢
    • 2013-05-28
    • 2012-01-28
    • 1970-01-01
    • 2015-12-11
    • 2019-08-08
    • 1970-01-01
    • 2016-08-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多