yii2 pdf generation not working - php

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

Related

How to make pdf password protected in yii2 php

I want to make my pdf password protected.
I am using kartik\mpdf\Pdf extension for generating 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_FILE,
// your html content input
'content' => $html,
// any css to be embedded if required
'cssFile' => '#api/web/css/notes.css',
// set mPDF properties on the fly
'options' => ['title' => $note['title']],
// call mPDF methods on the fly
'methods' => [
'SetHeader' => [''],
'SetFooter' => [''],
],
]);
$pdf->content = $html;
$file_name = "test" . rand(7, 100).pdf";
$password = "122":
$pdf->setProtection(array(),$password);
$pdf->filename = "uploads/" . $file_name;
echo $pdf->render();
But it gives me error like : Calling unknown method: kartik\mpdf\Pdf::setProtection();
I have also tried following code that is putting method after filename :
$pdf->filename->setProtection(array(), $password);
but it's also give me error like : Call to a member function setProtection() on string.
I know the setProtection method is available in vendor's kartik library but i am not sure of how to use it.
Please assist me if anyone has idea.
Try
$pdf->getApi()->setProtection();
Method getApi() is to directly call MPDF object and that is where SetProtection() method is.

PHP - Yii2 MPDF not rendering tables

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();

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'.

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