【发布时间】:2014-09-17 23:16:26
【问题描述】:
我想用 DOMPdf 创建一个 PDF。我的 PDF 模板位于
themes/Template/
themes/Template/templates
themes/Template/templates/Pdf
themes/Template/templates/Layout
但是我收到一个找不到模板的错误。
根据这个问题silverstripe Sitetree onAfterWrite - renderWith Error: Template not found,我将模板放入mysite/templates/Pdf,它可以工作。
有没有办法将模板保存在主题文件夹中?
这是我的代码。
public function createPdf(){
$pdf = new SS_DOMPDF();
$data = array(
);
$pdf->setHTML($this->customise($data)->renderWith('InvOffPdf'));
$pdf->render();
$pdfFilename = str_replace(array(' ', '#'), '', $this->Title) . '.pdf';
$pdfFolder = 'clientcenter/' . $this->Client()->CID . '-' . $this->Client()->Random;
$pdf->toFile($pdfFilename, $pdfFolder);
$filename = 'assets/' . $pdfFolder . '/' . $pdfFilename;
$file = File::get()->Filter('Filename', $filename)->First();
if( $file ){
$file->ClientID = $this->ClientID;
$file->ShowInSearch = 0;
$file->write();
$this->Download = '<a href="' . $filename . '">PDF herunterladen</a>';
$this->write();
}
}
编辑: 我试过 spekulatius 的解决方案。但我做错了什么。
$pdfTemplate = SSViewer::setTemplateFile('Layout', 'InvOffPdf');
$pdf->setHTML($this->customise($data)->renderWith($pdfTemplate));
这是我收到的错误
[Strict Notice] Non-static method SSViewer::setTemplateFile() should not be called statically, assuming $this from incompatible context
正确的语法是什么?
【问题讨论】:
-
您确定
Template是活动主题吗?renderWith只会查看当前处于活动状态的主题文件夹。 -
是的,它在活动主题中
标签: silverstripe