I am using mPDF in generating payslips. However, the icons in the payslip aren't showing once it is generated. It only leaves a blank space just like this:
Icons should show on those highlighted spots.
So far, here's what I've done:
I am using Yii2 PHP framework and here's my action controller:
public function actionPdf($id)
{
$model = $this->findModel($id);
$earnings = EarningDetails::find()->where(['payslip_id' => $model->_id, 'status' => 1])->all();
$deductions = DeductionDetails::find()->where(['payslip_id' => $model->_id, 'status' => 1])->all();
$html = $this->render('view', [
'model' => $model,
'earnings' => $earnings,
'deductions' => $deductions,
]);
$mpdf = new mPDF('c','A5-L','0','',0,4,1,1,0,0);
$mpdf->allow_charset_conversion = true;
$mpdf->charset_in = 'windows-1252';
$mpdf->SetTopMargin(0);
$user_password = User::find()->where(['_id' => $model->user_id ])->one();
$password = $user_password->fname.$user_password->lname;
$mpdf->SetProtection(array(), $password, $password);
$mpdf->WriteHTML($html);
$mpdf->Output('Payslip.pdf', 'D');
exit;
}
Am I missing something? Please let me know.
Encoding issues aside, this could be a couple of things. Firstly, you need to integrate FontAwesome with your MPDF installation. Secondly, you need to consider how you're speficiying the glyph in HTML.
Installing FontAwesome in mPDF
Download or clone FontAwesome from https://github.com/FortAwesome/Font-Awesome and copy fonts/fontawesome-webfont.ttf into your MPDF ttfonts/ directory.
In your MDPF config_fonts.php file, add the following lines to $this->fontdata:
/* FontAwesome */
"fontawesome" => array(
'R' => "fontawesome-webfont.ttf"
),
Adding the glyph in HTML
You need to keep in mind that the CSS :before pseudo-selector commonly used to add FontAwesome glyphs to HTML doesn't work in mPDF.
Bad:
<i class="fa fa-smile-o"></i>
... because this FontAwesome CSS rule doesn't work in mPDF:
.fa-smile-o:before {
content: "\f118";
}
Good:
<i class="fa"></i>
You can get the unicode code point for each glyph by clicking on it on the FontAwesome Icon List, but the Cheatsheet is more convenient for this.
You can try to use the re-mapped fontawesome 4.7 (free) font to #0021-#02E5 characters,
and use it as it was regular FONT (ascii).
You can download the ready-to-use font here:
http://mdb-blog.blogspot.com/2021/12/using-fontwesome-in-php-fpdf.html
Note that the example works for FPDF, but it is easly can be made for any tool :)
Related
Is it possible to change font size when using TemplateProcessor?
I was try to do this, but not working.
$template = new PhpOffice\PhpWord\TemplateProcessor('template.docx');
$template->setValues([
'name' => 'Nur Muhammad',
'country' => 'Indonesia'
]);
$template->saveAs('result1.docx');
// Try to change the font size
$phpword = PhpOffice\PhpWord\IOFactory::load('result1.docx');
$phpword->setDefaultFontSize(9);
$phpword->save('result2.docx');
The problem I get:
The document styles is lose
The font size doesn't change
Thanks in advance.
i have a mPDF report on my system and in the report, it has a header and footer where i use $mpdf->setHeader(); & $mpdf->setFooter(); to set the header and footer. but it displays a bottom border for header and top border for footer. can anyone help me how to remove this?
heres the image:
Here's my Code:
$mpdf=new mPDF('','LETTER-L','','',35,35,60,25,10,10);
//left margin, right margin, body, top margin,top margin,bottom margin,
/*HEADER Monthly Accomplishment Report*/
$mpdf->SetHeader('Sample');
/*FOOTER xMonthly Accomplishment Report*/
$mpdf->SetFooter('Sample');
//==============================================================
//=====================FILE DESCRIPTION=========================
//==============================================================
$mpdf->SetTitle('Monthly Accomplishment Report');
$mpdf->SetSubject('Report');
$mpdf->SetAuthor('sample');
$mpdf->Output('sample.pdf','I');
exit;
//==============================================================
//==============================================================
//==============================================================
You could use those two mpdf properties instead:
$mpdf->defaultheaderline = 0;
$mpdf->defaultfooterline = 0;
I had a look at the documentation of the method setHeader and found that exists a line parameter :
$line: specify whether to draw a line under the Header
You passed a string to the method but it also accept an array of options.
$line mentionned in the doc is not exactly a parameter of the method, rather a key of the configuration array.
So this code should accomplish what you look for, based on the documentation:
$mpdf = new mPDF('','LETTER-L','','',35,35,60,25,10,10);
$headerFooterContent = 'Sample';
$oddEvenConfiguration =
[
'L' => [ // L for Left part of the header
'content' => '',
],
'C' => [ // C for Center part of the header
'content' => '',
],
'R' => [
'content' => $headerFooterContent,
],
'line' => 0, // That's the relevant parameter
];
$headerFooterConfiguration = [
'odd' => $oddEvenConfiguration,
'even' => $oddEvenConfiguration
];
$mpdf->SetHeader($headerFooterConfiguration);
$mpdf->SetFooter($headerFooterConfiguration);
The setHeader and setFooter methods accept the same arguments (they are almost copy/pasted in the library).
I let you look further at the specific part of the examples related to complex header configuration of mPDF.
Let me know if it solves your issue.
Depending on the version of mpdf, you can use this:
$pdf->options['defaultheaderline'] = 0;
$pdf->options['defaultfooterline'] = 0;
How to display Tamil font in Yii2 advance template?
When I try to do that, it is showing me question marks : ??????. An in the Yii2 application, it is Displaying not displaying Yii2-mpdf export.
What am I doing wrong?
my code:
1) upload "MyFont.ttf" into /mpdf/ttfonts
2) in /mpdf/config_fonts.php , inside $this->fontdata array, add your:
"my_custom_fonttt" => array(
'R' => 'MyFont.ttf', //Regular- REQUIRED
'I' => "MyFont-Oblique.ttf", //Italic - OPTIONAL
'B' => "MyFont-Bold.ttf", //Bold - OPTIONAL
),
3) then, wherever you execute your custom script, use my_custom_fonttt in css:
<?php
$mpdf=new mPDF();
$texttt= '
<html><head><style>
body {font-family: "my_custom_fonttt";}
</style></head>
<body>My SAMPLE TEXTTTTT </body>
</html>';
$mpdf->WriteHTML("$texttt");
$mpdf->Output(dirname(__FILE__).'/new_created_file.pdf','F');
?>
Try add font to your css file by #font-face. And add option cssFile to config your mPDF object.
Simple:
$pdf = new Pdf([
...
'cssFile' => '#vendor/kartik-v/yii2-mpdf/assets/kv-mpdf-bootstrap.min.css',
...
]);
If don't help read docs.
I'm trying to hide the header page number on the first page using this example I found here. Which only works if I use it with footer-html and doesn't show/hide anything if I use it with header-html. Originally I was trying to augment this solution which also worked using footer-html, but since I couldn't get it to work in the header I kept on searching. I've tried it with and without 'header-center' => '[[page]]' in case using this with header-html caused conflicts. Anyone been able to get this to work in the headers recently? I'm using PHPWKHTMLtoPDF wrapper version 1.2.6-dev if that helps with a up to date version of WKHTMLtoPDF, since the newest version of PHPWKHTMLtoPDF uses namespaces and we're using CodeIgniter 2.x-dev, which doesn't support them (or play well can't remember).
// Create document PDF
$pdf = new $this->wkhtmltopdf;
// Locate WkHtmlToPdf executable for Windows
if( $pdf->getIsWindows() )
{
$pdf->setOptions( array( 'binPath' => 'C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe',
'no-outline',
'encoding' => 'UTF-8',
'margin-top' => 30,
'margin-right' => 20,
'margin-bottom' => 30,
'margin-left' => 20,
// Default page options
'disable-smart-shrinking',
'user-style-sheet' => 'pdf.css',
'header-html' => dirname(__FILE__) . '\..\views\wkhtmltopdf\header.html'
) );
}
// Generate document fields
$docInputs = $this->generate_inputs( $inputs, json_decode( $this->load->file( APPPATH . '/mapping/' . $document['mapping'], TRUE ), TRUE ) );
// Merge document fields into HTML exported Word files
$docHTML = $this->parser->parse( 'docs/' . $document['html'], $docInputs, TRUE );
// Add HTML as page, along with option for page header
$pdf->addPage( $docHTML, array( 'header-center' => '[[page]]',
'header-spacing' => '10',
'header-font-name' => 'Times New Roman'
) );
You need to add the <!DOCTYPE html> to the header file, WKHTMLtoPDF issue #46 for version 0.12
I'm posting this answer because this happened to me and this might also be a reason.
I'v also noticed if you set the header css to this. It will not show the header.
html{
width: 100%;
height: 100%;
}
Make sure you have version with patched qt
I have setup OpenTBS to generate dynamically from my contacts database. Now I want to setup the actual content of the email to come from a MySQL table created using CKeditor or another WYSISYG JS editor.
I have been able to get the content to display on the outputted docx file but it is coming this as a straing with the html tags, I somehow need this to format so it comes though as it was put in by the client. I am only going to allow them to use the basics, paragrahs, bold, italics and maybe a few other tags.
Is this someway I can convert this html string to word copy / text? Or somehow have a different WYSIWYG editor to save it in MySQL as word code rather then html code.
Thanks
It's not really relevant but below is the function that generates the OpenTBS document from a template and the database (I am using CakePHP) MailMerge.description is the body with the html code at the moment.
// FUNCTION EXPORT MAIL MERGE
// -------------------------------------------------------------->
function mail_merge()
{
Configure::write('debug',2);
$this->layout = null;
$this->autoRender = FALSE;
// GET THE CONTACTS
// ------------------------------------------------------------->
$contacts = $this->Contact->find('all', array('limit' => 20, 'contain' => FALSE));
$this->loadModel('MailMerge');
$content = $this->MailMerge->find('first', array(
'conditions' => array('MailMerge.id' => 1),
'fields' => array('MailMerge.description')
));
// Get a new instance of TBS with the OpenTBS plug-in
// ------------------------------------------------------------->
$otbs = $this->Tbs->getOpenTbs();
// Load the template file
// ------------------------------------------------------------->
$otbs->LoadTemplate(APP . DS . WEBROOT_DIR . '/files/data_files/enquiry_letter_sample_redcliffe.docx');
// Merge data in the template
// ------------------------------------------------------------->
$otbs->MergeBlock('r', $contacts);
$otbs->MergeBlock('content', $content);
// End the merge and export
// ------------------------------------------------------------->
$file_name = 'export.docx';
$otbs->Show(OPENTBS_DOWNLOAD, $file_name);
}