问题描述
我正在处理一个电子商务 Laravel 项目,该项目要求我在购买结束时上传发票,因此我制作了一张自定义发票以匹配我的公司徽标,当我尝试使用 DOMPDF Wrapper 以 PDF 格式下载它时它一直在加载,直到它最终告诉我 60 秒的最大执行时间已超过我做了一些研究,我认为我的 html 不兼容,因为我使用的是由我的数据库生成的动态变量($facture,$produits)查询,所以有人可以告诉我我该怎么做,因为我无法更改任何格式或从我的发票中删除任何变量。如果问题出在 DOMPDF 包中,您能否给我一个更好的选择来下载我的发票!
这是我的控制器
public function show($id)
{
// retreive all records from db
$facture = Facture::findOrFail($id);
$produits = DB::table('produits')
->join('paniers','paniers.produit_id','=','produits.id')
->select('produits.*',DB::raw('count(paniers.produit_id) as somme ') )
->Where('facture_id',$facture->id)
->groupBy('paniers.produit_id')
->Having(DB::raw('count(paniers.produit_id)'),'>',0)
->get();
// share data to view
$data = [
'facture' => $facture,'produits' => $produits,];
view()->share('front.invoice',$data);
$pdf = PDF::loadView('front.invoice',$data);
// download PDF file with download method
return $pdf->download('ma_facture.pdf');
}
这是我的发票 html
<!DOCTYPE html>
<html lang="fr">
<head>
<Meta charset="utf-8">
<title>Nouha | Ma facture</title>
<!-- Favicon -->
<link rel="apple-touch-icon" sizes="180x180" href="{{ asset('assets/favicon/apple-touch-icon.png') }}">
<link rel="icon" type="image/png" sizes="32x32" href="{{ asset('assets/favicon//favicon-32x32.png') }}">
<link rel="icon" type="image/png" sizes="16x16" href="{{ asset('assets/favicon//favicon-16x16.png') }}">
<link rel="manifest" href="{{ asset('assets/favicon//site.webmanifest') }}">
<link rel="stylesheet" href="{{ asset('assets/front/css/style.css') }}" media="all" />
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<div class="card">
<div class="card-header">
<a class="btn btn-warning" href="{{ URL('factures/'.$facture->id) }}" target="_blank"> <i class="fa fa-print"></i> Export to PDF</a>
</div>
<div class="card-body">
<h1 class="clearfix">
<b>ETS SI-KHALID</b>
<p style="float: center; font-size: 0.45em; line-height: 1.5em;">
Vente en gros de matériels et machines manuelles et automatiques destinées à tous usages<br>
RC : N13/001377932/A/14 *** NIF : 189200101209192<br>
NIS : 19820010120919 *** RIP : 0290031322003990001<br>
Adresse : cité EL KIFFANE,rue Darrar abd el rahmen N°03,TLEMCEN
</p>
</h1>
<h1 style="border-top: 0px; border-bottom: 0px;" class="clearfix">
<small><span style="color: #99154e;">DATE</span><br /> {{ \Carbon\Carbon::parse(Now('Africa/Algiers'))->translatedFormat('d M,Y') }}</small>
<span><b style="color: #99154e;">PROJET : </b> {{$facture->nom}}</span>
<small><span style="color: #99154e;">FACTURE PROFORMAT</span><br /> N° {{ $facture->id }}</small>
</h1>
<div id="details" class="clearfix">
<div id="project">
<div class="arrow"><div class="inner-arrow"><span>CLIENT :</span> {{ Auth::user()->name }} {{ Auth::user()->prenom }}</div></div>
<div class="arrow"><div class="inner-arrow"><span>VILLE :</span> {{ Auth::user()->ville }}</div></div>
</div>
<div id="company">
<div class="arrow back"><div class="inner-arrow">0793 12 06 21 / 0556 33 04 73<span>MOBILE</span></div></div>
<div class="arrow back"><div class="inner-arrow"><a href="mailto:laz-men@hotmail.fr">laz-men@hotmail.fr</a> <span>EMAIL</span></div></div>
</div>
</div>
<table>
<thead>
<tr>
<th class="service">PRODUIT</th>
<th class="desc">DESCRIPTION</th>
<th>PRIX</th>
<th>QNT</th>
<th>TOTAL</th>
</tr>
</thead>
<tbody>
@PHP
$total =0;
@endPHP
@foreach ($produits as $p)
<tr>
<td class="service">{{ $p->nomProduit }}</td>
<td class="desc">{{ $p->description }}</td>
<td class="unit">{{ $p->prix }}</td>
<td class="qty">{{ $p->somme }}</td>
<td class="total">{{ $p->prix * $p->quantite }}</td>
</tr>
@PHP
$total = $total +($p->prix * $p->quantite);
@endPHP
@endforeach
<tr>
<td colspan="4" class="sub">TOTAL H.T</td>
<td class="sub total">{{ $total }}</td>
</tr>
<tr>
<td colspan="4">TVA 19%</td>
<td class="total">{{ ($total*19)/100 }} </td>
</tr>
<tr>
<td colspan="4" class="grand total">TTC</td>
<td class="grand total">{{ (($total*19)/100)*$total }}</td>
</tr>
</tbody>
</table>
<div id="notices">
<p>NOTICE:</p>
<ul>
<li>Facture valable 03 mois.</li>
<li>Délais de livraison: 3 mois.</li>
<li>Garantie : 1 ans.</li>
</ul>
</div>
</div>
<div class="card-footer">
<p>
La facture a été créée sur un ordinateur et est valide sans la signature et le cachet.
</p>
</div>
</div>
</div>
</body>
</html>
这是发票的样子,我希望生成的 pdf 看起来像这样:
解决方法
我建议你使用 laravel-snappy
。如果你使用 windows 来设置二进制文件有点困难。在 linux 中它比 windows 容易。
Snappy 使用 wkhtmltopdf
,这是一个使用 Webkit
呈现页面的二进制文件。
Dompdf 使用 php 来呈现页面。
对 dompdf
使用 simple pages
,基于表格的布局(发票等),对复杂的 HTML 使用 snappy。
此外,如果您正在运行 php artisan serve
,那么有时 dom pdf
会出现 timeout
错误