生成pdf时如何解决最小/最大宽度误差domp?

时间:2018-12-24 21:35:03

标签: php html web pdf-generation dompdf

在转换为pdf时,我的代码出现以下错误

不包含任何内联语句,并且为每个表头定义的宽度仍然存在

<?php
//print_invoice.php
if(isset($_GET["pdf"]) && isset($_GET["id"]))
{
    require_once 'pdf.php';
    include('connection2.php');
    $output = '';
    $statement = $connect->prepare("
  SELECT * FROM POrder
  WHERE order_id = :order_id
  LIMIT 1
 ");
    $statement->execute(
    array(
        ':order_id'       =>  $_GET["id"]
    )
);
$result = $statement->fetchAll();
foreach($result as $row)
{
    $output .= '
   <table width="100%" border="1" cellpadding="5" cellspacing="0">
<tr>
 <td colspan="2" align="center" style="font-size:18px"><b>Invoice</b></td>
</tr>
<tr>
 <td colspan="2">
  <table width="100%" cellpadding="5">
   <tr>
    <td width="65%">
     To,<br />
     <b>Vendors Name</b><br />
     Name : '.$row["vendorname"].'<br />
     Description : '.$row["description"].'<br />
    </td>
    <td width="35%">
     Reverse Charge<br />
     Invoice No. : '.$row["order_no"].'<br />
     Invoice Date : '.$row["order_date"].'<br />
    </td>
   </tr>
  </table>
  <br />
  <table width="100%" border="1" cellpadding="5" cellspacing="0">
   <tr>
    <th>Sr No.</th>
    <th>Item Name</th>
    <th>Quantity</th>
    <th>Price</th>
    <th>Actual Amt.</th>
    <th colspan="2">GST (%)</th>
    <th rowspan="2">Total</th>
   </tr>
   <tr>
    <th></th>
    <th></th>
    <th></th>
    <th></th>
    <th></th>
    <th>Rate</th>
    <th>Amt.</th>
   </tr>';
    $statement = $connect->prepare(
            "SELECT * FROM POrder_item
   WHERE order_id = :order_id"
       );
        $statement->execute(
        array(
            ':order_id'       =>  $_GET["id"]
        )
    );
    $item_result = $statement->fetchAll();
    $count = 0;
    foreach($item_result as $sub_row)
    {
            $count++;
            $output .= '
   <tr>
    <td>'.$count.'</td>
    <td>'.$sub_row["item_name"].'</td>
<td>'.$sub_row["item_quantity"].'</td>
<td>'.$sub_row["item_price"].'</td>
<td>'.$sub_row["item_price_bt"].'</td>
<td>'.$sub_row["item_gst"].'</td>
<td>'.$sub_row["item_price_at"].'</td>
    <td>'.$sub_row["final_amount"].'</td>
   </tr>
   ';
        }
        $output .= '
  <tr>
   <td align="right" colspan="11"><b>Total</b></td>
   <td align="right"><b>'.$row["total_after_tax"].'</b></td>
  </tr>
  <tr>
   <td colspan="11"><b>Total Amt. Before Tax :</b></td>
   <td align="right">'.$row["total_before_tax"].'</td>
  </tr>
  <tr>
   <td colspan="11">Add : GST :</td>
   <td align="right">'.$row["gst"].'</td>
  </tr>
   <td colspan="11"><b>Total Tax Amt.  :</b></td>
   <td align="right">'.$row["order_total_tax"].'</td>
  </tr>
  <tr>
   <td colspan="11"><b>Total Amt. After Tax :</b></td>
   <td align="right">'.$row["total_after_tax"].'</td>
  </tr>

  ';
    $output .= '
      </table>
     </td>
   </tr>
   </table>
   ;
    }
    $pdf = new Pdf();
    $file_name = 'Invoice-'.$row["order_no"].'.pdf';
    $pdf->loadHtml($output);
    $pdf->render();
    $pdf->stream($file_name, array("Attachment" => false));
}
?>

// pdf.php

<?php
require_once 'dompdf/autoload.inc.php';
use Dompdf\Dompdf;
class Pdf extends Dompdf{
public function __construct() {
parent::__construct();
}
}
?>

我希望得到pdf文件,但我却收到此错误

  

致命错误:带有消息的未捕获异常'Dompdf \ Exception'   “表行的最小/最大宽度未定义”   /Applications/XAMPP/xamppfiles/htdocs/NTPC/dompdf/src/FrameReflower/TableRow.php:72   堆栈跟踪:#0   /Applications/XAMPP/xamppfiles/htdocs/NTPC/dompdf/src/FrameDecorator/AbstractFrameDecorator.php(903):   Dompdf \ FrameReflower \ TableRow-> get_min_max_width()#1   /Applications/XAMPP/xamppfiles/htdocs/NTPC/dompdf/src/FrameReflower/AbstractFrameReflower.php(268):   Dompdf \ FrameDecorator \ AbstractFrameDecorator-> get_min_max_width()#2   /Applications/XAMPP/xamppfiles/htdocs/NTPC/dompdf/src/FrameDecorator/AbstractFrameDecorator.php(903):   Dompdf \ FrameReflower \ AbstractFrameReflower-> get_min_max_width()#3   /Applications/XAMPP/xamppfiles/htdocs/NTPC/dompdf/src/FrameReflower/AbstractFrameReflower.php(268):   Dompdf \ FrameDecorator \ AbstractFrameDecorator-> get_min_max_width()#4   /Applications/XAMPP/xamppfiles/htdocs/NTPC/dompdf/src/FrameDecorator/AbstractFrameDecorator.php(903):   Dompdf \ FrameReflower \ AbstractFrameReflower-> get_min_max_width在   /Applications/XAMPP/xamppfiles/htdocs/NTPC/dompdf/src/FrameReflower/TableRow.php   在第72行

2 个答案:

答案 0 :(得分:1)

似乎不再支持像您一样包含dompdf,see issue 1153。正在询问的人会得到与您完全相同的错误消息。

我建议您遵循dompdf installation manual并使用composer进行安装(因为从长远来看,这是最轻松的方式)。我还在installing composer on XAMPP上找到了一些东西,但是由于我不了解XAMPP,所以我对此无能为力。作为备用,您可以下载预配置的程序包(在下面的几行中进行介绍)。

还要仔细检查quick start tutorial,看看dompdf是否能正常工作,而不是首先使用您自己的代码,因为其中的某些内容可能已被弃用。

希望这会有所帮助,祝您好运!

答案 1 :(得分:0)

请勿将显示属性应用于表格(非内联样式或外部样式)。

从网络上找到:

在这种情况下,修复最终变得非常简单,它不喜欢我添加到表中的内联样式"type": "dir"。 经过更多测试,我发现它可以使用display:block;display:inline;

这是有道理的,因为表本身具有属性display:inline-block;,并且我认为block可能并不是真正有效(尽管在浏览器中工作正常,这是应用于td元素以创建响应表的巧妙技巧,并且验证期间未生成任何警告。