Fixing html to pdf using dompdf (CSS) - html

I am trying to convert the html page to a pdf file. I have no problem using $view->display();. The css format are displaying properly in here. However, when I try to use the $dompdf instead, the css seems to be a mess and different. I need some help here
public function testPDF(){
$view = View::loadView('agentpanel/invoice_header.php');
$invoice_view = View::loadView('agentpanel/invoice_view.php');
$company = Company::fetch(1);
$client = Client::fetch(1);
$invoice = Invoice::fetch(1);
$invoice_view->addData(['company' => $company,
'client' => $client,
'template' => $company->invoiceTemplate,
'availableProducts' => [],
'availableForms' => [],
'sales' => $invoice->sales->all(true),
'inv' => $invoice,
'mode' => 'view'
]);
$invoice_view->set('date', date("M, d Y",time()));
$view->set('invoice_view', $invoice_view->render());
$view->display();
}
dompdf
public function testPDF1(){
$view = View::loadView('agentpanel/invoice_header.php');
$invoice_view = View::loadView('agentpanel/invoice_view.php');
$company = Company::fetch(1);
$client = Client::fetch(1);
$invoice = Invoice::fetch(1);
$invoice_view->addData(['company' => $company,
'client' => $client,
'template' => $company->invoiceTemplate,
'availableProducts' => [],
'availableForms' => [],
'sales' => $invoice->sales->all(true),
'inv' => $invoice,
'mode' => 'view'
]);
$invoice_view->set('date', date("M, d Y",time()));
$view->set('invoice_view', $invoice_view->render());
$dompdf = new Dompdf();
$dompdf->loadHtml($view->render());
$dompdf->setPaper('A4', 'landscape');
$dompdf->render();
$dompdf->stream();
}

try External css on your page
like;-
<link rel="stylesheet" href="http:****.css" type="text/css"/>
Dompdf have some limitation of applying css
try :- remove extra css and make simple other css file import like (link and try)
advance css not worked pure css apply

Related

mpdf yii2 throwing error Undefined offset: 0 for tableLevel

I am using kartik-v/yii2-mpdf v1.0.5 and simply printing html content into the pdf.
but recently I started facing issue without any change in code
my code:
$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' => $pdfTemplate->name],
// call mPDF methods on the fly
'methods' => [
'SetHeader' => [$pdfTemplate->name],
'SetFooter' => ['{PAGENO}'],
]
]);
return $pdf->render();

Yii2 - kartik-v/mpdf - Header only in first page

I have a template with header, content and footer rendered in PDF.
It's possible render header only in first page? It's possible?
$pdf = new Pdf([
// set to use core fonts only
'mode' => Pdf::MODE_CORE,
//Name for the file
'filename' => 'test.pdf',
// A4 paper format
'format' => Pdf::FORMAT_A4,
// portrait orientation
'orientation' => Pdf::ORIENT_LANDSCAPE,
// 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/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'=>$header,
'SetFooter'=>$footer,
],
'options' => [
'setAutoTopMargin' => 'pad',
'defaultfooterline' => false,
],
]);
If you are using CSS to style your page, as usual, you may find the #page selector useful. It should be something like this:
#page {
header: html_otherpages;
}
#page :first {
header: html_firstpage;
}

yii2 mpdf render html reached max memory size

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

Yii2 File Validation Error not working after set enableAjaxValidation to true

Previously, when I set enableAjaxValidation to false on my ActiveForm , the file validation worked so well ,but after I set enabledAjaxValidation to true in order to check unique slug, it doesn't work.
Here is what I've done
Form:
<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data'] , 'enableAjaxValidation' => true,'id' => 'article-form']); ?>
Model rule:
[['slug'], 'unique'],
[['featureFile' , 'bannerFile'], 'file', 'skipOnEmpty' => false, 'extensions' => 'png, jpg' , 'on' => 'create'],
Controller:
use yii\web\Response; //Ajax Validation
use yii\widgets\ActiveForm; //Ajax Validation
if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post()))
{
Yii::$app->response->format = Response::FORMAT_JSON;
return ActiveForm::validate($model);
}
Even though I choose the image file already but it still show validation error
Please help!!!
You can disable the ajax validation of the specified field.
Try
<?= $form->field($model, 'featureFile', ['enableAjaxValidation' => false])->fileInput() ?>

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.