Laravel Dompdf 设置 DPI 从 pdf 中删除图像
Laravel Dompdf Set DPI removes image from pdf
我正在使用 berryvdh/dompdf
生成 PDF,并将其作为电子邮件附件发送:
$pdf = PDF::loadView($view, compact('******'));
$pdf->setOptions(['dpi' => 300 ]);
$output = $pdf->save('pdf/' . $name_pr);
在电子邮件功能中:
$message->attach($file, [
'as' => $reference.'.pdf',
'mime' => 'application/pdf',
]);
在我的 pdf 视图中:
<!DOCTYPE html>
<html>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="content-type">
<style type="text/css">
body{position: relative;}
.pa{
position: absolute;
top: 0;
left: 0;
z-index: 2;
font-family:'Times New Roman';
padding: 0 8px;
font-size: 35px;
height: 42px;
/* background: red */
}
.ref-no{
width: calc(23.6% - 16px);
left: 31.3%;
top: 13.01%;
}
.vrn{
left: 31.3%;
top: 14.9%;
width: 22.8%;
}
.nop{
left: 31.3%;
top: 16.9%;
width: 63.8%;
}
.nro{
left: 31.3%;
top: 18.9%;
width: 63.8%;
}
.cn{
left: 31.3%;
top: 20.6%;
width: 22.8%;
}
.add{
left: 31.3%;
top: 22.6%;
width: 63.8%;
}
.pc{
left: 31.3%;
top: 24.6%;
width: 22.8%;
}
.nic{
left: 31.3%;
top: 26.6%;
width: 22.8%;
}
.en{
left: 31.3%;
top: 28.5%;
width: 22.8%;
}
.ec{
left: 31.3%;
top: 30.3%;
width: 22.8%;
}
.mm{
left: 31.3%;
top: 32.2%;
width: 22.8%;
}
.dr{
left: 31.3%;
top: 34.1%;
width: 22.8%;
}
.from-day{
left: 36.5%;
top: 37%;
width: 4%;
letter-spacing: 30px;
padding-left: 20px;
}
.from-month{
left: 43.5%;
top: 37%;
width: 4%;
letter-spacing: 30px;
padding-left: 20px;
}
.from-year{
left: 50.5%;
top: 37%;
width: 9.5%;
letter-spacing: 35px;
padding-left: 20px;
}
.to-day{
left: 65.6%;
top: 37%;
width: 4%;
letter-spacing: 30px;
padding-left: 20px;
}
.to-month{
left: 72.7%;
top: 37%;
width: 4%;
letter-spacing: 30px;
padding-left: 20px;
}
.to-year{
left: 79.7%;
top: 37%;
width: 9.5%;
letter-spacing: 35px;
padding-left: 20px;
}
.tc{
left: 31.3%;
top: 40.1%;
width: 22.8%;
}
.sticker-sno {
right: 11%;
left: auto;
top: 72.4%;
height: 35px;
font-size: 30px;
width: 14%;
padding-left: 10px;
}
.sticker-rno {
left: auto;
top: 73.8%;
height: 35px;
font-size: 30px;
width: 23%;
right: 10.6%;
}
.sticker-cno {
left: auto;
top: 75.1%;
height: 35px;
font-size: 30px;
width: 23%;
right: 10.6%;
}
.sticker-cp {
left: auto;
top: 76.3%;
height: 35px;
font-size: 30px;
width: 23%;
right: 10.6%;
}
</style>
</head>
<body>
<img src="{{asset('images/td.jpg')}}" alt="" style="width:100%">
<div class="ref-no pa">155dds5544</div>
<div class="vrn pa">A0E4974</div>
<div class="nop pa">Abbas Abdulla</div>
<div class="nro pa">Abbas Abdulla asas sadasd dasdsad</div>
<div class="cn pa">9100074</div>
<div class="add pa">157/85 A, Sassssss, asdawdw, wewdwds, sssdw</div>
<div class="pc pa">2547852</div>
<div class="nic pa">585441154311</div>
<div class="en pa">84561215842131</div>
<div class="ec pa">250cc</div>
<div class="mm pa">TKS2587AS</div>
<div class="dr pa">22/08/2016</div>
<div class="from-day pa">14</div>
<div class="from-month pa">08</div>
<div class="from-year pa">2016</div>
<div class="to-day pa">22</div>
<div class="to-month pa">04</div>
<div class="to-year pa">2025</div>
<div class="tc pa">8049841564</div>
<div class="sticker-sno pa">8049841564</div>
<div class="sticker-rno pa">789456513</div>
<div class="sticker-cno pa">548932115</div>
<div class="sticker-cp pa">10 Years</div>
</body>
</html>
但是当收到电子邮件时图像被删除了。如果没有 $pdf->setOptions(['dpi' => 300 ]);
,pdf 会加载图像,但设置 pdf 对齐方式我需要启用此选项,是否有办法解决这个问题?
谢谢。
我通过调用 public 路径并将图像路径连接到我认为的
来解决这个问题
{{public_path().'/images/td.jpg'}}
感谢所有回答我问题的人
我通过将图像转换为 base64 解决了这个问题。在我的例子中,我只使用了我在 google 上找到的转换器,因为我的图像是静态的,但如果你必须动态转换,你可以使用它
$path = 'myfolder/myimage.png';
$type = pathinfo($path, PATHINFO_EXTENSION);
$data = file_get_contents($path);
$base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);
来源:How to convert an image to base64 encoding?
我正在使用 berryvdh/dompdf
生成 PDF,并将其作为电子邮件附件发送:
$pdf = PDF::loadView($view, compact('******'));
$pdf->setOptions(['dpi' => 300 ]);
$output = $pdf->save('pdf/' . $name_pr);
在电子邮件功能中:
$message->attach($file, [
'as' => $reference.'.pdf',
'mime' => 'application/pdf',
]);
在我的 pdf 视图中:
<!DOCTYPE html>
<html>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="content-type">
<style type="text/css">
body{position: relative;}
.pa{
position: absolute;
top: 0;
left: 0;
z-index: 2;
font-family:'Times New Roman';
padding: 0 8px;
font-size: 35px;
height: 42px;
/* background: red */
}
.ref-no{
width: calc(23.6% - 16px);
left: 31.3%;
top: 13.01%;
}
.vrn{
left: 31.3%;
top: 14.9%;
width: 22.8%;
}
.nop{
left: 31.3%;
top: 16.9%;
width: 63.8%;
}
.nro{
left: 31.3%;
top: 18.9%;
width: 63.8%;
}
.cn{
left: 31.3%;
top: 20.6%;
width: 22.8%;
}
.add{
left: 31.3%;
top: 22.6%;
width: 63.8%;
}
.pc{
left: 31.3%;
top: 24.6%;
width: 22.8%;
}
.nic{
left: 31.3%;
top: 26.6%;
width: 22.8%;
}
.en{
left: 31.3%;
top: 28.5%;
width: 22.8%;
}
.ec{
left: 31.3%;
top: 30.3%;
width: 22.8%;
}
.mm{
left: 31.3%;
top: 32.2%;
width: 22.8%;
}
.dr{
left: 31.3%;
top: 34.1%;
width: 22.8%;
}
.from-day{
left: 36.5%;
top: 37%;
width: 4%;
letter-spacing: 30px;
padding-left: 20px;
}
.from-month{
left: 43.5%;
top: 37%;
width: 4%;
letter-spacing: 30px;
padding-left: 20px;
}
.from-year{
left: 50.5%;
top: 37%;
width: 9.5%;
letter-spacing: 35px;
padding-left: 20px;
}
.to-day{
left: 65.6%;
top: 37%;
width: 4%;
letter-spacing: 30px;
padding-left: 20px;
}
.to-month{
left: 72.7%;
top: 37%;
width: 4%;
letter-spacing: 30px;
padding-left: 20px;
}
.to-year{
left: 79.7%;
top: 37%;
width: 9.5%;
letter-spacing: 35px;
padding-left: 20px;
}
.tc{
left: 31.3%;
top: 40.1%;
width: 22.8%;
}
.sticker-sno {
right: 11%;
left: auto;
top: 72.4%;
height: 35px;
font-size: 30px;
width: 14%;
padding-left: 10px;
}
.sticker-rno {
left: auto;
top: 73.8%;
height: 35px;
font-size: 30px;
width: 23%;
right: 10.6%;
}
.sticker-cno {
left: auto;
top: 75.1%;
height: 35px;
font-size: 30px;
width: 23%;
right: 10.6%;
}
.sticker-cp {
left: auto;
top: 76.3%;
height: 35px;
font-size: 30px;
width: 23%;
right: 10.6%;
}
</style>
</head>
<body>
<img src="{{asset('images/td.jpg')}}" alt="" style="width:100%">
<div class="ref-no pa">155dds5544</div>
<div class="vrn pa">A0E4974</div>
<div class="nop pa">Abbas Abdulla</div>
<div class="nro pa">Abbas Abdulla asas sadasd dasdsad</div>
<div class="cn pa">9100074</div>
<div class="add pa">157/85 A, Sassssss, asdawdw, wewdwds, sssdw</div>
<div class="pc pa">2547852</div>
<div class="nic pa">585441154311</div>
<div class="en pa">84561215842131</div>
<div class="ec pa">250cc</div>
<div class="mm pa">TKS2587AS</div>
<div class="dr pa">22/08/2016</div>
<div class="from-day pa">14</div>
<div class="from-month pa">08</div>
<div class="from-year pa">2016</div>
<div class="to-day pa">22</div>
<div class="to-month pa">04</div>
<div class="to-year pa">2025</div>
<div class="tc pa">8049841564</div>
<div class="sticker-sno pa">8049841564</div>
<div class="sticker-rno pa">789456513</div>
<div class="sticker-cno pa">548932115</div>
<div class="sticker-cp pa">10 Years</div>
</body>
</html>
但是当收到电子邮件时图像被删除了。如果没有 $pdf->setOptions(['dpi' => 300 ]);
,pdf 会加载图像,但设置 pdf 对齐方式我需要启用此选项,是否有办法解决这个问题?
谢谢。
我通过调用 public 路径并将图像路径连接到我认为的
来解决这个问题{{public_path().'/images/td.jpg'}}
感谢所有回答我问题的人
我通过将图像转换为 base64 解决了这个问题。在我的例子中,我只使用了我在 google 上找到的转换器,因为我的图像是静态的,但如果你必须动态转换,你可以使用它
$path = 'myfolder/myimage.png';
$type = pathinfo($path, PATHINFO_EXTENSION);
$data = file_get_contents($path);
$base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);
来源:How to convert an image to base64 encoding?