Question

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:

  1. Table/Content layers
  2. PDF Template layers (group)
  3. Border and background layers (paths)

PDF layers without useTemplate() - top to bottom:

  1. Table/Content layers
  2. 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!

Was it helpful?

Solution

Did you try using the setPageMark()-method?

OTHER TIPS

The solution is to use setPageMark() indeed. Here is what worked very well for me:

public function AddPage($orientation = '', $format = '') {
    global $pdf_data_path;
    parent::AddPage($orientation, $format);
    if ($this->use_headed) {
        $this->setSourceFile($pdf_data_path."/headed.pdf");
        $tplidx = $this->importPage(1, '/MediaBox');
        $this->useTemplate($tplidx, 0, 0, 0, 0, true);
        $this->setPageMark();
    }       
}

The key is to place setPageMark() after you used the template. The borders will then be stacked on top of the template in resulting PDF.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top