TCPDF - remove empty gap when header is hidden - php

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.

Related

TCPDF getAliasNbPages - Get total pages of produced file

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/

TCPDF table of contents and issues with page numbers

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().

right align logo in tcpdf header

I have the following tcdpf syntax in my php document:
$title="My Heading";
$obj_pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.$title, PDF_HEADER_STRING, array(0,64,255), array(0,64,128));
The logo appears on the left, how can I right align the logo and left align $title?
You must define custom header to align image.
This example
shows how to do this.
To align your logo right, you must set palign attribute to 'R' in image definition.
$this->Image($image_file, 10, 10, 15, '', 'JPG', '', 'T', false, 300, 'R', false, false, 0, false, false, false);
Note that, your header data string position may not be what you want as. You will probably
need to play with several attributes, but in basic, image alignment can be done only via
overriding default Header() function as far as I know.
Check also Image() function.

TCPDF 2 columns same amount of text?

Hi I'm using tcpdf to generate a dynamic pdf for an e-zine i work for. But i have a problem, When i write the body text, I need to have the same amount of text per column and i use 2 of them. It writes my html text perfectly including images and everything, but at the end it doesn't align the text of both columns...
I'm new to tcpdf and i don't have any ideas where to start looking for a solution... any ideas how can i do this?
I'm using this function to write the text, a correction or a mockup for a solution would be greatly appreciated.
/**
* Print chapter body
* #param $file (string) name of the file containing the chapter body
* #param $mode (boolean) if true the chapter body is in HTML, otherwise in simple text.
* #public
*/
public function PartBody($text, $mode=false) {
$this->selectColumn();
// get esternal file content
$content = $text;
// set font
$this->SetFont('times', '', 9);
$this->SetTextColor(50, 50, 50);
// print content
if ($mode) {
// ------ HTML MODE ------
$this->writeHTML($content, true, false, true, false, 'J');
} else {
// ------ TEXT MODE ------
$this->Write(0, $content, '', 0, 'J', true, 0, false, true, 0);
}
$this->Ln();
}

How to create PDF documents from image files, using PHP

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.

Categories