使用Dompdf laravel6时,背景图像不会显示在pdf文件中



控制器

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格式显示空白

我建议为此使用javascript库。因为它们在渲染完所有内容后导出您的页面。我在一个项目中使用了jspdf+html2canvas导出证书,效果很好。我最近还找到了一个木偶师,我觉得这太棒了。

jspdf+html2canvas:

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

木偶师:

https://github.com/puppeteer/puppeteer

最新更新