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.
Related
I have a problem in adding a custom font to generated pdf .I used mpdf package and I did all the steps the add a custom font .I created a pdf.php in config folder with the code below
<?php
return [
'custom_font_dir' => base_path('resources/fonts/'), // don't forget the trailing slash!
'custom_font_data' => [
'circular-black' => [
'R' => 'CircularStd-Black.otf', // regular font
]
// ...add as many as you want.
]
];
and in my controller
use PDF;
class pdfController extends Controller
{
public function generateResultat($id){
$resultat=some data;
$fileName='Resultat Order-'.$resultat->order->examCodeSession.'.pdf';
$mpdf= new \Mpdf\Mpdf([
'mode' => 'utf-8',
'format' => 'A4',
'margin_left'=>10,
'margin_right'=>10,
'margin_top'=>15,
'margin_bottom'=>20,
'margin_header'=>10,
'margin_footer'=>10,
]);
$html=\View::make('pdf.resultat',[
'resultat'=>$resultat
]);
$html->render();
$mpdf->WriteHTML($html);
$mpdf->Output($fileName,'D');
}
}
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.
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.
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
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)