打印预览不起作用laravel-dompdf [英] Printing preview doesn't working laravel-dompdf

查看:83
本文介绍了打印预览不起作用laravel-dompdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一种用于管理快餐场所的多个分支的应用程序,它位于共享主机中,并尝试使用barryvdh/laravel-dompdf库在热敏打印机(客户端)中打印两张票(一张用于发票,其他用于订单).想法是在发票被注册时,然后显示用于打印两张票证的选项.但是,现在没有显示网络打印预览,仅注册发票并在日志中显示此错误"PrinterController :: pdf尝试转换时传递了无效字符,这些字符已被忽略".应用程序使用Laravel 5.8,php 7.3和laravel-dompdf 0.8.5.我将不胜感激,对此已经困扰了我好几天...

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:

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:

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.');
        }

    }
}

发票视图:

<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>

订单视图:

<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>

推荐答案

您看到的错误

传递了用于转换的无效字符,这些字符已被忽略

Invalid characters passed for attempted conversion, these have been ignored

是一个PHP错误,与解析来自字体指标的内容有关.这个问题(ref #2003 )应该在最近发布的0.8.4中解决.版本的Dompdf.

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.

由于laravel-dompdf需要Dompdf 0.8,因此您应该能够 composer update 安装,并希望问题会消失.

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

这篇关于打印预览不起作用laravel-dompdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆