【发布时间】:2021-11-08 21:23:21
【问题描述】:
我在 Yii 2 上使用 Krajee mdpf 工具(http://demos.krajee.com/mpdf)。我尝试生成一个视图,并成功使用了 render() 函数。问题是它显示了一个布局。所以我决定使用 renderPartial() 函数只显示视图的 htlm 但我得到了这个错误:
This page contains the following errors:
error on line 1 at column 1: Document is empty
error on line 1 at column 1: Encoding error
Below is a rendering of the page up to the first error.
这是我的 actionPdfTest 函数:
public function actionPdfTest()
{
// get your HTML raw content without any layouts or scripts
$content = Yii::$app->controller->renderPartial('_example');
// setup kartik\mpdf\Pdf component
$pdf = new Pdf([
// set to use core fonts only
'mode' => Pdf::MODE_CORE,
// A4 paper format
'format' => Pdf::FORMAT_A4,
// portrait orientation
'orientation' => Pdf::ORIENT_PORTRAIT,
// stream to browser inline
'destination' => Pdf::DEST_BROWSER,
// your html content input
'content' => $content,
// format content from your own css file if needed or use the
// enhanced bootstrap css built by Krajee for mPDF formatting
'cssFile' => '@vendor/kartik-v/yii2-mpdf/src/assets/kv-mpdf-bootstrap.min.css',
// any css to be embedded if required
'cssInline' => '.kv-heading-1{font-size:18px}',
// set mPDF properties on the fly
'options' => ['title' => 'Krajee Report Title'],
// call mPDF methods on the fly
'methods' => [
'SetHeader'=>['Krajee Report Header'],
'SetFooter'=>['Krajee Report Footer'],
]
]);
// return the pdf output as per the destination setting
return $pdf->render();
}
_example 视图代码:
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
/* @var $this yii\web\View */
/* @var $form yii\widgets\ActiveForm */
/* @var $model app\models\LoginForm */
$this->title = 'Hello World';
?>
<h1><?= Html::encode($this->title) ?></h1>
你能帮帮我吗?
【问题讨论】:
-
1.在将其发送到
Pdf对象之前,您是否尝试过echo $content?这个变量不是空的吗? 2. 您是否尝试过提供例如“123”作为content到Pdf对象,而不是呈现的内容?
标签: layout view yii2 mpdf renderpartial