Search code examples
phppdfcakephpdompdf

view the pdf on browser dompdf in cakephp


I have create a function php to download pdf in cakephp but I this download works just when I have IDM in my browser, when I disabled IDM I get nothing, this is my code pages: controller function:

public function admin_pdf()
{
 $this->layout="pdf";
 $tpl_dash = array();
 $date = "";
 if(isset($_POST['month']) && isset($_POST['year'])){
  .....
 }
  $this->set('tpl_dash',$tpl_dash);
}

this is my view pdf file:

<div class="container">
<div class="row">
    <div class="gap"></div>
    <div class="product-thumb product-thumb-horizontal">
        <h2 class="pull-left">Statistiques des Etablissements</h2>
        <table  class="table table-hover table-striped table-bordered" id="tpl-dashboard">
            <thead>
                <tr>
                    <th colspan="2" class="first-th"></th>
                    <th colspan="3" class="first-th">Winz</th>
                    <th colspan="3" class="first-th">Réservation</th>
                    <th colspan="3" class="first-th">Avis et Notes</th>
                </tr>
                <tr>
                    <th>Etablissement</th>
                    <th>Ville</th>   
                    <th>Nbre de Winz validés</th>
                    <th>Nbre de Winz consommés</th>
                    <th>Nbre de Winz non consommés</th>
                    <th>Nbre des Réservations validées</th>
                    <th>Nbre des Réservations consommées</th>
                    <th>Nbre des Réservations non consommées</th>
                    <th>Nbre Avis</th>
                    <th>Nbre Notes</th>
                    <th>Nbre Favoris</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($tpl_dash as $key => $value): ?>
                    <tr>
                        <td><?php echo $value['nom'] ?></td>
                        <td><?php echo $value['ville'] ?></td>
                        <td><?php echo $value['avis'] ?></td>
                        <td><?php echo $value['notes'] ?></td>
                        <td><?php echo $value['favoris'] ?></td>
                        <td><?php echo $value['winzsValides'] ?></td>
                        <td><?php echo $value['winzsConsommes'] ?></td>
                        <td><?php echo $value['winzsNonConsommes'] ?></td>
                        <td><?php echo $value['reservationValidees'] ?></td>
                        <td><?php echo $value['reservationConsommees'] ?></td>
                        <td><?php echo $value['reservationNonConsommees'] ?></td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>

and this is my pdf lyaout:

require_once (APP . 'Plugin' . DS . 'dompdf' . DS . 'autoload.inc.php');

$dompdf = new Dompdf\Dompdf();
$dompdf->load_html($this->fetch('content'));
//$dompdf->loadHtml($this->fetch('content'););
// (Optional) Setup the paper size and orientation
$dompdf->setPaper('A4', 'landscape');

// Render the HTML as PDF
$dompdf->render();

// Output the generated PDF to Browser
$dompdf->stream();

I was trying to make this $dompdf->stream('my.pdf',array('Attachment'=>0)); and I was trying to make Attachment on ture but both still not work


Solution

  • I solve the problem, in first script I was working with ajax to download pdf but it working just with IDM.

    function printPdf () {
        var m = document.getElementById("month");
        var y = document.getElementById("year");
        var month = m.options[m.selectedIndex].value;
        var year = y.options[y.selectedIndex].value;
        var monthText = m.options[m.selectedIndex].text;
        var  dataString = 'month='+month+'&year='+year+'&monthText='+monthText;
        $.ajax({
            type: "POST",
            url: "/admin/pages/pdf",
            data: dataString,
            cache: false,
            success: function(result){
    
            }
        });  
    }
    

    if I haven't IDM in my browser I can't download it,But now I delete printPdf() function and I put the pdf buttom generator in form, now the PDF was loaded in browser