CakePdf set Tcpdf engine options - php

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 ?

Related

how to get the page number inside content (html layout) of kartik mpdf

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];?>

Yii2 Translate Manager doesn't work

I m using a basic template for a small project on Yii2. I have already set the module Language Picker of Lajax (Doc) and I am trying now to manage the translation with the module Translate Manager of Lajax (Github). The plugin is scanning perfectly the project and getting the translatable texts. I even set some translations through this module and everything is saved in the database, but these translations are not set when changing the language.
here are my web.php Configurations:
'language' => 'en-GB',
'components' => [
...
'languagepicker' => [
'class' => 'lajax\languagepicker\Component',
'languages' => ['en-GB', 'fr-FR']
],
'i18n' => [
'translations' => [
'*' => [
'class' => 'yii\i18n\DbMessageSource',
'db' => 'db',
'sourceLanguage' => 'en-GB',
'sourceMessageTable' => '{{%language_source}}',
'messageTable' => '{{%language_translate}}',
'forceTranslation' => true,
'cachingDuration' => 86400,
'enableCaching' => true,
],
],
],
...
]
'modules' => [
...
'translatemanager' => [
'class' => 'lajax\translatemanager\Module',
'root' => '#app', // The root directory of the project scan.
'scanRootParentDirectory' => false, // Whether scan the defined `root` parent directory, or the folder itself.
// IMPORTANT: for detailed instructions read the chapter about root configuration.
'layout' => 'language', // Name of the used layout. If using own layout use 'null'.
'allowedIPs' => ['127.0.0.1'], // IP addresses from which the translation interface is accessible.
'roles' => ['#'], // For setting access levels to the translating interface.
'tmpDir' => '#runtime', // Writable directory for the client-side temporary language files.
// IMPORTANT: must be identical for all applications (the AssetsManager serves the JavaScript files containing language elements from this directory).
'phpTranslators' => ['::t'], // list of the php function for translating messages.
'jsTranslators' => ['lajax.t'], // list of the js function for translating messages.
'patterns' => ['*.js', '*.php'],// list of file extensions that contain language elements.
'ignoredCategories' => ['yii'], // these categories won't be included in the language database.
'ignoredItems' => ['config'], // these files will not be processed.
'scanTimeLimit' => null, // increase to prevent "Maximum execution time" errors, if null the default max_execution_time will be used
'searchEmptyCommand' => '!', // the search string to enter in the 'Translation' search field to find not yet translated items, set to null to disable this feature
'defaultExportStatus' => 1, // the default selection of languages to export, set to 0 to select all languages by default
'defaultExportFormat' => 'json',// the default format for export, can be 'json' or 'xml'
'tables' => [ // Properties of individual tables
[
'connection' => 'db', // connection identifier
'table' => '{{%language}}', // table name
'columns' => ['name', 'name_ascii'],// names of multilingual fields
'category' => 'database-table-name',// the category is the database table name
]
],
'scanners' => [ // define this if you need to override default scanners (below)
'\lajax\translatemanager\services\scanners\ScannerPhpFunction',
'\lajax\translatemanager\services\scanners\ScannerPhpArray',
'\lajax\translatemanager\services\scanners\ScannerJavaScriptFunction',
'\lajax\translatemanager\services\scanners\ScannerDatabase',
],
],
...
]
I always use something like this im code for translatable strings:
<?= Yii::t('app','Test') ?>
Am I doing something wrong?

Wkhtmltopdf + cakepdf - Custom page size

I can't figure out how to enable custom page sizes with CakePDF and wkhtmltopdf. I have the following configuration code:
Configure::write('CakePdf', [
'engine' => [
'className' => 'CakePdf.WkHtmlToPdf',
'binary' => '/usr/local/bin/wkhtmltopdf',
],
'orientation' => 'portrait',
'pageSize' => '', // this line
'download' => true
]);
I want to have 150x150mm pages. I already tried several things like passing an array [150,150] but also things like '150 150' or '150mm 150mm'. Is this even possible?
The CakePDF pageSize option maps to the page-size option of wkhtmltopdf, which takes QPrinter::PaperSize constant names, like for example A4, A5, B0, B1, Legal, Letter, etc., ie you cannot define a custom size using that option.
If you need a custom size, then you have to use the wkhtmltopdf specific page-width and page-height options, which both take millimeter values by default.
Quote from the wkhtmltopdf docs:
Page sizes:
The default page size of the rendered document is A4, but using this
--page-size option this can be changed to almost anything else, such as: A3,
Letter and Legal. For a full list of supported pages sizes please see
http://qt-project.org/doc/qt-4.8/qprinter.html#PaperSize-enum.
For a more fine grained control over the page size the --page-height and
--page-width options may be used
Configure::write('CakePdf', [
'engine' => [
'className' => 'CakePdf.WkHtmlToPdf',
'binary' => '/usr/local/bin/wkhtmltopdf',
'options' => [
'page-width' => 150,
'page-height' => 150
]
],
'orientation' => 'portrait',
'download' => true
]);
See also
wkhtmltopdf docs
Qt Documentation > QtGui > QPrinter > enum QPrinter::PaperSize

Retina iconLink with Google Drive API

I'm an getting a list of files in a folder. The response contains a iconLink for every file returned. This icon is 16x16 pixels.
Does anyone know a way to retrieve a retina image? Or another way to retrieve a bigger icon image?
https://developers.google.com/drive/v2/reference/files
top: Google Drive UI
bottom: Google Drive API integration
The good news is although not officailly documented driver does have 2x resolution icons. The bad news is they have inconsistent file names; for example the icon you linked in the comments has a 32px version availabel here: ssl.gstatic.com/docs/doclist/images/mediatype/icon_3_pdf_x32.png
Now here is my soltion, it's not perfect but it will do the job for a while:
function getIcons($file_type)
{
$icons = [
'pdf' => [
'icon' => 'icon_12_pdf_list.png',
'retina' => 'icon_3_pdf_x32.png'
],
'document' => [
'icon' => 'icon_1_document_x16.png',
'retina' => 'icon_1_document_x32.png'
],
'image' => [
'icon' => 'con_1_image_x16.png',
'retina' => 'icon_1_image_x32.png'
],
'word' => [
'icon' => 'icon_1_word_x16.png',
'retina' => 'icon_1_word_x32.png'
],
'text' => [
'icon' => 'icon_1_text_x16.png',
'retina' => 'icon_1_text_x32.png'
],
'spreadsheet' => [
'icon' => 'icon_1_spreadsheet_x16.png',
'retina' => 'icon_1_spreadsheet_x32.png'
],
'form' => [
'icon' => 'icon_2_form_x16.png',
'retina' => 'icon_2_form_x32.png'
],
'audio' => [
'icon' => 'icon_1_audio_x16.png',
'retina' => 'icon_1_audio_x32.png'
]
];
return isset($icons[$file_type]) ? $icons[$file_type] : $icons['text'];
}
The reasion I say it will work for a while is that I'm asuming the _3_ in pdf icon file name for instance is the version number. So if Google updates it's icons again in the future this solution may brake.
I'm using drive rest api and what i observed was that iconLink attribute had a definite pattern.
"https://drive-thirdparty.googleusercontent.com/" + size + mimetype
By default size is 16. So, before adding your icon to Image, use this:
String iconLink = (String) jsonObject.get("iconLink");
iconLink=iconLink.replace("16","128");
check out these both links:
https://drive-thirdparty.googleusercontent.com/128/type/application/pdf
https://drive-thirdparty.googleusercontent.com/16/type/application/pdf
Looks like images with x128 also added/present for various versions:
Ver. 1
Ver. 2
Ver. 3
Better to replace the x16 from the fetched iconLink and replace it with x128.

Kartik Export - How to style exported PDF files?

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.

Categories