John Davidson

php - How to remove watermark on last page in TCPDF

0 comments
Message:


I need to remove a watermark on the last page of my PDF, but I can't detect the last page in the header Yes I can do it in the footer.


 protected $last_page_flag = false;

public function Close() {
$this->last_page_flag = true;
parent::Close();
}

public function Footer(){
if ($this->last_page_flag ){
//this work fine

}
}

//But I can't detect the last page in the header, which is where I put the watermark.

public function Header(){


$img_file = K_PATH_IMAGES.'marca_agua.jpg';
$this->Image($img_file, 12, 80, 185, 180, '', '', '', false, 300, '', false, false, 0);
$this->setPageMark();

//if I do
if ($this->last_page_flag ){
//Do nothing...

}
}

My pdf has a dynamic number of pages and I need to remove the watermark on the last one. How can I detect if it is the last page in the header?


Or how can I insert the watermark in a section other than the header?


Thanks! I am really desperate with this problem because it is the last thing I have left to deliver a job and I have been trying it for two days without a solution.



My Answer:
One way to achieve this is by using the getPageCount() method provided by TCPDF to get the total number of pages in the PDF document. You can then compare the current page number with the total number of pages to determine if it is the last page.

Here's an example of how you can modify your Header() function to check if it is the last page:

php
public function Header() {
$img_file = K_PATH_IMAGES.'marca_agua.jpg';
$this->Image($img_file, 12, 80, 185, 180, '', '', '', false, 300, '', false, false, 0);

// Get the total number of pages
$total_pages = $this->getPageCount();

// Check if it is the last page
if ($this->getPage() == $total_pages) {
// Do nothing
}
}


This code snippet will check if the current page number is equal to the total number of pages in the PDF document. If it is the last page, the watermark will not be inserted in the header.

Alternatively, you can also consider inserting the watermark in the Footer() function instead of the Header() function. This way, you can easily detect the last page and remove the watermark accordingly.

I hope this helps! Let me know if you have any further questions.

Rate this post

5 of 5 based on 1079 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us