问题
I am using TCPDF to generate a pdf.
My issue is the following line from the Footer() method:
$this->Cell(0, 10, 'Page '.$this->getAliasNumPage().'/'.$this->getAliasNbPages(), 'T', false, 'R');
This is the standard line for inserting the pagenumber in the document's footer but there is a litte space to right in the output. You can see it in example 1 from the website: http://www.tcpdf.org/examples/example_001.pdf
If I insert normal text like this:
$this->Cell(0, 10, 'Foobar', 'T', false, 'R');
the text is perfectly aligned to the right without any problems.
I digged into the source, it seems to be something with the encoding, but I don't really get the point.. can somebody help?
regards
回答1:
i solved it like this:
$this->Cell(0, 0, $this->getAliasRightShift().$this->PageNo().'/'.$this->getAliasNbPages(), 1, 0, 'R');
if you have more than 99 pages, you will again have alignment problems! use something else to generate a pdf of your degree thesis! :)
回答2:
I have solved using:
$txt='Page '.$this->PageNo().' of '.$this->getNumPages()
instead of:
$txt='Page '.$this->getAliasNumPage().' of '.$this->getAliasNbPages()
Italian translation and updates at Isotypelab.org
回答3:
Unfortunately TCPDF align the alias and not the final number that replaces the alias. Using $this->AliasNbPages('{p}') and $this->AliasNumPAge('{n}') won't help a lot. Extra padding will remain in place.
The only solution is to put page numbers instead of aliases.
Original bug at SourceForge.net (TCPDF bug tracker)
回答4:
As my document is no more than 10 pages I have 2 solutions, give it more WIDTH(260 in my case because I have PDF_PAGE_ORIENTATION='L') and align 'R':
$this->Cell(260, 6, 'Page '.$this->getAliasNumPage().' of '.$this->getAliasNbPages(), 0, 0, 'R', 0, '', 0, false, 'T', 'C');
or two cells with WIDTH=0, align 'C' and second Cell with some spaces:
$this->Cell(0, 6, '', 0, 0, 'C', 0, '', 0, false, 'T', 'C');
$this->Cell(0, 6, 'Page '.$this->getAliasNumPage().' of '.$this->getAliasNbPages().' ', 0, 0, 'C', 0, '', 0, false, 'T', 'C');
so play with WIDTH and SPACES if you have more than 10 pages.
This aligned page numbers to the right in my case.
回答5:
I believe the problem is within the aliases. These are just pieces of text of which the width is calculated at the time of insertion, and not the time of replacing them with numbers.
So the text-width is calculated for the string (literally) {np}/{nb}
. And when it is replaced with numbers right at the end 1/9
is smaller than that, whilst 23/109
is larger.
You can replace the NbPages alias used (setAliasNbPages()
?), with something that reflects the width of the total number of pages
íf you can estimate how many that will be. And I believe you can directly use the current page number without using an alias so the problem at least doesn't appear for that alias.
回答6:
getNumPages won't do it. If you have 3 pages, getNumPages() will result in 1/1, 2/2 and 3/3 getAliasNbPages() will result in 1/3, 2/3 and 3/3 so the problem remains!
回答7:
You can add page number after generating, no need of aliases.
Do it at the end just before $pdf->Output(...);
$numPages = $pdf->getNumPages();
for($int=1;$int < $numPages + 1; $int++)
{
$pdf->setPage($int);
$pdf->SetY(-15);
$pdf->SetFont('helvetica', '', 9);
$pdf->writeHTML('Page '.$int.'/'.$numPages,true,false,true,false,'R');
}
回答8:
TCPDF align the alias instead of the real page number. To align to the right, don't use the alias:
$pageNumber = $this->getPage();
$this->Cell(0, 10, 'Page '.$pageNumber, 'T', false, 'R');
Unfortunately for the total number of pages, you have no solution because when your footer code is called, all the pages are not yet inserted.
回答9:
Try this
$txt='Page '.$this->PageNo().' of '.$this->getNumPages()
working for me
来源:https://stackoverflow.com/questions/5485295/tcpdf-pagenumbers-not-exactly-right-aligned