Michael Rotteveel
Michael Rotteveel

Reputation: 89

yii2 ~ Generate PDF from view

I'm trying to generate a PDF of my view using mPDF. but when I try to use the properties of the model it doesn't work what am I doing wrong?

class FacturenController extends Controller
{

    public function actionMpdfDemo1() {
        $content = $this->renderPartial('factuur');
        $pdf = new Pdf([
            'mode' => Pdf::MODE_UTF8, // leaner size using standard fonts
            'content' => $content,
            'options' => [
                'title' => 'Factuur',
                'subject' => 'Generating PDF files via yii2-mpdf extension has never been easy'
            ],
            'methods' => [
                'SetHeader' => ['Generated By: Krajee Pdf Component||Generated On: ' . date("r")],
                'SetFooter' => ['|Page {PAGENO}|'],
            ]
        ]);
        return $pdf->render();
    }

The PDF is generated via a file called "factuur.php" in there I'm trying to use view items like "company_name" "factuur_id" ect also, is it maybe possible to use a title like 'Factuur' . 'factuur_id'?

<?= DetailView::widget([
        'model' => $model,
        'attributes' => [
            'factuur_id',
            'company.company_name',
            'person.Contactpersoon',
//          'person.last_name',
            'product.product_id',
            'product.product_name',
            'product.amount',
            'product.price',
            'date',
        ],
    ]) ?>

    <?php

use yii\helpers\Html;
use yii\widgets\DetailView;
use app\models\Facturen;
use helpers\ArrayHelper;
use app\models\Producten;
use app\controllers\FacturenController;
/* @var $this yii\web\View */
/* @var $model app\models\Facturen */
/* @var $modelProducten app\models\Producten */

$this->title = 'Factuur: '. $model->factuur_id; ?>
<div class="container">
    <div class="row">
        <div class="col-xs-12">
            <div class="invoice-title">
                <h2><?php $this->title; ?></h2><h3 class="pull-right">Factuurnummer # <?php 'factuur_id' ?></h3>
            </div>
            <hr>
            <div class="row">
                <div class="col-xs-6">
                    <address>
                        <strong>Bedrijsnaam: </strong><?php 'company.company_name' ?><br>
                        <strong>Contactpersoon: </strong><?php'person.Contactpersoon'?><br>
                         <.../>

If you want to know anything else or see images hit me up

~~~~~~EDIT~~~~~~

After using some of the comments I got, I'have a new "error"

Bad Request (#400) Missing required parameters: id

I think it has something to do with this code

class FacturenController extends Controller {

public function actionMpdfDemo1($id) {
  $model = Facturen::findOne($id);
    $content = $this->renderPartial('factuur', [
        'model' => $model,
        'company' => $company,
    ]);

also you guys should know I never worked with a framework before so I'm quite the NOOB

Upvotes: 0

Views: 12169

Answers (1)

Imtiaz
Imtiaz

Reputation: 2524

If you want your view file serve dynamic content, you can pass variables to it using the same way you use render():

    $content = $this->renderPartial('factuur', [
                 'model' => $model,
                 'company' => $company,
                 'person' => $person,
                 // etc...
                 ]);



EDIT [13-Sep-2017]

For your convenience, I'm showing you the entire Controller Action and View code for you. You need to generate a model with the help of Gii from a database table.

Example

Controller Code

    public function actionMpdfDemo1($id) {
        $model = \app\models\Dealer::findOne($id);
        $content = $this->renderPartial('_pdf-dealer', [
                     'model' => $model,
                     // etc...
                     ]);
        $pdf = new \kartik\mpdf\Pdf([
            'mode' => \kartik\mpdf\Pdf::MODE_UTF8, // leaner size using standard fonts
            'content' => $content,
        'cssFile' => '@vendor/kartik-v/yii2-mpdf/assets/kv-mpdf-bootstrap.min.css',
        'cssInline' => '.kv-heading-1{font-size:18px}',
            'options' => [
                'title' => 'Factuur',
                'subject' => 'Generating PDF files via yii2-mpdf extension has never been easy'
            ],
            'methods' => [
                'SetHeader' => ['Generated By: Krajee Pdf Component||Generated On: ' . date("r")],
                'SetFooter' => ['|Page {PAGENO}|'],
            ]
        ]);
        return $pdf->render();
    }

View: _pdf-dealer.php

<?php
use yii\helpers\Html;

?>

<div class="pdf-dealer container">
    <table class="table table-bordered">
        <thead>
            <tr>
                <th>ID</td>
                <th>Name</td>
                <th>Address</td>
                <th>Phone</td>
                <th>District</td>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td><?= $model->id ?></td>
                <td><?= $model->name ?></td>
                <td><?= $model->address ?></td>
                <td><?= $model->phone ?></td>
                <td><?= $model->district ?></td>
            </tr>
        </tbody>
    </table>
</div>

You then generate the PDF file by going to this route similar to this: http://example.com/your-controller/mpdf-demo1?id=1

Upvotes: 1

Related Questions