【问题标题】:CakePHP 3+ issue with DomPdfDomPdf 的 CakePHP 3+ 问题
【发布时间】:2016-10-17 01:21:08
【问题描述】:

我目前在项目控制器类中有一个函数,我正在调用它来将特定项目导出为 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');
}

在我看来.ctp

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

更新

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

【问题讨论】:

    标签: php cakephp dompdf


    【解决方案1】:

    你可以使用像cakephp-dompdf这样的插件,你的代码会更干净。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-04-07
      • 2015-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-12
      相关资源
      最近更新 更多