TCPDF - Pagenumbers not exactly right aligned

我与影子孤独终老i 提交于 2019-12-04 02:46:51

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! :)

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

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)

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.

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.

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!

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');
}

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.

zeezoo

Try this

$txt='Page '.$this->PageNo().' of '.$this->getNumPages()

working for me

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!