jeudi 25 juillet 2019

Getting the word price to show up on every page in laravel snappy pdf

I'm trying to create a pdf that on every page it will have price on the right hand side on top of all the prices. The way I've done it is that each product falls under their respective category and it's in a table format. Now because every category has different amounts of product not all categories end up starting on a new page. I'm also using barryvdh/snappy

Here is my product-pdf.blade.php

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

<body style="font-family: arial;">
  <div style="text-align: center; font-size: 40px;">Product Catalogue</div>

  <div>
    @foreach($product_categories as $category)
      @if(count($category->parentCategory()->where('category_id', '>', 0)->where('active', '1')->get()) && $category->active == 1)
        <div style="text-align: center; font-size: 40px; padding-top: 20px;">
           !!
        </div>

        <table width="100%">
          <tr>
            <td colspan="2"></td>
            <td>Price</td>
          </tr>
        </table>

        @if(count($category->parentCategory))
          @foreach($category->parentCategory as $parent)
            @if(count($parent->parentCategory()->where('category_id', '>', 0)->where('active', 1)->get()) && $parent->active == 1)
              <div style="text-align: center; font-size: 40px; padding-top: 20px;">
                 @@
              </div>

              @if(count($parent->parentCategory))
                @foreach($parent->parentCategory as $child)
                  @if($child->active == 1)
                    <div style="text-align: center; font-size: 40px; padding-top: 20px;">
                       ##
                    </div>


                    <table width="100%">
                      @foreach($child->product as $product)
                        <tr class="page">
                          <td style="border-bottom: 2px solid #bab6b6;">
                            <?php
                              $image = getImagesArray($product->image);
                            ?>

                            @if(!empty($image))
                              <img src={!! asset("product_images/products/$image[0]") !!} width="100">
                            @endif
                          </td>

                          <td style="border-bottom: 2px solid #bab6b6;">
                            <div>
                              
                            </div>

                            <div>
                              
                            </div>
                          </td>

                          <td style="border-bottom: 2px solid #bab6b6; text-align: right;">
                            R 
                          </td>
                        </tr>
                      @endforeach
                    </table>
                  @endif
                @endforeach
              @endif
            @elseif($parent->active == 1)
              <div style="text-align: center; font-size: 40px; padding-top: 20px;">
                 $$

                <table width="100%">
                  @foreach($parent->product as $product)
                    <tr>
                      <td style="border-bottom: 2px solid #bab6b6;">
                        <?php
                          $image = getImagesArray($product->image);
                        ?>

                        @if(!empty($image))
                          <img src={!! asset("product_images/products/$image[0]") !!} width="100">
                        @endif
                      </td>

                      <td style="border-bottom: 2px solid #bab6b6;">
                        <div>
                          
                        </div>

                        <div>
                          
                        </div>
                      </td>

                      <td style="border-bottom: 2px solid #bab6b6; text-align: right;">
                        R 
                      </td>
                    </tr>
                  @endforeach
                </table>
              </div>
            @endif  
          @endforeach
        @endif
      @elseif($category->active == 1 && $category->category_id == 0)
        <div style="text-align: center; font-size: 40px; padding-top: 20px;">
           %%
        </div>

        <table width="100%">
          @foreach($category->product as $product)
            <tr>
              <td style="border-bottom: 2px solid #bab6b6;">
                <?php
                  $image = getImagesArray($product->image);
                ?>

                @if(!empty($image))
                  <img src={!! asset("product_images/products/$image[0]") !!} width="100">
                @endif
              </td>

              <td style="border-bottom: 2px solid #bab6b6;">
                <div>
                  
                </div>

                <div>
                  
                </div>
              </td>

              <td style="border-bottom: 2px solid #bab6b6;">
                R 
              </td>
            </tr>
          @endforeach
        </table>
      @endif
    @endforeach
  </div>
</body>

and here is my downloadPdf function

public function downloadPdf()
{
  $products = Product::all();

  $categories = Category::with('product')->where('active', 1)->where('category_id', '>', '1')->get();

  $product_categories = Category::where('category_id', 0)->with('parentCategory')->get();

  $pdf = PDF::loadView('admin.pdf.product-pdf', compact('categories', 'product_categories'));

  return $pdf->download('catalogue.pdf');
}

I did try to have this

$pdf->setOption('header-html', 'http://laravel-pdf.test/admin/pdf/header');

The exit status code '1' says something went wrong: stderr: "Loading pages (1/6) [> ] 0% [======> ] 10% [==============================> ] 50% [=======================================> ] 65% Error: Failed to load http://laravel-pdf.test/admin/pdf/header, with network status code 299 and http status code 500 - Error downloading http://laravel-pdf.test/admin/pdf/header - server replied: Internal Server Error [============================================================] 100% [======> ] 10% [==============================> ] 50% [============================================================] 100% Counting pages (2/6) [============================================================] Object 1 of 1 Resolving links (4/6) [============================================================] Object 1 of 1 Loading headers and footers (5/6) Printing pages (6/6) [> ] Preparing Done Exit with code 1 due to network error: UnknownContentError " stdout: "" command: "C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf" --lowquality --header-html "http://laravel-pdf.test/admin/pdf/header" "C:\WINDOWS\TEMP\knp_snappy5d3979ce2845b7.57696581.html" "C:\WINDOWS



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire