【问题标题】:Yii2: renderPartial error => error on line 1 at column 1: Document is emptyYii2:renderPartial 错误 => 第 1 行第 1 列错误:文档为空
【发布时间】: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”作为contentPdf 对象,而不是呈现的内容?

标签: layout view yii2 mpdf renderpartial


【解决方案1】:

如果没有正确处理 http-header、document-format 和/或将输出发送到浏览器,则通常会出现此错误消息。

我不知道,return $pdf-&gt;render(); 到底在做什么。但我猜你的问题是发送错误的标头、多次发送标头和/或发送与标头不匹配的输出。

所以你应该考虑一下:

  • 您希望脚本的输出类型是什么?检查是否设置了与输出类型匹配的 http-Header。

  • 检查 $pdf->render();输出比返回值更多的东西。

  • 如果$pdf-&gt;render() 返回一个pdf,你应该把它作为文件发送到浏览器,就像那样(“伪代码”)。使用sendFilesendContentAsFilesendStreamAsFile取决于$pdf-&gt;render()的输出(https://www.yiiframework.com/doc/guide/2.0/en/runtime-responses#sending-files

    return Yii::$app->response->sendContentAsFile( $pdf->渲染(), "下载.pdf", ['内联' => 真, 'mimeType' => '应用程序/pdf']

【讨论】:

    【解决方案2】:

    谢谢大家, 我找到了解决方案。这是要添加:

    Yii::$app->response->format = \yii\web\Response::FORMAT_RAW;
    

    之前:

    $content = Yii::$app->controller->renderPartial('_example');
    

    这是最终代码:

    public function actionPdfTest()
        {
    
            Yii::$app->response->format = \yii\web\Response::FORMAT_RAW;
            // 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(); 
        }
    

    【讨论】:

      猜你喜欢
      • 2011-04-03
      • 1970-01-01
      • 1970-01-01
      • 2023-01-27
      • 2021-02-03
      • 2016-08-15
      • 1970-01-01
      • 2011-12-18
      • 1970-01-01
      相关资源
      最近更新 更多