SamC
SamC

Reputation: 21

Yii2: renderPartial error => error on line 1 at column 1: Document is empty

I am using Krajee mdpf tool(http://demos.krajee.com/mpdf) on Yii 2. I tried to generate a view and I succeed with the render() function. The problem is that it displays a layout. So I decided to use renderPartial() function to only display the htlm of the view but I get this error:


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. 

Here is my actionPdfTest function:

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

Code of _example view:



    <?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>

Please Can you help me?

Upvotes: 0

Views: 341

Answers (2)

SamC
SamC

Reputation: 21

Thank you every body, I found the solution. It was to add :

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

before the:

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

Here is the final code:

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

Upvotes: 1

The Bndr
The Bndr

Reputation: 13394

this error Message mostly appears, if there is no correct handling of http-header, document-format and/or sending output to the browser.

I don't know, what return $pdf->render(); exactly is doing. But I guess your problem is a mix of sending wrong header, sending header multiple times and/or sending output that doesn't matches the header.

So you should think about:

  • What output-type you expect for your script? Check, if you set the http-Header that matched your output type.

  • Check if $pdf->render(); outputs something more than the return value.

  • If $pdf->render() returns an pdf, you should send this as file to the browser, like that ("pseudo-code"). Using sendFile, sendContentAsFile or sendStreamAsFile depends on the output of $pdf->render() ( https://www.yiiframework.com/doc/guide/2.0/en/runtime-responses#sending-files )

    return Yii::$app->response->sendContentAsFile( $pdf->render(), "download.pdf", ['inline' => true, 'mimeType' => 'application/pdf']

Upvotes: 0

Related Questions