Search code examples
phppdfdompdf

dompdf landscape view not applied when the creating the PDF


I want to create the report by using DOMPDF. My pdf is creating successfully, but the problem is that I want print it out in landscape orientation. I gave the option $pdf->setPaper('A4', 'landscape');//set page size and orientation but it is still showing the portrait view. How do I solve this issue?

<?php


if(isset($_GET["action"]))
{
  include('database_connection.php');
  require_once 'pdf.php';
  session_start();

 if($_GET["action"] == "leave_apply_report")
  {
    if(isset($_GET["month"],$_GET["department"]))
    {
     $pdf = new Pdf();
     $currentMonth= date('Y-m');
     $passmonth= $_GET["month"];

        if($currentMonth == $passmonth){
          $strat = date('Y-m-01');
          $end = date('Y-m-d');
        }
        else{
          $strat = date('Y-m-01', strtotime($passmonth));
          $end = date('Y-m-t', strtotime($passmonth));
        }
       $query01 ="SELECT * 
                   FROM department
                   WHERE dept_Id ='".$_GET["department"]."'";
               $statement = $connect->prepare($query01);

               if($statement->execute())
               {  

                $result = $statement->fetchAll();
                foreach($result as $row){
        $header = '<table width="100%" border= 0>';
        $header.='<tr><td style="width:50%"><b>Deprtment   :</b><font size="1"><span> &nbsp;'. $row["dept_Name"] .'</span></font></td>
                  <td style="width:17%"><b>Period    :</b><font size="1"><span><span> &nbsp;'.$strat.' - '.$end.'</span><span></font></td></tr>' ;
        $header .= '</table>';
                }
              }

     $query ="SELECT DISTINCT(emp_id),emp_name,Designation,Department,Section,shift_Id FROM nonacadamic 
              INNER JOIN department 
              ON department.dept_Name = nonacadamic.Department  
              WHERE dept_Id = '".$_GET["department"]."'";

     $statement = $connect->prepare($query);
     if($statement->execute())
     {   
        $result = $statement->fetchAll();
        $headerfix = '<div class="body">
                 <b>University Of Kelaniya &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Course Overtime approval month Report</b><br><br>';


        $table = '<style>
        @page { margin: 20px 30px 40px 50px; }
        #table { border: 1px solid black;
                         font-family: arial, sans-serif;}
        </style>';  
        $table .='<table width="100%" border="1" cellpadding="3" cellspacing="0" id="table">';
        $table .='<tr><th style="text-align: center;width:10%;"><font size="1">Employee Number</font></th><th style="text-align: center;width:30%;"><font size="1">Employee Name</font></th><th style="text-align: center;width:40%;"><font size="1">Designation</font></th><th style="text-align: center;width:10%;"><font size="1">Approved OT</font></th><th style="text-align: center;"><font size="1">Pending OT</font></th></tr>';


            foreach($result as $row)
            {

               $table .='<tr>';
               $table .='<td style="text-align: center;"><font size="1">' . $row["emp_id"] . '</font></td>';
               $table .='<td ><font size="1">' . $row["emp_name"] . '</font></td>';
               $table .='<td ><font size="1">' . $row["Designation"] . '</font></td>';
               $table .='<td ><font size="1">' . ' ' . '</font></td>';
               $table .='<td style="text-align: center;"><font size="1">' . ' ' . ' </font></td>';
               $table .='</tr>';

           }

        $table .='</table>';

      }
            $footer = '<style>
      #footer { position: fixed; right: 0px; bottom: 10px; text-align: center;border-top: 1px solid black;}
        #footer .page:after { content: counter(page, decimal); }
      </style>
       <div id="footer">
                  <p class="page">Page </p>
        </div>';
      $query01 ="SELECT * 
                   FROM department
                   WHERE dept_Id ='".$_GET["department"]."'";
      $statement = $connect->prepare($query01);

      if($statement->execute())
      {  

        $result = $statement->fetchAll();
        foreach($result as $row){
          $department= $row["dept_Name"];

      $file_name = $department.' Course Overtime Approval Report';
      $pdf->loadHtml($headerfix.$header.'<br>'.$table.$footer);//echo $output;
      $pdf->set_option("isPhpEnabled", true);
      $pdf->render();////Render the HTML as PDF
      $pdf->setPaper('A4', 'landscape');//set page size and orientation
      ob_end_clean();//fix the pdf unsupported problem
      $pdf->stream($file_name, array("Attachment" => false));
      exit(0);
        }
      } 
    }
  }
}

this my already try code.this working find but only problem is create the report in portrait.


Solution

  • You need to set the paper size before rendering. After you call render dompdf has already done most of the heavy lifting around rendering the HTML to PDF.

     $pdf->setPaper('A4', 'landscape');//set page size and orientation
    

    line should be place before the

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