Add custom font mpdf-laravel - php

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');
}
}

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.

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.

How to Properly use HTML2PDF and Yii-PDF in Yii?

Hello I'm new to yii so currently don't know how to properly use this Html2pdf & yii-pdf extension in yii to get the pdf..
What i actually want.. I have a page called http://localhost/site/Users/Results.. which is showing the list of users..So If i click on user one, it will open a new page called http://localhost/site/Applicant/1
& for user two, it will be
http://localhost/site/Applicant/2
So on these pages there is all the information of users. I want to put a download PDF button on this page, If user clicks on it, He will be able to download all his information in PDF. There can be many users. Everyuser will be able to download all his information in pdf.
I got the html2pdf & yii-PDF .. I got this settings by searching on google, but unable to find proper example to use it according to my requirements above.
config/main.php
'ePdf' => array(
'class' => 'ext.yii-pdf.EYiiPdf',
'params' => array(
'HTML2PDF' => array(
'librarySourcePath' => 'application.extensions.html2pdf.*',
'classFile' => 'html2pdf.class.php', // For adding to Yii::$classMap
/*'defaultParams' => array( // More info: http://wiki.spipu.net/doku.php?id=html2pdf:en:v4:accueil
'orientation' => 'P', // landscape or portrait orientation
'format' => 'A4', // format A4, A5, ...
'language' => 'en', // language: fr, en, it ...
'unicode' => true, // TRUE means clustering the input text IS unicode (default = true)
'encoding' => 'UTF-8', // charset encoding; Default is UTF-8
'marges' => array(5, 5, 5, 8), // margins by default, in order (left, top, right, bottom)
)*/
)
),
),
Here is my controller.
class UsersController extends Controller
{
public function createPDF(){
$html2pdf = Yii::app()->ePdf->HTML2PDF();
$html2pdf->WriteHTML($this->renderPartial('index', array(), true));
$html2pdf->Output();
}
}
im totally new to yii, so how to use this extension, I have never used any extension before.. is there any other method to download pdf of each user information. any suggestions or and example of working code.
config/main.php
'ePdf' => array(
'class' => 'ext.yii-pdf.EYiiPdf',
'params' => array(
'HTML2PDF' => array(
'librarySourcePath' => 'application.vendors.html2pdf.*',
'classFile' => 'html2pdf.class.php', // For adding to Yii::$classMap
)
),
),
Controller
class UsersController extends Controller
{
public function createPDF(){
$pdffilename = 'test.pdf';
$html2pdf = Yii::app()->ePdf->HTML2PDF();
$html2pdf->WriteHTML($this->renderPartial('index', array(), true));
ob_clean();
$html2pdf->Output($pdffilename,"I"); // OUTPUT_TO_BROWSER
/* OUTPUT_TO_BROWSER = "I" */
/* OUTPUT_TO_DOWNLOAD = "D" */
/* OUTPUT_TO_FILE = "F" */
/* OUTPUT_TO_STRING = "S" */
}
}

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