Problem with Fpdf in yii 2

0

Hi, I have this problem with fpdf. at the moment of generating the file, it launches this: Instead of the simple hello world that I ask, Here my controller function, the function is actionGenerate:

<?php

namespace backend\controllers;

use Yii;
use app\models\Reportes;
use yii\data\ActiveDataProvider;
use yii\web\Controller;
use rudissaar\fpdf\FPDF;
use yii\web\NotFoundHttpException;
use app\models\RegistroAdq;
use app\models\Adq;
use yii\filters\VerbFilter;

/**
 * ReportesController implements the CRUD actions for Reportes model.
 */
class ReportesController extends Controller
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    /**
     * Lists all Reportes models.
     * @return mixed
     */
    public function actionIndex()
    {
        $dataProvider = new ActiveDataProvider([
            'query' => Reportes::find(),
        ]);

        return $this->render('index', [
            'dataProvider' => $dataProvider,
        ]);
    }


    protected function findModel($id)
    {
        if (($model = Reportes::findOne($id)) !== null) {
            return $model;
        }

        throw new NotFoundHttpException('The requested page does not exist.');
    }

    public function actionGenerar()
    {
            //$fecha_ini=$_POST['fecha_inicio'];
            //$fecha_fin=$_POST['fecha_fin'];
            //
            //$radq=RegistroAdq::find()
            //->where(['between', 'radq_fecha', $fecha_ini, $fecha_fin])
            //->all();
            //
            //foreach ($radq as $dato) {
            //$id=$dato->radq_adq;
            //}

        $pdf = new FPDF();
        $pdf->AddPage();
        $pdf->SetFont('Arial', 'B', 15);
        $pdf->Cell(40, 10, 'Hello World');
        $pdf->Output('I');
    }

}

As you can see, it's a simple hello world, just to make it work and then make my complete pdf. I have already elaborated several but this is the first one I do in yii2, I hope you can help me.

    
asked by Jose Arturo Jimenez Leyva 03.10.2018 в 22:59
source

2 answers

0

Try leaving your layout blank

public function actionGenerar()
{
        //$fecha_ini=$_POST['fecha_inicio'];
        //$fecha_fin=$_POST['fecha_fin'];
        //
        //$radq=RegistroAdq::find()
        //->where(['between', 'radq_fecha', $fecha_ini, $fecha_fin])
        //->all();
        //
        //foreach ($radq as $dato) {
        //$id=$dato->radq_adq;
        //}
    $this->layout = false;
    $pdf = new FPDF();
    $pdf->AddPage();
    $pdf->SetFont('Arial', 'B', 15);
    $pdf->Cell(40, 10, 'Hello World');
    $pdf->Output('I');
}
    
answered by 04.10.2018 в 00:14
0

Apparently the page does not know how to interpret the code you are trying to print, try adding PDF type headers to your method:

header("Content-type: application/pdf");
header("Content-Disposition: inline; filename=filename.pdf");

Your method would look like this:

public function actionGenerar()
    {
        header("Content-type: application/pdf");
        header("Content-Disposition: inline; filename=filename.pdf");

        $pdf = new FPDF();
        $pdf->AddPage();
        $pdf->SetFont('Arial', 'B', 15);
        $pdf->Cell(40, 10, 'Hello World');
        $pdf->Output('I');
    }
    
answered by 04.10.2018 в 00:55