yii2 mpdf render html reached max memory size - yii2

I trying to render a very big pdf with mdpdf on Yii2 framework.
I genereate an html page, but when i call the render function, php run out of memory.
I don't wanna expand the memory_limit ini settings (256M is more than necessary).
I use this configuration, $html contains my huge code:
$pdf = new Pdf([
'mode' => Pdf::MODE_CORE,
'content' => $html,
'options' => [
'title' => 'Report',
],
'marginHeader' => 2,
]);
return $pdf;
Maybe there's a way to render step to step the pdf?

In Yii2 mpdf the content is normally a renderPartial for a form layout
and the renderPartial is populated by one or more models that are the result of query eg:
$models = MyModel::find()->all();
$content = $this->renderPartial('_mpdf_report_scheda', [
'model' => $models,
]);
$pdf = new Pdf([
.......
'content' => $content,
could be that in your case the result of the query retrieve to much rows so you could spliet you content in a part
eg: using limit() and offset()
$models = MyModel::find()
->limit(20)
->all();
$models = MyModel::find()
->limit(20)
.>offset(20)
->all();
and launch the pdf for parts

Related

Yii2 Two different way for breadcrumbs

I have a gii generated view.php page, which can be reached from two different paths, but the breadcrumbs is the same. Anyone know how to fix?
Let me explain: I have the view.php view which shows the summary of the report made and can be reached from two paths:
when I create a new report and therefore the path should be HOME / CREATION / VIEW;
and also from a section that shows the user the summary of the reports she sent and therefore the breadcrumbs should be HOME / SUMMARY / VIEW.
You have 2 options to know where it came from:
Send a query variable on each link or in the redirection to manually build the breadcumb. Like: $value = Yii::$app->request->getQueryParam('breadcumb')
Other option is to get the referrer url. And base on the value you can pass it to the switch. You can get referrer's url using: $value = Yii::$app->request->getReferrer().
Then do a switch to build the link:
switch(value) {
case 'creation':
$label = 'CREATION';
$url = 'url_of_creation';
break;
case 'summary':
$label = 'SUMMARY';
$url = 'url_of_summary';
break;
}
Then just do something like this:
$this->params['breadcrumbs'][] = ['label' => $label, 'url' => $url];
This is a short breadcrum solution using match():
$this->params['breadcrumbs'][] = match($path) {
'creation' => ['label' => 'Creation', 'url' => Url::to['creation'],
'summary' => ['label' => 'Summary', 'url' => Url::to['summary'],
};
$path should either be set in the controller or determined by Yii::$app->request->getReferrer().
Please note, that this requires PHP8.

Yii2 Pagination + PrettyURL Cannot Find site/index

I have pagination setup in site/index, with pretty url working. But my site/index is hidden by either the rewrite engine of Apache, or by UrlManager. In any case, my index page address is simply "X.COM' and pagination wishes to redirect a page change to "X.COM/index?PAGINATIONQUERY", so it always returns a 404.
Example Pagination Request (Returns 404):
x.com/index?page=2&per-page=12
Here is my UrlManager
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
// '<alias:\w+>' => 'site/<alias>',
'<action:\w+>' => 'site/<action>',
],
],
How would I either remove the 'index' portion from pagination requests, or allow myself to see /index in Url again?
Thank you!
Edit:
This is my Index action
public function actionIndex()
{
$query = Shout::find()->orderBy(['id' => SORT_DESC]);
$countQuery = $query->count();
$pagination = new Pagination(['totalCount' => $countQuery, 'pageSize' => 12]);
$shouts = $query->offset($pagination->offset)
->limit($pagination->limit)
->all();
return $this->render('index', [
'shouts' => $shouts,
'pagination' => $pagination,
]);
}
Use Yii2 Gii for Generating Crud Modules with Inbuilt Pagination & Searching.
Yii2 Gii - https://www.yiiframework.com/doc/guide/2.0/en/start-gii
Make Sure Pjax enable when create crud with Gii.

Why my listview not found any record in yii2?

I have a dataprovider to search a world but my listview does not display any record? How can i send this query object to listview?
every thing in my webpage is worked very good but at the output my list view show "not found any result" this mean my list view code is no have problem . problem is in my dataprovider and this query beacuse i customize that
my controller:
$query = new Query();
$dataProvidersearch=new ActiveDataProvider([
'query'=>$query->from('tbl_post')->Where(['like', 'title', $search])-
>andWhere(['like', 'en_title', $search])->andWhere(['like', 'content', $search])->andWhere(['like', 'en_content', $search]),
]);
this is my list view in my view:
$posts = $model->getModels();
echo ListView::widget([
'dataProvider'=>$posts,
'itemView'=>'search',
'summary' => '',
'itemOptions' => [
'tag' => false
],
]);
I'm not sure you have enough code here for someone to help. Even something simple like a listview could consist of a view, a controller, and two model files and your code could be failing at any of these points. You may have simply forgot to include the listview library at the top of your view, but we can't see that in your current example.
What I would recommend is using Gii to generate a listview. It is simple to do and once you have it created, you can study the code to see where you went wrong. You can see how to get started generating code with Gii here: http://www.yiiframework.com/doc-2.0/guide-start-gii.html
ANSWER FROM COMMENTS: Replace andWhere with orWhere, no results are found because no record can match 'title' and 'en_title' and 'content' and 'en_content'.
You are submitting $posts as 'dataProvider' while it should be dataProvidersearch
Instead of:
$posts = $model->getModels();
echo ListView::widget([
'dataProvider'=>$posts,
'itemView'=>'search',
'summary' => '',
'itemOptions' => [
'tag' => false
],
];
Should be:
$posts = $model->getModels();
echo ListView::widget([
'dataProvider'=>$dataProvidersearch,
'itemView'=>'search',
'summary' => '',
'itemOptions' => [
'tag' => false
],
];

Yii2 render database content

There is a textarea where user can edit some templates and can use variables like:
{{model.user.name}}
Application need to replace this variables with data and display HTML output.
We can write a small function that will replace variables from template with data but we need to use a template engine like Twig or Smarty.
https://github.com/yiisoft/yii2-twig
https://github.com/yiisoft/yii2-smarty
Now we can use ViewRenderer from Smarty or Twig.
$render = new ViewRenderer();
$content = $render->render($this->view,'template.twig',[
'model' => $model,
]);
But I see this error:
Unable to find template "template.twig" (looked into: .).
How can I use Smarty or Twig to render a template with the content from database in Yii2 ?
I found Mustache ( https://github.com/bobthecow/mustache.php ) and I'm using like this:
$m = new \Mustache_Engine();
echo $m->render("Hello {{model.client.firma}}",['model' => $model]);
You don't create a renderer manually, you configure your application components, like this:
[
'components' => [
'view' => [
'class' => 'yii\web\View',
'renderers' => [
'twig' => [
'class' => 'yii\twig\ViewRenderer',
'cachePath' => '#runtime/Twig/cache',
// Array of twig options:
'options' => [
'auto_reload' => true,
],
'globals' => ['html' => '\yii\helpers\Html'],
'uses' => ['yii\bootstrap'],
],
// ...
],
],
],
]
You are telling Yii that there is a renderer to handle templates with the .twig extension.
Then, all you need to do is add the .twigextension when calling render in your controller actions:
public function actionIndex()
{
return $this->render('index.twig');
Then put your twig templates in the view folders where the views normally go.
Read the documentation for the twig extension: Twig Extension for Yii 2
If you want to only use twig templates, you can avoid specifying the extension (.twig) if you set the default extension:
'components' => [
'view' => [
'defaultExtension' => 'twig',
You should create a twig component which will wrap around Twig_Environment. You can add yii\twig\Extension extension if you need. Then you should use it like this:
$renderedTemplate = Yii::$app->twig->render($template, $context);
If you really need to render a template from a string variable you may implement your own Twig_LoaderInterface. There is a Twig_Loader_String, but it's deprecated and you should not use it.

html to pdf converter in yii2 with pagination in table

view:
<p>
<?= Html::a('Download This page', ['report'], ['class' => 'btn btn-danger']) ?>
</p>
controller:
public function actionReport()
{
// setup kartik\mpdf\Pdf component
$pdf = new Pdf([
'content' => $content,
'options' => ['title' => 'Krajee Report Title'],
'methods' => [
'SetHeader' => ['Krajee Report Header'],
'SetFooter' => ['{PAGENO}'],
]
]);
return $pdf->render();
}
This function works perfectly but my html table has pagination . so i am confused how to deal with table that has pagination.
You should disable the pagination. it all depends on how you define your data provider (read more about data providers here http://www.yiiframework.com/doc-2.0/guide-output-data-providers.html). Probably you should do something like this
************* = new ActiveDataProvider([
'pagination' => false,
..............
]);
I think you can also call it like
$dataProvider->pagination =false;
Just in case you need to disable it in a specific case.