When using TCPDF together with FPDI templates, some CSS support is lost in the process. The problem are things like borders or background-colors, that end up in layers below the PDF template. TCPDF uses SetLineStyle()
to convert CSS borders/backgrounds to PDF and this seems to be the problem.
For example:
$this->setSourceFile($filename); // /path/to/my/background.pdf
$imported_page = $this->ImportPage(1);
$this->useTemplate($imported_page);
...
$html = '<table style="border: 1px solid #000;"><tr><td style="background-color: #ff0000;">...</td></tr></table>';
$this->writeHTMLCell(45, 25, 160, 29, $html);
doesn't render the CSS borders. As soon as useTemplate()
is removed the borders are there. Analyzing the resulting PDFs with Illustrator shows some interesting things:
PDF layers with useTemplate()
- top to bottom:
- Table/Content layers
- PDF Template layers (group)
- Border and background layers (paths)
PDF layers without useTemplate()
- top to bottom:
- Table/Content layers
- Border and background layers (paths)
When disabling the layer group containing the PDF template in Illustrator, the borders and backgrounds become visible.
Unfortunately we didn't find a way to put the PDF template layer group at the bottom of the stack so everything else renders above it. The only workaround we could come up with, was wrapping the writeHTMLCell()
call in startTemplate()
/ endTemplate()
and finishing up with printTemplate()
:
$this->setSourceFile($filename); // /path/to/my/background.pdf
$imported_page = $this->ImportPage(1);
$this->useTemplate($imported_page);
...
$html = '<table style="border: 1px solid #000;"><tr><td style="background-color: #ff0000;">...</td></tr></table>';
$workaround_template = $this->startTemplate($w, $h);
$this->writeHTMLCell(45, 25, 160, 29, $html);
$this->endTemplate();
$this->printTemplate($workaround_template, $x, $y, $w, $h, 'T', 'L');
So out of curiosity: is this the only way to do it, or is there a way to put the PDF template at the bottom of all things to come?
Thanks in advance!