Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

melx's avatar
Level 4

PRINT PDF, HTML to DomPdf

how can i convert HTML to PDF document, i have tried the below code but it does not convert to pdf its comes with HTML layout

                       // Send data to the view using loadView function of PDF facade
                        $pdf = 
                       PDF::loadView('report.printreport',compact('sales','Expenses',
                            'totalexpenses','totalAmount','customersaleview','totalAmountcustomer'));
                   // If you want to store the generated pdf to the server then you can use the store function
                        $pdf->save(storage_path().'_filename.pdf');
                // Finally, you can download the file using download function
                    return $pdf->download('generalReport.pdf');
0 likes
13 replies
melx's avatar
Level 4

@BEKASKAKI - i have 3 tables i display in one page

    @extends('layouts.master')
      @section('content')
  <div id="content-wrapper" >

      <div class="card shadow mb-4">
  <div class="card-header py-3">
    <h6 class="m-0 font-weight-bold text-primary">General Report Today</h6>
     
   </div>
  <div class="card-body">
    <div class="row float-right">
      <a class="btn btn-danger  " href="{{ URL::to('/printreport/pdf') }}">Export PDF</a>
       </div>
    <hr>

       <!-- dashboard Sumary -->


            <table id="" class="table table-sm table-bordered" style="width:50%">
           <thead>
           <tr>
        <th scope="col">#</th>
        <th scope="col">Discriptions</th>
        <th scope="col">Amount</th>
      </tr>
      </thead>
           <tbody>
      <tr class="table-primary">
        <td>1</td>
        <td>OPEN BALANCE</td>
        <td>{{number_format($totalAmount-$totalexpenses,2)}}</td>
      </tr>
      <tr class="table-success">
        <td>2</td>
        <td>CASH SALES(DAY)</td>
        <td>{{number_format($totalAmount,2)}} Tshs</td>


      </tr>
      <tr>
        <td>3</td>
        <td>CASH RECEIVED(CUSTOMER)</td>
        <td></td>
      </tr>
      <tr class="table-danger">
        <td>4</td>
        <td>EXPENSES (DAY)</td>
        <td>{{number_format($totalexpenses,2)}} Tshs</td>


      </tr>
      <tr class="table-secondary">
       <td>5</td>
       <td>CLOSED BALANCE</td>
       <td>{{number_format($totalAmount-$totalexpenses,2)}}</td>
     </tr>
        </tr>
     </tbody>
    </table>

   <!-- table one -->
         <table id="example" class="table table-bordered" style="width:100%">
      <thead>
  <tr>
    <th scope="col">#</th>
    <th scope="col">Product Name</th>
    <th scope="col">Invoice No</th>

    <th class="text-right">Quantity</th>
    <th class="text-right">Price</th>
    <th class="text-right">Amount</th>
  </tr>
</thead>
<tbody>
 @foreach($sales as $key=>$data)
 <tr>
  <td>{{++$key }}</td>
  <td>{{ $data->product_name }}</td>
  <td>{{$data->address_id}}</td>
  <td>{{$data->quantity}}</td>
  <td>{{$data->price}}</td>
  <td class="text-right">{{number_format($data->totalAmount,2)}} Tshs</td>
</tr>
@endforeach
Grand Total {{number_format($totalAmount,2)}} Tshs
</table>
# Invoice No Customer Name Total Amount Created date @foreach($customersaleview as $key=>$data) {{ ++$key }} {{$data->customer_id}}
  <td>{{ $data->clientName }}</td>
  <td class="text-right">{{number_format($data->totalAmountcustomer,2)}} Tshs</td>
  <td>{{ $data->created_at }}</td>
 </tr>
@endforeach
 </tbody>
 <tr>
<th colspan="4"> Grand Total</th>
<!-- <th ></th> -->
<th class="text-right">{{number_format($totalAmountcustomer,2)}} Tshs</th>
<!-- <th scope="col"></th> -->
<table id="example" class="table  table-bordered" style="width:100%">
    <thead>
     <tr>
        <th scope="col">#ID</th>
      <th scope="col">Description</th>
     <th scope="col">Amount</th>
      <!--  <th class="text-right">created_at</th> -->
       </tr>
     </thead>
       <tbody>
         @foreach ($Expenses as $expns)
  <tr>
    <td>{{ $expns->eid }}</td>
    <td>{{ $expns->description }}</td>
    <td class="text-right">{{number_format($expns->amount,2)}} Tshs</td>
    <!-- <td>{{ $expns->created_at }}</td> -->
    <!-- <td></td> -->
  </tr>
  @endforeach
</tbody>
<tfoot>
  <tr>
 <th colspan="2"> Grand Total</th>
   <!-- <th ></th> -->
   <th class="text-right">{{number_format($totalexpenses,2)}} Tshs</th>
   <!-- <th scope="col"></th> -->
 </tr>
</tfoot>
    </table>
</div>
   </div>

  </div>
@endsection
   @section('scripts')
    @endsection
mushood's avatar

I think the issue come from using blade templating. Try to load a self-contained view, that does not use include or extends

melx's avatar
Level 4

@MUSHOOD - can you show me the sample of to load in self-content

bobbybouwmann's avatar

Just do this instead in your controller

return view('report.printreport',compact('sales', 'Expenses', 'totalexpenses', 'totalAmount', 'customersaleview', 'totalAmountcustomer'));

If you do a GET request this will output the HTML that will be generated for the PDF as well!

bekaskaki's avatar

@EMFINANGA - try this

<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>test</title>

    <style type="text/css">
        @page {
            margin: 0px;
        }
        body {
            margin: 0px;
        }
        * {
            font-family: Verdana, Arial, sans-serif;
        }
        a {
            color: #fff;
            text-decoration: none;
        }
        #customers {
            font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
            border-collapse: collapse;
            width: 100%;
            font-size:12px;
        }
        #customers td, #customers th {
            border: 1px solid #ddd;
            padding: 8px;
            text-align: center;
        }

        tfoot tr td {
            font-weight: bold;
            font-size:12px;
        }
        .invoice table {
            margin: 15px;
        }
        .invoice h3 {
            margin-left: 15px;
        }
        .information {
            background-color: #60A7A6;
            color: #FFF;
            font-size:12px;
        }
        .information .logo {
            margin: 5px;
        }
        .information table {
            padding: 10px;
        }
    </style>

</head>
<body>

    <div class="information">
        <table width="100%">
            <tr>
                <td align="left" style="width: 40%;">
                    <h3>name</h3>
                    <pre>
                        testttt
                        <br /><br />
                        test
                    </pre>

                </td>
                <td align="center">
                    <img src="backend/images/default.jpg" alt="Logo" width="70" class="logo"/>
                </td>
                <td align="right" style="width: 40%;">

                    <h3></h3>
                    <pre>
                        sajfkaskjfas
                    </pre>

                </td>
            </tr>

        </table>
    </div>




    <div class="invoice" style="overflow-x:auto;">
        <h4 align="center" style="font-size: 12px;">testtttt</h4>
        <table id="customers">
            <thead>
                <tr>
                    <th>Keterangan Layanan / Produk</th>
                    <th class="text-center">Harga</th>
                    <th class="text-center">Qty</th>
                    <th class="text-center">Total</th>
                </tr>
            </thead>
            <tbody>
                <tr>

                </tr>

                <tr>

                </tr>


            </tbody>

            <tfoot>
                <tr>
                    <td colspan="2"></td>
                    <td align="left">Sub Total</td>
                    <td align="left" class="gray"></td>
                </tr>
                <tr>
                    <td colspan="2"></td>
                    <td align="left">Grand Total</td>

                    <td align="left" class="gray"></td>
                </tr>
            </tfoot>
        </table>
    </div>

    <div class="information" style="position: absolute; bottom: 0;">
        <table width="100%">
            <tr>
                <td align="left" style="width: 50%;">
                    &copy; {{ date('Y') }} {{ config('app.url') }} - All rights reserved.
                </td>
                <td align="right" style="width: 50%;">
                    Company Slogan
                </td>
            </tr>

        </table>
    </div>
</body>
</html>
melx's avatar
Level 4

i tried this code and it dowload the last pdf when i click export button?

why its coming old download file? and not new file pdf

               $pdf = PDF::loadView('report.printreport',compact('sales','Expenses','totalexpenses',
             'totalAmount','customersaleview','totalAmountcustomer',
          'mikopo','totalmikopo','payment','totalpayment'));
// If you want to store the generated pdf to the server then you can use the store function
$pdf->save(storage_path().'_filename.pdf');
// Finally, you can download the file using download function
return $pdf->download('printreport.pdf');
bekaskaki's avatar

@EMFINANGA - you may have to be validate the pdf file....

i'm use this for download pdf file :

   public function cetak_invoice($id)
    {

        $title = $this->title;
        $transaksi = TransaksiKamar::with('kamar','tamu')->find($id);
        $layanan = TransaksiLayanan::where('transaksi_kamar_id',$transaksi->id)->get();
        $perusahaan = Perusahaan::all();
        $pdf = PDF::loadView('admin.'.$title.'.invoice',compact('title','transaksi','jumlah_hari','layanan','perusahaan'));
         return $pdf->stream();

    }

Please or to participate in this conversation.