简体   繁体   English

CakePHP 3+与DomPdf有关的问题

[英]CakePHP 3+ issue with DomPdf

I currently have a function in a project controller class that I am calling to export a specific project to a pdf. 我目前在项目控制器类中有一个函数,我正在调用该函数将特定项目导出为pdf。 I am running into problems when I try to pass that single project page that I am pulling from. 当我尝试传递从中提取的单个项目页面时,我遇到了问题。 If I call function from my view and pass in a string of valid html from my export() function it will create a pdf correctly. 如果我从视图中调用函数,并从export()函数中传入有效的html字符串,它将正确创建pdf。 I am just wondering how I can get it from that ctp template to my controller to be created as a pdf. 我只是想知道如何从该ctp模板获取它到我的控制器,以将其创建为pdf。 Thanks. 谢谢。

In my ProjectsController.php 在我的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');
}

In my view.ctp 在我看来

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

Update 更新

I got it figured out. 我明白了。 Configured a new .ctp with the same information from my view.ctp and called an export there with the populated data in a php script at the end of my file. 使用来自view.ctp的相同信息配置了一个新的.ctp,并在文件末尾使用php脚本中的填充数据调用了导出操作。

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

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

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