PHP - Yii2 MPDF not rendering tables - php

I am using mpdf extension in Yii2 for generating pdf (http://www.yiiframework.com/extension/yii2-mpdf/)
The code is given below.
$fileName = 'tst.pdf';
$invoiceHtml = "
<table style = 'height:500px;width:500px' border='1'>
<tr>
<td>Test
</td>
<td>Test2
</td>
</tr>
<tr>
<td>Test
</td>
<td>Test2
</td>
</tr>
<table>
";
$pdf = new Pdf([
// set to use core fonts only
'mode' => Pdf::MODE_CORE,
// A4 paper format
'orientation' => Pdf::ORIENT_LANDSCAPE,
// stream to browser inline
'destination' => Pdf::DEST_DOWNLOAD,
// your html content input
'content' => $invoiceHtml,
'filename' =>$fileName,
'cssInline' => ' #page{size: 500mm 200mm}',
// set mPDF properties on the fly
'options' => ['title' => 'Krajee Report Title'],
// call mPDF methods on the fly
'methods' => [
'SetHeader'=>['New Horizon Travel And Tours LLC'],
]
]);
return $pdf->render();
The code is generating blank pdf file. I have tried other HTML tags and other tags and plain text is working as expected. When table tag is used it is working.
Looking for a solution for this problem

This might not be the answer to your question. But i already face the same problem before. You may use my alternative if u insist...
1 - put your Html code inside HTML file..a separate one.
2 - use below code to subtitute your code.
$invoiceHtml = Yii::$app->controller->renderPartial('_yourHtmlFile');
$pdf = new Pdf([
// set to use core fonts only
'mode' => Pdf::MODE_CORE,
// A4 paper format
'orientation' => Pdf::ORIENT_LANDSCAPE,
// stream to browser inline
'destination' => Pdf::DEST_DOWNLOAD,
// your html content input
'content' => $invoiceHtml,
'filename' =>$fileName,
'cssInline' => ' #page{size: 500mm 200mm}',
// set mPDF properties on the fly
'options' => ['title' => 'Krajee Report Title'],
// call mPDF methods on the fly
'methods' => [
'SetHeader'=>['New Horizon Travel And Tours LLC'],
]
]);
return $pdf->render();

Related

how to get the page number inside content (html layout) of kartik mpdf

I am using Kartik mpdf library to generate pdf. I am generating barcode in the body of the pdf which contains mcqs question generated through loop so pages are inserted randomly depending on the questions, So I need the page number to be set in my barcode on each page.
Kartik mpdf provides a method 'SetFooter' => ['{PAGENO}|'] which gives page number in footer but this {'PAGENO'} is not working in my html document... How would I get the page number inside my document.
PAGENO has nothing to do with kartik, is from mpdf. When you say is not working in your html, that's because that placeholder variable is going to be parsed with mpdf, so if you are not generating a pdf it wont take effect.
On the other hand, you can't show the page number in your html. There's no pages numbers on a html document, as it will depend in the page size and the font size of the browser, so it won't be possible to know "where you are" on the server side.
I hope you are using renderPartial method. Please find my code below.
$pdf = new Pdf([
'mode' => Pdf::MODE_UTF8, // leaner size using standard fonts
'destination' => Pdf::DEST_BROWSER,
'orientation' => Pdf::ORIENT_PORTRAIT,
'content' => $this->renderPartial('print', [
'model' => $model,
'quotations' => $modelQtn,
'pageNo' => ['{PAGENO} of {nb}'],
]),
'filename' => $model->po_no,
'cssFile' => [
'#vendor/../backend/assets/css/custom-style.css',
'#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:26px
}
.text-center {
font-family : Arial !important;
font-size:
}',
'options' => [
// any mpdf options you wish to set
'title' => 'Purchase Order'
],
'methods' => [
'SetTitle' => $model->po_no,
'SetSubject' => 'Mohammed Iqbal Khan',
'SetHeader' => [ 'Generated On: ' . date("F j, Y, g:i a")],
'SetFooter' => ['|Page {PAGENO} of {nb}|'],
'SetAuthor' => 'Mohammed Iqbal Khan',
'SetCreator' => 'Mohammed Iqbal Khan',
'SetKeywords' => 'Mohammed Iqbal Khan',
'SetWatermarkText' => 'DRAFT',
'SetWatermarkImage' => '',
]
]);
return $pdf->render();
Once you pass the page no method, then you can use the below code to show page no in your document while generating.
<?= $pageNo[0];?>

Font size becomes small when the text is too long in kartik/mpdf of Yii framework 2. How to fix that?

Working with Yii framework 2, I use kartik\mpdf\Pdf class to response my HTML page in PDF format to the browser. Below is my configuration.
$pdf = new kartik\mpdf\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' => $htmlContent,
// 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} .page-break{ page-break-after:always; } li { list-style:none; }',
// set mPDF properties on the fly
'options' => ['title' => 'My title'],
// call mPDF methods on the fly
'methods' => [
'SetHeader'=>['My_header_ ' . date('d-m-Y')],
'SetFooter'=>['{PAGENO}'],
],
]);
// return the pdf output as per the destination setting
return $pdf->render();
Everything works perfectly, except when there is a variable inside my HTML page, which returns a long string, that text becomes small. How can I solve it?
mPDF resizes too large tables to fit a page. To prevent this, either add
'options' => ['shrink_tables_to_fit' => 0];
to your kartik\mpdf\Pdf constructor call OR set a custom HTML attribute
<table autosize="0">
Also see mPDF manual on tables.

Internal style not working for my pdf

$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/assets/kv-mpdf-bootstrap.min.css',
// any css to be embedded if required
'cssInline' => '.kv-heading-1{font-size:18mm}',
// set mPDF properties on the fly
'options' => ['title' => 'Title'],
// call mPDF methods on the fly
'methods' => [
// 'SetHeader'=>['Krajee Report Header'],
// 'SetFooter'=>['{PAGENO}'],
],
'marginTop' => 1,
'marginBottom' => 1,
'marginLeft' => 2,
'marginRight' => 0,
]);
Html:
$html = '<style>'. $mystyle .' </style>';
$html .= '<div>' . $mydiv . '</div>';
echo $html;
Trying to print the div with the internal style but the internal style don't seem to work even though div is showing. There is not error shown so is there any chance that it doesn't support internal style? Or did i miss out something?
**Inline style is working
The answer I found to the internal style not working is because whatever I pass to the PDF is read as 'content'. I need to pass the style to 'cssInline' and not 'content'.

yii2 pdf generation not working

I have install mpdf using this link pdf yii2 installer ,
This is not working.
My action is :
public function actionReport() {
// get your HTML raw content without any layouts or scripts
$content = '<html><head></head><body><h1 class="kv-heading-1">hello</h1></body></html>';
// setup kartik\mpdf\Pdf component
$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/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'=>['Krajee Report Header'],
'SetFooter'=>['{PAGENO}'],
]
]);
// return the pdf output as per the destination setting
return $pdf->render();
}
Output:
What should I have to do for pdf ?
Try using
// set to use core fonts only
'mode' => Pdf::MODE_BLANK,
I think your issue is due to the encoding used when rendering the document. I've had a couple of similar issues in the past and setting the mode to UTF8 seems to have always solved the problem for me.
'mode' => Pdf::MODE_UTF8

Generate many pdf pages with PHP

I have a problem to generate many pages (more of 5,000 pages) with PHP. In real, the problem is a time of wait to generate.
I am use the framework YII 2.0 and the widget mPDF, to generate 60 pages the time of wait its from 2mins.. I have to otimize this time, but its necessary generate the pdf (with css to style the doc)..
I understand to generate more of 5,000 pages it will take a while, but i need otimize the maximum this time, the way it is now It is too long.
Anyone know the way to do this? Not necessary need to use YII, if have other way with PHP (library, or some way else).
Code:
$content = $this->renderPartial('tabela-pdf', [
'model' => $model,
'table' => $table, //CONTENT
]);
$header = $this->renderPartial('pdf');
// setup kartik\mpdf\Pdf component
$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/assets/kv-mpdf-bootstrap.min.css',
// any css to be embedded if required
'cssInline' => '.kv-heading-1{font-size:14px}',
// set mPDF properties on the fly
'options' => ['title' => 'Relatório'],
'defaultFontSize' => 3,
'marginLeft' => 10,
'marginRight' => 10,
'marginTop' => 30,
'marginBottom' => 11,
'marginHeader' => 6,
'marginFooter' => 6,
// call mPDF methods on the fly
'methods' => [
'SetHeader' => [$header],
'SetFooter' => ['<p style="text-align: center;font-size: 8px;">FOOTER</p>'],
]
]);
$pdfName = 'FILE-_' . date('d-m-Y') . '.pdf';
Yii::$app->response->format = Response::FORMAT_HTML;
Yii::$app->response->setDownloadHeaders($pdfName, 'application/pdf');
return $pdf->render();
Just a document with a big table (no have identical content)

Categories