php - yii2 ~ Generate PDF from view -
i'm trying generate pdf of view using mpdf. when try use properties of model doesn't work 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 generated via file called "factuur.php" in there i'm trying use view items "company_name" "factuur_id" ect also, maybe possible use title '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 want know else or see images hit me up
~~~~~~edit~~~~~~
after using of comments got, i'have new "error"
bad request (#400) missing required parameters: id
i think has code
class facturencontroller extends controller {
public function actionmpdfdemo1($id) { $model = facturen::findone($id); $content = $this->renderpartial('factuur', [ 'model' => $model, 'company' => $company, ]);
also guys should know never worked framework before i'm quite noob
if want view file serve dynamic content, can pass variables using same way use render():
$content = $this->renderpartial('factuur', [ 'model' => $model, 'company' => $company, 'person' => $person, // etc... ]);
edit [13-sep-2017]
for convenience, i'm showing entire controller action , view code you. need generate model of gii 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 generate pdf file going route similar this: http://example.com/your-controller/mpdf-demo1?id=1
Comments
Post a Comment