I am using Kartik Export (kartik\export\ExportMenu) to export data from gridview (kartik\grid\GridView) to PDF file. The problem is that the file has a small font and data seems jumbled up since there are no table borders or other formatting present.
My question is - How can I add custom styles to the exported document?
This is the code I am using in my view file:
<?= GridView::widget([
'dataProvider' => $provider,
'columns' => $columns,
]); ?>
<?= ExportMenu::widget([
'dataProvider' => $provider,
'columns' => $export_columns,
'target' => ExportMenu::TARGET_SELF,
'showConfirmAlert' => false,
'showColumnSelector' => false,
'exportConfig' => [
ExportMenu::FORMAT_HTML => false,
ExportMenu::FORMAT_TEXT => false,
],
'filename' => 'exported-data_' . date('Y-m-d_H-i-s'),
]); ?>
In your exportConfig array add this setting:
GridView::PDF => [
'config' => [
'cssFile' => '#webroot/css/report.css',
]
]
Now you're able to create report.css in the css folder of you web folder and you can style ahead. This will replace the default bootstrap stylesheet, but reading your question it looks like it didn't get loaded anyways.
You'll find other configuration options for PDF here: [http://demos.krajee.com/grid#default-export-config]
The difference between GridView and ExportMenu is that the ExportMenu uses PHPExcel library to generate the PDF via mPDF (or any other lib you need) while GridView directly offers method to use the mPDF library.
The formatting is controlled by PHPExcel settings (header, footer, fonts) - check the PHPExcel Documentation for details.
Related
i want to to implement cakepdf in my project and i want to use tcpdf as pdf engine. i know the recommendet engine was WkHtmlToPdf but i prefer the tcpdf engine. and i want to know how i can implement engine specific options for tcpdf.
i tried it over the the options array in the Configuration but nothing works. i also tried it over the viewBuilder->setOptions function but again nothing works. as example i need to disable the header and footer and i know it works with the setPrintHeader => false.
i tried it in the AppController with this
Configure::write('CakePdf', [
'engine' => 'CakePdf.Tcpdf',
'orientation' => 'portrait',
'download' => true,
'options' => [
'setPrintHeader' => false,
'setPrintFooter' => false
]
]);
and also with this
$this->viewBuilder()->setClassName('CakePdf.Pdf');
$this->viewBuilder()->setOptions([
'pdfConfig' => [
'orientation' => 'portrait',
'filename' => 'test_',
'setPrintHeader' => false,
'setPrintFooter' => false
]
]);
nothing of these works. the header and footer lines also print on the pdf. how i can disable this ?
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];?>
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.
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