打印预览不起作用 laravel-dompdf



我正在开发一个应用程序来管理快餐场所的多个分支,它在一个共享主机中,并尝试使用 barryvdh/laravel-dompdf 库在热敏打印机(客户端(中打印两张票(一张用于发票,另一张用于订单(。这个想法是在注册发票时,然后显示打印两张票的选项。但是现在没有显示Web打印预览,仅注册发票和日志显示此错误"打印机控制器::p df尝试转换传递的无效字符,这些已被忽略"。该应用程序使用 Laravel 5.8、php 7.3 和 laravel-dompdf 0.8.5。 我将不胜感激任何帮助,我已经为此困了好几天了......

销售总监:

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

打印机控制器:

namespace AppHttpControllers;
use BarryvdhDomPDFFacade as PDF;
use IlluminateSupportFacadesLog;
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>

您看到的错误

尝试转换时传递的无效字符,这些字符已被忽略

是与解析字体指标中的内容相关的 PHP 错误。这个问题(ref #2003(应该在最近发布的Dompdf的0.8.4版本中得到解决。

由于 laravel-dompdf 需要 Dompdf 0.8,您应该能够composer update安装,并希望看到问题消失。

最新更新