创建 PDF 时未应用 dompdf 横向视图

dompdf landscape view not applied when the creating the PDF

我想使用 DOMPDF 创建报告。我的 pdf 创建成功,但问题是我想横向打印它。我给了选项 $pdf->setPaper('A4', 'landscape');//set page size and orientation 但它仍然显示纵向视图。我该如何解决这个问题?

<?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);
        }
      } 
    }
  }
}

这是我已经尝试过的 code.this 工作发现,但唯一的问题是创建纵向报告。

您需要在渲染前设置纸张尺寸。在您调用 render dompdf 之后,已经完成了将 HTML 渲染为 PDF 的大部分繁重工作。

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

行应该放在

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

你可以试试这个代码

    $pdf->setPaper('A4', 'landscape');
or
   $pdf->setPaper('Letter', 'landscape');
or
   $pdf->set_paper("A4", "portrait");
or
  $pdf->set_paper("Letter", "portrait");

除了通过 PHP 定义纸张尺寸,您还可以在 CSS:

中定义它
<style>
  @page {
    size: a4 landscape;
  }
</style>