【问题标题】:DOMPDF Wrapper for Laravel 8 isn't working for my designed InvoiceLaravel 8 的 DOMPDF Wrapper 不适用于我设计的发票
【发布时间】:2021-09-10 23:14:39
【问题描述】:

我正在处理一个电子商务 Laravel 项目,该项目要求我在购买结束时上传发票,因此我制作了一张自定义发票以匹配我的公司徽标,并且当我尝试使用 DOMPDF Wrapper 以 PDF 格式下载它时它一直在加载,直到它最终告诉我超过 60 秒的最大执行时间我做了一些研究,我认为我的 html 不兼容,因为我使用的是由查询,所以有人可以告诉我该怎么做,因为我无法更改任何格式或从发票中删除任何变量。如果问题出在 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>

这是 m 发票的样子,我希望生成的 pdf 看起来像这样:

【问题讨论】:

    标签: laravel invoice export-to-pdf html-to-pdf downloadfile


    【解决方案1】:

    我建议你使用laravel-snappy。如果你使用windows来设置二进制文件有点困难。在linux中它比windows容易。

    Snappy 使用wkhtmltopdf,一个使用Webkit 的二进制文件来呈现页面。 Dompdf 使用 php 来渲染页面。

    dompdf 用于simple pages,基于​​表格的布局(发票等),用于复杂的HTML。

    另外,如果你正在运行php artisan serve,那么有时dom pdf 会给出timeout 错误

    参考: https://github.com/barryvdh/laravel-snappy

    参考:https://github.com/barryvdh/laravel-snappy/issues/226

    【讨论】:

    • 谢谢你的提议,我正在尝试这个包,但我遇到了无法加载的图片和 css 文件的问题,你能告诉我我应该如何声明它们吗?包不会给我一个错误?这是显示的错误:
    • RuntimeException 退出状态码 '1' 表示出了点问题:stderr: "Loading pages (1/6) [> ] 0% [======> ] 10% [== ======> ] 14% 错误:无法加载 maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css,网络状态代码为 3 和 http 状态代码 0 - 找不到主机错误:无法加载 cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.1/css/…,网络状态代码为 3 和 http 状态代码 0 - 找不到主机警告:无法加载 localhost:8000/assets/front/css/factureTemplate.css(忽略)警告:阻止访问文件
    • @youss。我总是使用带有 https 完整 url 的内联 css 和 cdn。有时会给出资产()错误。
    • 好的,谢谢任何方式,我会尝试以某种方式解决。你真好
    猜你喜欢
    • 1970-01-01
    • 2022-07-13
    • 2021-06-26
    • 2022-01-26
    • 2013-08-03
    • 2023-01-12
    • 2013-08-10
    • 2021-04-28
    • 2015-05-18
    相关资源
    最近更新 更多