AbdulBazith's avatar

How to give page numbers for printout in barryvdh/laravel-dompdf

Guys iam working with a project Inventory control

i need to take a print out for the purchase order.

i used barryvdh/laravel-dompdf to make it as pdf and then to take printout.

now the print out is fine.

this is my controller


public function printpdf($id)
    {

        $pur_list_det = PurchaseOrder::where('id', $id)->first();

        $orderlists = OrderList::where ('login_user_id',auth()->id())->where('purchase_order_id',$id)->orderBy('updated_at','desc')->get();

        // return view('Purchase.manage-order-list')->withPur_list_det($pur_list_det)->withOrderlists($orderlists);

        $pdf=PDF::loadView('Purchase.printpdf', [
            'pur_list_det' => $pur_list_det,
            'orderlists'=>$orderlists,
            ]);

            $pdf->setPaper('A4','portrait');

            return $pdf->stream('print.pdf');
    }

and this is my printpdf.blade file


<!DOCTYPE html>

<head>
    <style>
        #design {
            font-family: Serif;
            border-collapse: collapse;
            width: 100%;
        }

        #design td,
        #design th,
        #design thead {
            border: 1px solid #000;
            font-size: 12px;
        }

        #design1 {
            font-family: Serif;
            width: 100%;
        }

        #design1 td,
        #design1 th,
        #design1 thead {
            border: none;
            font-size: 12px;
        }
    </style>
</head>

<body>
    <table id="design1">
        <tr>
            <td><b>Order Date: </b> {{ \Carbon\Carbon::parse( $pur_list_det->order_date)->format('d-m-Y') }}</td>

            <td><b>Order Time: </b> {{ $pur_list_det->order_time }}</td>

            <td><b>Order ID : </b> {{ $pur_list_det->pur_order_num }}</td>

        </tr>

        <tr>
            <td><b>Supplier: </b> {{ $pur_list_det->supplier->sup_name }}</td>

            <td><b>Expected Date: </b> {{ \Carbon\Carbon::parse( $pur_list_det->expec_date)->format('d-m-Y') }}</td>

            <td><b>Expected Time: </b> {{ $pur_list_det->expec_time }}</td>

        </tr>
    </table>

    <caption>
        <h3 align="center"><br>ORDER DETAILS</h3>
    </caption>

    <table id="design">
        <thead>

            <tr>
                <th>S.No</th>
                <th>Product</th>
                <th>Quantity</th>

            </tr>
        </thead>
        <tbody>
            @php ($s_no = 1)

            @foreach($orderlists as $orderlist)
            <tr>

                <td>{{ $s_no }}</td>
                <td>{{ $orderlist->product->product_name }}</td>
                <td>{{ $orderlist->qty }}</td>

            </tr>

            @php ($s_no++)
            @endforeach

        </tbody>
    </table>
</body>

</html>


but what my boss is saying is, he need page number for the printout pages.

how to do this??

and also he need the top portion


<table id="design1">
        <tr>
            <td><b>Order Date: </b> {{ \Carbon\Carbon::parse( $pur_list_det->order_date)->format('d-m-Y') }}</td>

            <td><b>Order Time: </b> {{ $pur_list_det->order_time }}</td>

            <td><b>Order ID : </b> {{ $pur_list_det->pur_order_num }}</td>

        </tr>

        <tr>
            <td><b>Supplier: </b> {{ $pur_list_det->supplier->sup_name }}</td>

            <td><b>Expected Date: </b> {{ \Carbon\Carbon::parse( $pur_list_det->expec_date)->format('d-m-Y') }}</td>

            <td><b>Expected Time: </b> {{ $pur_list_det->expec_time }}</td>

        </tr>
    </table>



the above portion in all pages, because this protion contains the order Id, supplier name, order date, order time, expected date, expected time so these portion must be repeated in all pages how to do that

Kindly reply please

0 likes
3 replies
ettore's avatar

Hey.

What if you split up $orderlists to chunks that is of the size of a page and use the chunk key as page number?

https://laravel.com/docs/5.5/collections#method-chunk

Then every chunk could be a page and you could include your header in the beginning of new page. End then you finish every page with

        <div class="page-break"></div>

with the CSS

        <style>
            .page-break {
                page-break-after: always;
            }
        </style>

I would create the template from the above like "page.blade.php" and run the chunks on the template file

@foreach($chunks as $key => $chunk)
    @include("page", ['page' => $key + 1, $dataset => $chunk]
@endforeach
AbdulBazith's avatar

@ettore thank you so much for your response.

but can you explain some what briefly, please??

ettore's avatar

Cool, what i suggest is that you see how many orders that will fit on a single page. If your page fits 5 orders, then you would split a collection of 20 orders into 4 chunks of 5 orders within each, as that helper function will let you do.

Then you can create your template as i suggested above with you header, body (5 orders) and footer (page number) at choosing.

You can pass into every page the page number as a $page variable that you use (which will be the chunk $key + 1).

Look at this page in the documentation if you are unsure of how to use the include method with variables passed in: https://laravel.com/docs/5.8/blade#including-sub-views

Please or to participate in this conversation.