I'm using the follwing TCPDF code to generate PDFs with the writeHTML function. I have the page number footer function which puts page numbers at the bottom of each page as the pdf total pages grows. I'm trying to find a way at the end of creating all the pages to determine how many total pages the produced document has so that i can store that information into a variable and submit that data to a DB.
I've tried:
$total = $pdf->getAliasNbPages();
but doesnt work, Any ideas?
Thanks
// PAGE NUMBERED FOOTER
class MYPDF extends TCPDF {
// Page footer
public function Footer() {
// Position at 15 mm from bottom
$this->SetY(-15);
// Set font
$this->SetFont('Calibri', '', 8);
// Page number
$pageNumbers = 'Page '.$this->getAliasNumPage().' of '.$this->getAliasNbPages();
$this->Cell(0, 10, $pageNumbers, 0, false, 'C', 0, '', 0, false, 'T', 'M');
}
}
$html = 'html content';
$pdf->writeHTML($html, true, false, true, false, '');
$pdf->Output('file.pdf', 'I');
Use the getNumPages function instead:
$total = $pdf->getNumPages();
Note that this counts the amount of pages that have been created so far using the Addpage() function. So if you want the total amount of pages declare it after your last use of Addpage()
$pdf->Cell(0, 10,'{:ptp:}', 0, false, 'C', 0, '', 0, false, 'T', 'M');
To get the total number of pages, use $this->getAliasNbPages() as chown in this example https://tcpdf.org/examples/example_003/
Related
When I hide header with
$this->setPrintHeader(false);
it still leaves an empty gap (space) from the top. How to remove the gap so the body would appear from the beginning of page?
EDIT1: this the prestashop function I use
public function writePage()
{
$this->SetTopMargin(-50);
$this->SetHeaderMargin(-55);
$this->setPrintHeader(false);
$this->SetFooterMargin(21);
$this->setMargins(10, 50, 10);
$this->SetAutoPageBreak(true, 40);
$this->AddPage();
$this->writeHTML($this->content, true, false, true, false, '');
}
EDIT 2: this combination works for me
$this->setPrintHeader(false);
$this->SetTopMargin(0);
$this->setMargins(10, 20, 10); <- Now I can set margins as I want
Apart from not printing the header you also need to set margins to 0, so in this case:
$this->SetTopMargin(0);
Note that you can manipulate the margin of the header too with $this->SetHeaderMargin();
Edit: you can use negative values too.
To display how many pages are in this pdf you can actually use
$pdf->AliasNbPages('{customNb}');
$pdf->Cell(0, 10, 'Page '.$this->PageNo().'/{customNb}', 0, 0, 'C');
This should print something like e.g.
Page 1/5
What if i'm using a MultiCell, how can I use AliasNbPages there ?
$pdf->MultiCell(34, 5, 'Page '.$pdf->PageNo() . '/{customNb}');
This returns
Page 1/{customNb}
I tried to pass it like sample above, also as a part of a variable
$foo = 'Page '.$pdf->PageNo() . '/{customNb}';
$pdf->MultiCell(34, 5, $foo);
But this all is not working as expacted. Is there an other way to solve this ???
Ok, I guess I found a solution....
obviously there is a problem using custom name for pageiterator.
If I simply call
$pdf->AliasNbPages();
and than use
$pdf->MultiCell(34, 5, 'Page '.$pdf->PageNo() . '/{nb}');
everything works like expacted.
I am using TCPDF to create simple pdf document.
I am creating a page and adding link using below code
$pdf->addTOCPage();
$link = $pdf->AddLink();
$pdf->SetLink($link, 0, -1);
Now link is set successfull.But to navigate to that page what should I add ?
I tried below code , but it does nothing,
Return to TOC
// Create a fixed link to the first page using the * character
$index_link = $pdf->AddLink();
$pdf->SetLink($index_link, 0, '*1');
$pdf->Cell(0, 10, 'Link to INDEX', 0, 1, 'R', false, $index_link);
http://www.tcpdf.org/examples/example_045.phps
update -
refer to this function addHtmlLink() in tcpdf library.
You can add a internal link through this
$pdf->addHtmlLink('#'.$index_link, 'hello');
where 'hello' begin the name of anchor and and first param being identifier to the link.
In your case
$pdf->addHtmlLink('#'.$link, 'Whatever you like to name it');
$html = 'link name';
$pdf->writeHTML($html, true, false, true, false, '');
I'm using the TCPDF library to generate PDFs with PHP/HTML. The docs can be found here: http://www.tcpdf.org/doc/code/classTCPDF.html.
I am trying to create a table of contents (as demonstrated in example 59 on the TCPDF site) but I'm experiencing a few issues:
1) The Table of Contents page number is showing as the last page of the document. (8 of 8 when in reality it's 2 of 8, comes after a cover page).
2) The page numbers on the other pages are not adjusting. The page after the TOC should be 3 of 8, but instead says 2 of 8.
3) The table of contents has the correct page numbers for the bookmarks, but these numbers don't match the page numbers on those pages (related to issue #2).
How I'm generating bookmarks:
I'm generating bookmarks for each page by calling the Bookmark() method after each added page.
$pdf->AddPage();
$pdf->Bookmark('Chapter 1', 0, 0, '', 'B', array(0,64,128));
How I'm generating the table of contents:
This is ripped directly from Example 59 linked above. In that example, the scenario is slightly different as it does not have a cover page.
// add a new page for TOC
$pdf->addTOCPage();
// write the TOC title and/or other elements on the TOC page
$pdf->SetFont('times', 'B', 16);
$pdf->MultiCell(0, 0, 'Table Of Content', 0, 'C', 0, 1, '', '', true, 0);
$pdf->Ln();
$pdf->SetFont('helvetica', '', 10);
// define styles for various bookmark levels
$bookmark_templates = array();
/*
* The key of the $bookmark_templates array represent the bookmark level (from 0 to n).
* The following templates will be replaced with proper content:
* #TOC_DESCRIPTION# this will be replaced with the bookmark description;
* #TOC_PAGE_NUMBER# this will be replaced with page number.
*
* NOTES:
* If you want to align the page number on the right you have to use a monospaced font like courier, otherwise you can left align using any font type.
* The following is just an example, you can get various styles by combining various HTML elements.
*/
// A monospaced font for the page number is mandatory to get the right alignment
$bookmark_templates[0] = '<table border="0" cellpadding="0" cellspacing="0" style="background-color:#EEFAFF"><tr><td width="155mm"><span style="font-family:times;font-weight:bold;font-size:12pt;color:black;">#TOC_DESCRIPTION#</span></td> <td width="25mm"><span style="font-family:courier;font-weight:bold;font-size:12pt;color:black;" align="right"> #TOC_PAGE_NUMBER#</span></td></tr></table>';
$bookmark_templates[1] = '<table border="0" cellpadding="0" cellspacing="0"><tr><td width="5mm"> </td><td width=" 150mm"><span style="font-family:times;font-size:11pt;color:green;">#TOC_DESCRIPTION#</span></td><td width="25mm"><span style="font-family:courier;font-weight:bold;font-size:11pt;color:green;" align="right">#TOC_PAGE_NUMBER#</span></td></tr ></table>';
$bookmark_templates[2] = '<table border="0" cellpadding="0" cellspacing="0"><tr><td width="10mm"> </td><td width=" 145mm"><span style="font-family:times;font-size:10pt;color:#666666;"><i>#TOC_DESCRIPTION#</i></span></td><td width="25mm "><span style="font-family:courier;font-weight:bold;font-size:10pt;color:#666666;" align="right">#TOC_PAGE_NUMBER#</span ></td></tr></table>';
// add other bookmark level templates here ...
// add table of content at page 1
// (check the example n. 45 for a text-only TOC
$pdf->addHTMLTOC(2, 'INDEX', $bookmark_templates, true, 'B', array(128,0,0));
// end of TOC page
$pdf->endTOCPage();
How I'm getting the page numbers for the footers:
// Page footer
public function Footer() {
$pageN = $this->PageNo();
if($pageN === 1){
// Do nothing
} else {
// Position at 15 mm from bottom
$this->SetY(-15);
// Set font
$this->SetFont($helvetica_light, '', 10);
$this->setTextColor(255,255,255);
// Page number
$this->Cell(0, 12, ' '.$this->PageNo().' of '.$this->getNumPages(), 0, false, 'C', 0, '', 0, false, 'T', 'M');
}
}
Basically, I'd like the Table of Contents to fall on page 2, with a footer that says Page 2, all subsequent page numbers to be labeled correctly. How can I do this? I can clarify the question/code if needed.
Any help is appreciated :)
PageNo() is a bit funky when it comes to calculating pages. It outputs what page it is in order of creation, and since the ToC page is created as the very last it will show that as the last page.
So instead use
// Page number
$this->Cell(0, 12, ' '.$this->getAliasNumPage().' of '.$this->getAliasNbPages(), 0, false, 'C', 0, '', 0, false, 'T', 'M');
The rest of the code you provided, like the check if it is the first page, should work with PageNo().
With PHP application, I have to generate single PDF documents, from set of images.
Which is best way to achieve this? Can I use TCPDF library, and can you give me some example?
The easiest way is to use TCPDF (http://www.tcpdf.org) and set the images as full background as on the example n. 51.
For example:
// disable auto-page-break
$pdf->SetAutoPageBreak(false, 0);
// set image 1
$pdf->AddPage();
$this->Image('image_demo1.jpg', 0, 0, 210, 297, '', '', '', false, 300, '', false, false, 0);
$this->setPageMark();
// set image 2
$pdf->AddPage();
$this->Image('image_demo2.jpg', 0, 0, 210, 297, '', '', '', false, 300, '', false, false, 0);
$this->setPageMark();
// ...
you can use dompdf.You can find a copy of the original documentation at this link
I don't know your requirements but ImageMagick could probably do the trick. I believe that they also have a php wrapper available for all the image manipulation/conversion functions.