繁体   English   中英

CakePHP 3+与DomPdf有关的问题

[英]CakePHP 3+ issue with DomPdf

我目前在项目控制器类中有一个函数,我正在调用该函数将特定项目导出为pdf。 当我尝试传递从中提取的单个项目页面时,我遇到了问题。 如果我从视图中调用函数,并从export()函数中传入有效的html字符串,它将正确创建pdf。 我只是想知道如何从该ctp模板获取它到我的控制器,以将其创建为pdf。 谢谢。

在我的ProjectsController.php中

public function view($id)
{
    $creator = $this->Auth->user();
    $project = $this->Projects->get($id, [
        'contain' => [
            'Countries', 'Languages', 'Tags',
            'ProjectsLanguages', 'Students'
        ]
    ]);
    $languages = $this->__getLanguageReqs($id);
    $tags = $this->__getTagReqs($id);
    $projSupervisors = $this->__getSupervisorsProjects($id);
    $this->set('locations',$this->__getLocations($id,"project"));

    $this->set('projSupervisors',$projSupervisors);
    if($creator['role_id'] == 2){
        $this->set('is_owner',in_array($creator['id'],array_keys($projSupervisors)));
    }
    else{
       $this->set('is_owner', false);
    }
    $this->set('languages',$languages);
    $this->set('tags',$tags);
    $this->set('project', $project);
    $this->set('_serialize', ['project']);

}
public function export($id = null) {
    $dompdf = new Dompdf();
    $dompdf->loadHtmlFile('/projects/view/' . $id);
    $dompdf->render();
    $dompdf->output();
    $dompdf->stream('project');
}

在我看来

<button class = 'project_edit' onclick = "location.href='/projects/export/<?= h($project->id) ?>'">Export this Project</button>

更新

我明白了。 使用来自view.ctp的相同信息配置了一个新的.ctp,并在文件末尾使用php脚本中的填充数据调用了导出操作。

您可以使用cakephp-dompdf之类的插件,您的代码将更加干净。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM