【问题标题】:Why do i have a blank pdf?为什么我有一个空白的pdf?
【发布时间】:2021-10-21 19:22:27
【问题描述】:

我正在尝试生成 pdf 并使用 Laravel-7 显示它,但是当我生成我的 pdf 时它是空白的

这是我的 ProductController 及其 PDF 方法:

public function PDFProducts(){

    $products = Product::all();

    $pdf = \PDF::loadView('pdf-products', compact('products'));
    return $pdf->stream('products.PDFProducts');
}   

这是我的路线:

Route::get('/pdfproduct', 'Backend\ProductController@PDFProducts')->name('products.PDFProducts');

这是我的 pdf 视图,名为 pdf-products.blade.php:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Productos</title>
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
</head>
<body>
<div class="card-body">
    <table id="example1" class="table table-striped table-responsive" width="100%">
        <thead class="thead">

            

            <tr>
                <th style="display: none">Codigo</th>
                <th>ID</th>
                <th>Producto</th>
                <th>Tipo de Producto</th>
                <th>Marca</th>
                <th>Modelo</th>
                <th>Moneda de Compra</th>                                                                       
                <th class="quitarDecimales">Costo Total</th>
                <th>Moneda de Venta</th>
                <th class="quitarDecimales">Precio de Lista</th>
                <th>Margen Bruto</th>
                
                <th style="width: 12%">Accion</th>
            </tr>
        </thead>
        <tbody>
           
            @foreach($products as $product)
            @if($product->status == 1)
                <tr>
                    <td style="display: none">{{ $key+1 }}</td>
                    <td>{{ $product->id }}</td>
                    <td>{{$product->marca->brandName . " " . $product->modelo->modelName}}</td>
                    <td>{{ $product['ptype']['productType']}}</td>
                    <td>{{ $product->marca->brandName }}</td>
                    <td>{{ $product->modelo->modelName}}</td>
                    <td >{{ $product->coin }}</td>                                    
                    <td class="numero">{{ $product->costUSD }}</td>
                    <td>{{$product->sale_coin}}</td>
                    <td class="numero">{{$product->list_priceUSD}}</td>
                    <td class="numero">{{$product->marginUSD}}</td>
                    
                    @php
                    $count_product = App\Model\Purchase::where('product_id',$product->id)->count();
                    @endphp
                        
                </tr>
            @endif

                
            @endforeach
        </tbody>
        
    </table>
 </div><!-- /.card-body -->
 </body>
</html>

在我的 view-products.blade.php 中,我有以下按钮:

<a title="PDF" id="pdf" class="btn btn-sm btn-info btn btn-success" href="{{route('products.PDFProducts')}}" ><i class="fas fa-print" ></i></a>

这就是我的 PDF 的样子:

这是我在检查时在我的开发工具控制台中得到的:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body cz-shortcut-listen="true">
    <div class="container"></div>
</body>
</html>

不是我的h1

【问题讨论】:

  • 你是用products.blade.php还是pdf-products.blade.php来显示pdf?
  • 我改了,结果还是一样。我在 laracast 上回答了你。 @MichalOravec
  • 我编辑了我的问题,知道为什么会这样吗? @MichalOravec

标签: laravel pdf laravel-7


【解决方案1】:

尝试将您的控制器编辑为:

public function PDFProducts(){

    $products = Product::all();

    view()->share('products', $products);
    $pdf = \PDF::loadView('pdf-products', 
    compact('products'));
    return $pdf->stream('products.PDFProducts');
}   

【讨论】:

  • 同样的问题。我认为我在使用引导程序时遇到了问题,并且不知何故不让我展示我的观点。你知道吗?