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;
Related
I was looking around for examples and/or guides on how to use phpspreadsheet's conditional formatting. Specifically looking so that I could zebra stripe my spreadsheet in a way that allows for resorting of the sheet. I came up very empty with only a few spotty SO questions using the old PHPExcel library and just one example from the documentation. Given that zebra stripes are pretty common style to apply I was mildly outraged that I couldn't find a copy/paste job. So I figured it out myself and now will leave it here for posterity and hopefully myself in a few years.
I am actually going to spell out a lot more as I have used phpspreadsheet for a few years now and out of the box the defaults don't really give you a nice looking spreadsheet. I will also note I do not care how it looks; its just data to me but, my bosses must have quality formatting.
Zebra Stripping
$range = 'A3:'.$spreadsheet->getActiveSheet()->getHighestDataColumn.
$spreadsheet->getActiveSheet()->getHighestDataRow();
$conditional1 = new \PhpOffice\PhpSpreadsheet\Style\Conditional();
$conditional1->setConditionType(\PhpOffice\PhpSpreadsheet\Style\Conditional::CONDITION_EXPRESSION)
->setOperatorType(\PhpOffice\PhpSpreadsheet\Style\Conditional::OPERATOR_EQUAL)
->addCondition('MOD(ROW(),2)=0');
$conditional1->getStyle()->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)
->getStartColor()->setARGB('DFDFDF');
$conditional1->getStyle()->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)
->getEndColor()->setARGB('DFDFDF');
$spreadsheet->getActiveSheet()->getStyle($range)->setConditionalStyles([$conditional1]);
The range starts at A3 because I have a logo in row 1 and headers in row 2. Also you can apply as many conditions as you need which is why the final setConditionalStyles is wrapped in an array.
Freeze 1 columns and 2 rows
$sheet->freezePane('B2');
This will freeze that cell B2 and everything to the left and above. Super helpful for long and wide sheets letting you keep your primary_key and headers in view at all times.
Insert Logo in 1st cell
$spreadsheet->getActiveSheet()->insertNewRowBefore(1, 1);
$drawing = new \PhpOffice\PhpSpreadsheet\Worksheet\Drawing();
$drawing->setCoordinates('A1');
$drawing->getShadow()->setVisible(true);
$drawing->setName({{alt text}});
$drawing->setPath(resource_path().{{filepath}});
$drawing->setWorksheet($spreadsheet->getActiveSheet());
$spreadsheet->getActiveSheet()->getRowDimension('1')->setRowHeight(55);
Hiding a sheet
$spreadsheet->getActiveSheet()->setSheetState('veryHidden');
This is one thing I really like to do as I have written some automation that relies on a user making
edits to a spreadsheet and returning the sheet to a specific email address that I can access via api and download and reprocess based on the edits to the sheet. I use this mainly for storing information our erp needs to access records such as a customer_id or contract_number so that when the sheet comes back I don't have to re-query ident info I can just grab it from the hidden sheet. Bonus of using very hidden is a normal user can't unhide it unless they know enough to open the dev window and get in VB script.
Put Filters on all headers
$dimensions = 'A2' . ':' . $spreadsheet->getActiveSheet()->getHighestDataColumn() .
$spreadsheet->getActiveSheet()->getHighestDataRow();
$spreadsheet->getActiveSheet()->setAutoFilter($dimensions);
$autoFilter = $spreadsheet->getActiveSheet()->getAutoFilter();
$autoFilter->showHideRows();
The trick on this one is the last line it won't actually apply the filters until there is some kind of action so I just grabbed a random function that wouldn't have any effect on my sheet.
Auto Width Columns
$colNumber = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::columnIndexFromString(
$spreadsheet->getActiveSheet()->getHighestDataColumn);
for ($col = 1; $col <= $colNumber; $col++) {
$colAlpha = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::stringFromColumnIndex($col);
$spreadsheet->getActiveSheet()->getColumnDimension($colAlpha)->setAutoSize(true);
}
$sheet->calculateColumnWidths();
Making Spreadsheets with multiple tabs
First let me note I have come up with a somewhat standard structure I use whenever I make a spreadsheet and then I feed it through one function that does all of the above things. So here is my standard.
$report = json_encode([
'filepath' => 'Program Size/',
'filename' => $customer_name->company_name.' Program Size '.Carbon::parse('now')->format('Ymd').'.xlsx',
'Type' => 'buildMultiSheet',
'1' => [
'request' => [
'TabName' => 'Program Size',
'header' => array_keys($source[0]),
'body' => $source,
'formatArray'=> [
'aboveHeader'=>['Total Value:',$sum],
'zebra' => 'stripe',
'F:G' => '"$"#,##0.00_-'
]
]
],
'2' => [
'request' => [
'TabName' => 'config_sheet',
'header' => [],
'body' => $queryLog,
'formatArray'=> [
'hidden'=> 1
]
]
]
]);
This structure has every thing you need to make a spreadsheet with multiple tabs. Source for me is generally the result of a DB call. This isn't complete detail for example a tabname longer than 31 characters will freak out so you have to check for it. One of these days I might tidy things up and publish a package to go along side phpspreadsheet but, we will see if/when that ever happens.
$spreadsheet = new Spreadsheet();
$filename = $requestD["filename"];
unset($requestD["filename"]);
$filepath = $requestD["filepath"];
unset($requestD["filepath"]);
$tabCounter = 0;
$spreadsheet->setActiveSheetIndex($tabCounter);
foreach ($requestD as $tab) {
$sheet = $spreadsheet->getActiveSheet();
$body = $tab["request"]["body"];
$spreadsheet->getActiveSheet()->setTitle($tab["request"]["TabName"]);
$headers = $tab["request"]["header"];
$sheet->fromArray($headers, NULL, 'A1');
$sheet->fromArray($body, NULL, 'A2', false);
//processing here, sheet formatting , etc.
$tabCounter++;
$spreadsheet->createSheet();
$spreadsheet->setActiveSheetIndex($tabCounter);
}
//remove last empty sheet before resetting index
$spreadsheet->setActiveSheetIndex(0);
$spreadsheet->removeSheetByIndex($tabCounter);
Well that's all the tips I have. If I have done anything really dumb please do let me know how I can improve.
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 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 :)
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 am using DOMPDF to generate a PDF file from PHP. It works very good.
But now when I click on the link that generates the PDF I want it to take me to the fifth page of the PDF (if there's content on that page). Is there a way to do this?
According to the source code posted on this page, you can use the function Cpdf.openHere:
/**
* specify where the document should open when it first starts
*/
function openHere($style, $a = 0, $b = 0, $c = 0) {
// this function will open the document at a specified page, in a specified style
// the values for style, and the required paramters are:
// 'XYZ' left, top, zoom
// 'Fit'
// 'FitH' top
// 'FitV' left
// 'FitR' left,bottom,right
// 'FitB'
// 'FitBH' top
// 'FitBV' left
$this->numObj++;
$this->o_destination($this->numObj, 'new', array('page' => $this->currentPage, 'type' => $style, 'p1' => $a, 'p2' => $b, 'p3' => $c));
$id = $this->catalogId;
$this->o_catalog($id, 'openHere', $this->numObj);
}