Printing preview doesn't working laravel-dompdf

烈酒焚心 提交于 2021-01-28 23:56:02

问题


I'm developing an aplication to manage multiples branches for a fast food locale, it's up in a shared hosting, and trying to use barryvdh/laravel-dompdf library to print in a thermal printer (client side) two tickets (one for invoice and other for order). The idea is when the invoice is registered, then show the options to print the two tickets. But right now the web print preview doesn't show, only register the invoice and log show this error 'PrinterController::pdf Invalid characters passed for attempted conversion, these have been ignored'. The aplication uses Laravel 5.8, php 7.3 and laravel-dompdf 0.8.5. I would appreciate any help, I've been stuck for several days with this...

SalesController:

public function store(Request $request){

        try{

            $data = $request->all();
            $client = json_decode($data['client']);
            $till = Till::where('id', session('till'))->first();

            DB::beginTransaction();

            // ============================= INVOICE CREATION ==================================
            $branch_id = Branch::where('id', auth()->user()->branch_id)->pluck('code')->first();
            $user_code = (integer) substr((string) auth()->user()->ci, -3);
            $previous_id = Invoice::whereRaw("id = (select max(id) from invoices where id like '" . $branch_id . $user_code . "%')")->pluck('id')->first();


            if ($previous_id == null) {
                $pre_id = $branch_id . $user_code;
                $id = str_pad($pre_id, 10, "0", STR_PAD_RIGHT);
            }

            if ($previous_id != null)
                $id = $previous_id + 1;


            $invoice = new Invoice();
            $invoice->id = $id;
            $invoice->payment_id = 1;
            $invoice->client_id = $client->id;
            $invoice->received = $data['total_received'];
            $invoice->total = 0;
            $invoice->save();


            // ============================= INVOICE DETAIL ====================================

            $product_detail = $request->session('products')->all();
            $sum = 0;
            foreach ($product_detail['products'] as $item) {
                $detail = new InvoiceDetail();
                $detail->invoice_id = $invoice->id;
                $detail->product_id = $item->id;
                $detail->quantity = $item->quantity;
                $detail->sub_total = (integer) $item->quantity*(integer) $item->price;// modificar a sub total
                $detail->save();

                $sum += $detail->sub_total;

                $actual_stock = Stock::where('product_id', $item->id)->where('branch_id', auth()->user()->branch_id)->first();
                $new_stock = (integer) $actual_stock->quantity - (integer) $item->quantity;

                if ($item->quantity > $actual_stock->quantity)
                    return redirect()->back()->with('error', 'La cantidad solicitada supera el stock disponible');

                $stock = Stock::find($actual_stock->id);
                $stock->quantity = $new_stock;
                $stock->save();

                $stock_history = new StockHistory();
                $stock_history->stock_id = $stock->id;
                $stock_history->product_id = $item->id;
                $stock_history->type = 'sales';
                $stock_history->old_quantity = $actual_stock->quantity;
                $stock_history->new_quantity = $new_stock;
                $stock_history->ext_trans = $invoice->id;
                $stock_history->user_id = auth()->user()->id;
                $stock_history->save();
            }


            // ================================= ORDER =========================================

            $order = new Order();
            $order->invoice_id = $invoice->id;
            $order->status = 0;
            $order->save();


            // ================================= SALES =========================================

            $sales = new Sales();
            $sales->invoice_id = $invoice->id;
            $sales->client_id = auth()->user()->id;
            $sales->till_id = session('till');
            $sales->save();


            // ============================= TILL_TRANSACTION ==================================

            $invoice = Invoice::find($invoice->id);
            $invoice->total = $sum;
            $invoice->save();

            foreach (session('products') as $item) {
                $old_quantity = Stock::where('product_id', $item->id)->where('branch_id', auth()->user()->branch_id)->first();
                $update = Stock::find($old_quantity->id);
                $update->quantity = $old_quantity->quantity - $item->quantity;
                $update->save();
            }


            $till_transaction = new TillTransaction();
            $till_transaction->till_id = session('till');
            $till_transaction->type_id= 3;
            $till_transaction->detail_id = $sales->id;
            $till_transaction->cash_before_op = $till->actual_cash;
            $till_transaction->cash_after_op = $till->actual_cash + $invoice->total;
            $till_transaction->user_id = auth()->user()->id;
            $till_transaction->save();

            DB::commit();

            $branch = Branch::where('id', auth()->user()->branch_id)->pluck('name')->first();
            $change = $invoice->received - $invoice->total;

            $printer = new PrinterController();
            $printer->printPDF($invoice, $product_detail['products'], $branch, $change, $order);

            session(["products"=>[]]);

            return redirect()->back()->with('success', 'Se ha registrado la venta');

        } catch (\Exception $e){
            DB::rollBack();
            Log::error('SalesController::store ' . $e->getMessage(), ['error_line' => $e->getLine()]);
            return redirect()->back()->with('error', 'Oops parece que ocurrio un error, por favor intente nuevamente.');
        }
    }

PrinterController:

namespace App\Http\Controllers;

use Barryvdh\DomPDF\Facade as PDF;
use Illuminate\Support\Facades\Log;

class PrinterController extends Controller
{
    public function printPDF($invoice, $products, $branch, $change, $order){

        try{

            $data = [
                'invoice' => $invoice,
                'product_detail' => $products,
                'branch' =>  $branch,
                'change' => $change,
                'order' => $order
            ];

            $invoice = PDF::loadView('ticket.invoice', $data);
            return $invoice->stream('test.pdf');


        } catch (\Exception $e){
            Log::error('PrinterController::pdf ' . $e->getMessage(), ['error_line' => $e->getLine()]);
            return redirect()->back()->with('error', 'Oops parece que ocurrio un error al imprimir el ticket.');
        } catch (\Throwable $e) {
            Log::error('PrinterController::pdf ' . $e->getMessage(), ['error_line' => $e->getLine()]);
            return redirect()->back()->with('error', 'Oops parece que ocurrio un error al imprimir el ticket.');
        }

    }
}

Invoice view:

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

    <style>
        body {
            font-family: Nunito }

        .detail{
            text-align: end !important;
        }
    </style>
</head>
<body>
<div class="ticket">
    <div>
        <img src="../images/logo.png" style="width: 130px; margin-left: 60px;">
        <h4><strong>Nro de Orden: {{ $order->id }}</strong></h4>
        <br>
        <h5 style="margin-top: -15px"><strong>Nro Factura: {{ $invoice->id }}</strong></h5>
        <h5 style="margin-top: -10px"><strong>Fecha: {{ date( "d/m/Y", strtotime($invoice->created_at)) }} </strong> &nbsp; <strong>Hora: {{ date( "H:i:s", strtotime($invoice->created_at)) }}</strong></h5>

    </div>

    <table>
        <hr>
        <thead>
        <tr style="font-size: 17px">
            <td><strong>Articulo</strong></td>
            <td><strong>Cant.</strong></td>
            <td style="padding-left: 10px"><strong>Precio</strong></td>
            <td style="padding-left: 20px; text-align: right"><strong>Sub Total</strong></td>
        </tr>
        </thead>
        <tbody>

        @foreach($product_detail as $product)
            <tr>
                <td>{{ $product->name  }}</td>
                <td>{{ $product->quantity  }}</td>
                <td style="padding-left: 10px">{{ $product->price }}</td>
                <td style="padding-left: 30px">{{ (integer) $product->quantity * (integer) $product->price  }}</td>
            </tr>
        @endforeach
        </tbody>

        <tfoot>
            <tr class="detail">
                <td colspan="3" style="padding-top: 20px"><strong>Total: </strong></td>
                <td style="padding-top: 20px">{{ $invoice->total }}</td>
            </tr>
            <tr class="detail">
                <td colspan="3"><strong>Recibido: </strong></td>
                <td>{{ $invoice->received }}</td>
            </tr>
            <tr class="detail">
                <td colspan="3"><strong>Su Vuelto: </strong></td>
                <td>{{ $change }}</td>
            </tr>
        </tfoot>
    </table>
    <hr>
    <div style="margin-top: 9px; margin-bottom: -15px">
        <h5 style="margin-bottom: -10px"><strong>Local: {{ $branch  }}</strong></h5>
        <h5><strong>Cajero: {{ auth()->user()->name }}</strong></h5>
        <h4>Gracias por su compra!</h4>
    </div>
    <p>--------------------------------------------</p>
    <p style="font-size: 13px; text-align: center">DOCUMENTO NO VALIDO COMO FACTURA</p>
</div>
</body>
</html>

Order View:

<html>
<head>
    <!-- Styles -->
    <link href="{{ asset('css/app.css') }}" rel="stylesheet">
    <link href="{{ asset('fontawesome/css/all.css') }}" rel="stylesheet">

    <style>
        .ticket {
            width: 283px;
            max-width: 280px;
        }
    </style>
</head>
<body>
<div class="ticket" >
    <table class="table" style="border-top: hidden">
        <caption style="caption-side: top; text-align: center">ORDEN N° 002517</caption>
        <caption style="caption-side: top; margin-top: -20px; margin-bottom: -15px">=============================</caption>
        <caption style="caption-side: bottom; margin-top: -20px; margin-bottom: -15px">=============================</caption>
        <caption style="caption-side: bottom; margin-bottom: -15px">Solicitado: 2019/12/30 -- 22:51:33</caption>
        <thead>
        <tr>
            <th>Art.</th>
            <th>Cant.</th>
        </tr>
        </thead>
        <tbody>
        <tr>
            <td>Producto de prueba</td>
            <td>10</td>
        </tr>
        </tbody>
    </table>
</div>
</body>
</html>

回答1:


The error you're seeing

Invalid characters passed for attempted conversion, these have been ignored

is a PHP error related to parsing content from the font metrics. This issue (ref #2003) should be addressed in the recently-released 0.8.4 version of Dompdf.

Since laravel-dompdf required Dompdf 0.8 you should be able to composer update your install and hopefully see a the issue go away.



来源:https://stackoverflow.com/questions/59772891/printing-preview-doesnt-working-laravel-dompdf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!