Page footer centering using MPDF - php

I have this pdf:
How can I make the page numbering centered instead of right alignment?
This is how I added the page numbering:
include("mpdf60/mpdf.php");
$mpdf=new \mPDF('c','A4','','' , 0, 0, 0, 0, 0, 0);
$mpdf->setFooter('{PAGENO} of {nbpg}');
$mpdf->WriteHTML($body);
$mpdf->Output('Packing Slip.pdf','I');
How can I make it center? its the 'setFooter' ?
Im am using mpdf.

You can set an HTML Footer and give the proper format:
$mpdf->SetHTMLFooter('<div style="text-align: center">{PAGENO} of {nbpg}</div>');
SetHTMLFooter() Function

you can try the following:
$mpdf->setFooter('|{PAGENO} of {nbpg}|');
This will center align the page numbers. Any thing on the left of first | will be left aligned and anything on the right of second | will be right alligned.

Related

Insert mpdf TOC with page number for dynamic data PHP

I am using mpdf to create PDF files from data extracted from a database. There are three sections in pdf which i need to display in TOC with their page number on which they are appearing. They can appear on separate page or on a single page as well when data is too short. Below is my code currently using:
$mpdf = new \Mpdf\Mpdf();
$mpdf->SetFont('avenirnext');
$mpdf->SetTitle($title);
$mpdf->SetAuthor('Blavatnik');
$mpdf->WriteHTML($stylesheet, 1);
$mpdf->WriteHTML($stylesheet2, 1);
$mpdf->WriteHTML($stylesheet3, 1);
$mpdf->setAutoTopMargin = 'stretch';
$mpdf->SetHtmlHeader($header, 'OE');
$mpdf->SetHTMLFooter($footer, 'OE');
$mpdf->AddPage('', // L - landscape, P - portrait
'', '', '', '', 20, // margin_left
20, // margin right
5, // margin top
25, // margin bottom
10, // margin header
10); // margin footer
$mpdf->WriteHTML($html);
$mpdf->Output($title . '.pdf', 'd');
I need to display TOC on first page and it also has some designing so I think it has to be done via html as displayed in example below:
https://mpdf.github.io/what-else-can-i-do/table-of-contents.html

How to set full width image in footer of pdf in tcpdf library codeigniter?

I'm using the TCPDF library to create a .pdf file. I have to add an image in the footer which should cover 100% of the width, but it's coming in the center with equal margin from left and right. I want to remove those margins and set full width to the footer image. The code I'm using to set the footer is below. You can also see in the attach image how my image appears in the footer:-
$footer_image_file = './images/logo/footer.png';
$footer_logo_html = '<div style="width:100opx !important;height:100px;"><img width:"1000px;" src="' . $footer_image_file . '" /></div>';
$pdf->writeHTML($footer_logo_html, true, 0, true, 0);
Try use writeHTMLCell() for this. Set 1st parameter 21 - A4 width (I set cm as unit of measure), and 4 as A4 height - image height:
$pdf->writeHTMLCell(21, '', 0, 29.7 - 4, $footer_logo_html, 0, 1, false, true, 'L', false);
Result:
I use Your .png, so it have white space on the edges =)

TCPDF + pure HTML ... Create table of contents

I want to print HTML to PDF using TCPDF so I do this:
$html = '<h1>Hello</h1>
How are you?
<h2>Answer</h2>
I am well, thanks';
// ... etc. Long HTML document.
$pdf->writeHTML($html);
The question is if there is a way how to add a table of contents. If TCPDF can recognize page numbers of headings.
I also sometimes use HTML page breaks:
<div style="page-break-after: always"><span style="display:none"> </span></div>
I understand that I need bookmarks. But in this case TCPDF would have to create them automatically. If I add an ID to html-heading-tag, TCPDF probably does not find it and cannot use it.
<h1 id="abc123">header1</h1>
// but following does not do anything:
$pdf->addTOC()
Since the ToC page is created as the very last it will show that as the last page.
So instead use
$this->Cell(0, 12, ' '.$this->getAliasNumPage().' of '.$this->getAliasNbPages(), 0, false, 'C', 0, '', 0, false, 'T', 'M');
//It will show page numbers

FPDF borderlines

I used to make some very simple PDF with this library, FPDF, and now I'm trying to make a "result page" and it have to show something like this image, but with a border surrounding it all, not every cell.
I already got that in FPDF, made will Cells (don't know if it's the best way for doing it), and code goes like this (for the moment I added the results by hand, just to figure out how it will look like):
//SimpleTable
function SimpleTable() {
$this->Cell(280,15,"Inscription Number: 1",0);
$this->Cell(265,15,"Date: 28/03/2008",0);
$this->Ln();
$this->Cell(265,15,"Name and Surname: Name Surname ",0);
$this->Cell(265,15,"",0);
$this->Ln();
$this->Cell(280,15,"Address: Address",0);
$this->Cell(265,15,"",0);
$this->Ln();
$this->Cell(280,15,"Zip Code: Zip Code",0);
$this->Cell(280,15,"City: City",0);
$this->Ln();
$this->Cell(280,15,"Year of birth: Birthday",0);
$this->Cell(280,15,"Age: Age",0);
$this->Ln();
$this->Cell(280,15,"VIP: No - First Time: Yes",0);
$this->Cell(280,15,"School: My school",0);
}
Hope you can help me how to border all thouse results in one, I'm not beign able to do it, and didn't find anything.
Thanks again for your time, as always!!!
You can use
Rect(StartX, StartY, Width, Height, Options)
Options
D - Draw Line
F - Fill
DF or FD - Draw Line and Fill
Example $pdf->Rect(0, 0, 210, 100, 'D');
//The BorderBox
$actual_position_y = $pdf->GetY();
$pdf->SetFillColor(255, 255, 255);
$pdf->SetDrawColor(0, 0, 0);
$pdf->Cell($your_content_width, $your_content_heigth, "", 1, 1, 'C');
//Your actual content
$pdf->SetXY($yourLeftosition, $actual_position_y);
$pdf->Cell($cellwidth, $cellheigth, "Inscription Number", 0, 1, 'C');
Thanks for your time, but I finally found the way to make it. On border option from the cell, I added "LT", "RT"... and I could made a border-line that surrounds all (may not the best way to make it, if you know another and better way to make it, I would appreciate that).
Hope this can help other people that wishes to do the same!
Your should first draw an empty Cell with a border surrounding it, then you can store your Y Position and reset the PDF positions and begin to draw your cells within your border Cell.
Like this:
//The BorderBox
$actual_position_y = $pdf->GetY();
$pdf->SetFillColor(255, 255, 255);
$pdf->SetDrawColor(0, 0, 0);
$pdf->Cell($your_content_width, $your_content_heigth, "", 1, 1, 'C');
//Your actual content
$pdf->SetXY($yourLeftosition, $actual_position_y);
$pdf->Cell($cellwidth, $cellheigth, "Inscription Number", 0, 1, 'C');
...
Hope this helps :)
Use FancyRow plugin of fpdf,to give the border or create the box If you need check the link here, it might help you. and the another plugin you can use here is table with multicell, check this link.
None of the above works.
it can be done with 4th param in cell() method.
4th parameter you can have border as :
L: left
T: top
R: right
B: bottom
You can provide them in any order with comma separate.
So in your case you can have top row with "L,T,R" as 4th parameter like below:
$this->Cell(280,15,"Inscription Number: 1","L,T,R");
$this->Cell(265,15,"Date: 28/03/2008","L,T,R");
And for middle part only "L,R" only
$this->Cell(265,15,"Name and Surname: Name Surname ","L,R");
$this->Cell(265,15,"","L,R");
And bottom one as
$this->Cell(280,15,"VIP: No - First Time: Yes","L,R,B");
$this->Cell(280,15,"School: My school","L,R,B");

TCPDF with FPDI templates and CSS

When using TCPDF together with FPDI templates, some CSS support is lost in the process. The problem are things like borders or background-colors, that end up in layers below the PDF template. TCPDF uses SetLineStyle() to convert CSS borders/backgrounds to PDF and this seems to be the problem.
For example:
$this->setSourceFile($filename); // /path/to/my/background.pdf
$imported_page = $this->ImportPage(1);
$this->useTemplate($imported_page);
...
$html = '<table style="border: 1px solid #000;"><tr><td style="background-color: #ff0000;">...</td></tr></table>';
$this->writeHTMLCell(45, 25, 160, 29, $html);
doesn't render the CSS borders. As soon as useTemplate() is removed the borders are there. Analyzing the resulting PDFs with Illustrator shows some interesting things:
PDF layers with useTemplate() - top to bottom:
Table/Content layers
PDF Template layers (group)
Border and background layers (paths)
PDF layers without useTemplate() - top to bottom:
Table/Content layers
Border and background layers (paths)
When disabling the layer group containing the PDF template in Illustrator, the borders and backgrounds become visible.
Unfortunately we didn't find a way to put the PDF template layer group at the bottom of the stack so everything else renders above it. The only workaround we could come up with, was wrapping the writeHTMLCell() call in startTemplate() / endTemplate() and finishing up with printTemplate():
$this->setSourceFile($filename); // /path/to/my/background.pdf
$imported_page = $this->ImportPage(1);
$this->useTemplate($imported_page);
...
$html = '<table style="border: 1px solid #000;"><tr><td style="background-color: #ff0000;">...</td></tr></table>';
$workaround_template = $this->startTemplate($w, $h);
$this->writeHTMLCell(45, 25, 160, 29, $html);
$this->endTemplate();
$this->printTemplate($workaround_template, $x, $y, $w, $h, 'T', 'L');
So out of curiosity: is this the only way to do it, or is there a way to put the PDF template at the bottom of all things to come?
Thanks in advance!
The solution is to use setPageMark() indeed.
Here is what worked very well for me:
public function AddPage($orientation = '', $format = '') {
global $pdf_data_path;
parent::AddPage($orientation, $format);
if ($this->use_headed) {
$this->setSourceFile($pdf_data_path."/headed.pdf");
$tplidx = $this->importPage(1, '/MediaBox');
$this->useTemplate($tplidx, 0, 0, 0, 0, true);
$this->setPageMark();
}
}
The key is to place setPageMark() after you used the template. The borders will then be stacked on top of the template in resulting PDF.
Did you try using the setPageMark()-method?

Categories