2
votes

I currently have a function in a project controller class that I am calling to export a specific project to a 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. I am just wondering how I can get it from that ctp template to my controller to be created as a pdf. Thanks.

In my 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.

1

1 Answers

0
votes

You can use a plugin like cakephp-dompdf, your code will be cleaner.