带有FPDI模板和CSS的TCPDF


TCPDF with FPDI templates and CSS

当将TCPDF与FPDI模板一起使用时,在这个过程中会失去一些CSS支持。问题是像边框或背景颜色这样的东西,最终会出现在PDF模板下面的层中。TCPDF使用SetLineStyle()将CSS边框/背景转换为PDF,这似乎是个问题。

例如:

$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); 

不呈现CSS边框。一旦useTemplate()被移除,边界就在那里。使用Illustrator分析生成的PDF显示了一些有趣的事情:

带有useTemplate()的PDF层-从上到下:

  1. 表/内容层
  2. PDF模板图层(组)
  3. 边界层和背景层(路径)

没有useTemplate()的PDF层-从上到下:

  1. 表/内容层
  2. 边界层和背景层(路径)

在Illustrator中禁用包含PDF模板的图层组时,边框和背景将变为可见。

不幸的是,我们没有找到一种方法将PDF模板层组放在堆栈的底部,以便其他所有内容都在其上方呈现。我们唯一能想到的解决方法是,将writeHTMLCell()调用封装在startTemplate()/endTemplate()中,最后使用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');

出于好奇:这是唯一的方法吗?还是有办法把PDF模板放在所有事情的底部?

提前感谢!

解决方案实际上是使用setPageMark()。以下是对我非常有效的方法:

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();
    }       
}

关键是在使用模板后放置setPageMark()。然后,边框将堆叠在模板的顶部,形成PDF。

您尝试过使用setPageMark()-方法吗?