How to make pdf password protected in yii2 php - 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.

Related

How to add a watermark with Yii2 kartik-mpdf

How can I add watermark image on pdf using kartik-pdf?
Here is the line I added:
'options' => [
'title' => 'Title',
'showWatermarkText' => true,
'showWatermarkImage' => true,
],
// call mPDF methods on the fly
'methods' => [
'SetHeader' => ['Title'],
'SetFooter' => ['{PAGENO}'],
'SetWatermarkText' => ['DRAFT'],
'SetWatermarkImage' => $basepath . '/images/imgbox1.jpg',
]
Only the text watermark display.
Is there something wrong on how I used watermarkImage?
use kartik mpdf library to create object of mpdf;
The following code will create simple pdf using kartik widget
use mPDF;
class SiteController extends Controller
{
public funtion createpdf()
{
$HTML='your html content';
$mpdf = new \Mpdf\Mpdf(); //please use your kartik mpdf object here
$mpdf->SetWatermarkImage('../images/background.jpg');
$mpdf->showWatermarkImage = true;
$mpdf->WriteHTML($HTML);
// Saves file on the server as 'filename.pdf'
$mpdf->Output('filename.pdf', \Mpdf\Output\Destination::FILE);
}
}
Set the SetWatermarkImage property as an array, like this:
// call mPDF methods on the fly
'methods' => [
'SetHeader' => ['Title'],
'SetFooter' => ['{PAGENO}'],
'SetWatermarkText' => ['DRAFT'],
'SetWatermarkImage' => ['/images/imgbox1.jpg'],
]
Also, you have to check the image path. You used the $basepath, I usually don't use it, since my images folder is already inside my web folder.

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