PHP - Yii2 MPDF无法呈现表格

问题描述 投票:0回答:1

我在Yii2中使用mpdf扩展来生成pdf(http://www.yiiframework.com/extension/yii2-mpdf/

代码如下。

$fileName = 'tst.pdf';
    $invoiceHtml = "
    <table style = 'height:500px;width:500px' border='1'>
      <tr>
        <td>Test
        </td>
        <td>Test2
        </td>
      </tr>
        <tr>
          <td>Test
          </td>
          <td>Test2
          </td>
        </tr>
    <table>
    ";
    $pdf = new Pdf([
         // set to use core fonts only
         'mode' => Pdf::MODE_CORE,
         // A4 paper format
         'orientation' => Pdf::ORIENT_LANDSCAPE,
         // stream to browser inline
         'destination' => Pdf::DEST_DOWNLOAD,
         // your html content input
         'content' => $invoiceHtml,
         'filename' =>$fileName,
         'cssInline' => ' @page{size: 500mm 200mm}',

          // set mPDF properties on the fly
         'options' => ['title' => 'Krajee Report Title'],
          // call mPDF methods on the fly
         'methods' => [
             'SetHeader'=>['New Horizon Travel And Tours LLC'],
         ]
   ]);
   return $pdf->render();

代码生成空白pdf文件。我尝试过其他HTML标记和其他标记,纯文本按预期工作。当使用表标签时,它正在工作。寻找这个问题的解决方案

php yii2 mpdf
1个回答
0
投票

这可能不是您问题的答案。但我之前已经面临同样的问题了。如果你坚持,你可以使用我的替代方案......

1 - 将您的Html代码放在HTML文件中......单独一个。 2 - 使用下面的代码来代替你的代码。

$invoiceHtml = Yii::$app->controller->renderPartial('_yourHtmlFile');
$pdf = new Pdf([
     // set to use core fonts only
     'mode' => Pdf::MODE_CORE,
     // A4 paper format
     'orientation' => Pdf::ORIENT_LANDSCAPE,
     // stream to browser inline
     'destination' => Pdf::DEST_DOWNLOAD,
     // your html content input
     'content' => $invoiceHtml,
     'filename' =>$fileName,
     'cssInline' => ' @page{size: 500mm 200mm}',

      // set mPDF properties on the fly
     'options' => ['title' => 'Krajee Report Title'],
      // call mPDF methods on the fly
     'methods' => [
         'SetHeader'=>['New Horizon Travel And Tours LLC'],
     ]
]);
return $pdf->render();
© www.soinside.com 2019 - 2024. All rights reserved.