问题
I am extending the TCPDF library to add a Header function which includes a watermark image.
class PdfWrapperDraft extends PdfWrapper {
// TODO: Rename to PdfWrapperWatermark and allow any watermark, not just the DRAFT image.
public function Header()
{
// get the current page break margin
$bMargin = $this->getBreakMargin();
// get current auto-page-break mode
$auto_page_break = $this->AutoPageBreak;
// disable auto-page-break
$this->SetAutoPageBreak(false, 0);
// set background image: in actual practice, all but one of the lines below would be commented out.
$img_file = K_PATH_IMAGES . 'image_demo.jpg'; // This inbuilt sample image works fine.
$img_file = BD . PP . '/images/image_demo.png'; // The sample image also works when copied to another directory.
$img_file = BD . PP . '/images/image_demo.png'; // The sample image still works when converted to PNG.
$img_file = BD . PP . '/images/DRAFT.jpg'; // This watermark image also works fine.
$img_file = BD . PP . '/images/DRAFT.png'; // This is the one I actually want, and it appears only on the first four pages. Why?!
$this->Image($img_file, 0, 0, 210, 297, '', '', '', false, 0, '', false, false, 0);
// restore auto-page-break status
$this->SetAutoPageBreak($auto_page_break, $bMargin);
// set the starting point for the page content
$this->setPageMark();
}
}
As the comments in the code make clear, the sample TCPDF JPEG image works fine as a watermark, appearing on all 52 pages of the PDF document. It continues to work when loaded from a different folder, and when converted to PNG.
My own supplied image also works on all 52 pages of the PDF as a JPEG, but is too heavy to function as a watermark: the text over it is hard to read. However, when I try the PNG that I actually want to use, it works beautifully on the first four pages, and then disappears. What on Earth is going on?
Experimenting with different sizes of image, and different page sizes (A4, A5), sometimes the image will last a few pages more or less, but it always disappears after the first few pages.
image_demo.png:
Width: 1181 pixels
Height: 1772 pixels
Bytes: 3.8 MB
DRAFT.png:
Width: 1181 pixels
Height: 1772 pixels
Bytes: 105.1 kB
来源:https://stackoverflow.com/questions/59720761/tcpdf-watermark-images-work-only-on-first-few-pages-of-output-pdf