0
votes

I am trying to use CakePdf to generate a pdf file within CakePHP. I have a function in the CourseGradesController called viewReport. I want viewReport to allow you to select a student from a select field, and then upon submitting the form, it will generate a PDF with the appropriate data. If I put the data into a table and do not try to make a PDF, the page will display correctly, so I don't think that is the problem. In bootstrap.php, I have

CakePlugin::load('CakePdf', array('bootstrap' => true, 'routes' => true));
Configure::write('CakePdf', array(
    'engine' => 'CakePdf.WkHtmlToPdf',
    'options' => array(
        'print-media-type' => false,
        'outline' => true,
        'dpi' => 96
    ),
    'margin' => array(
        'bottom' => 15,
        'left' => 50,
        'right' => 30,
        'top' => 45
    ),
    'binary' => '/var/www/cakephp/app/Plugin/CakePdf/Vendor/WkHtmlToPdf',
    'orientation' => 'landscape',
    'download' => false
));

I have the WkHtmlToPdf folder moved into /var/www/cakephp/app/Plugin/CakePdf/Vendor/WkHtmlToPdf, so the lib and include folders are in that directory.

In the viewReport function of CourseGradesController, I have

    function viewReport($id = null)
    {
        $this->CourseGrade->id = $id;
            if (!$this->CourseGrade->exists()) 
            {
                throw new NotFoundException(__('Invalid invoice'));
            }
            $this->pdfConfig = array(
                'orientation' => 'portrait',
                'filename' => 'Invoice_' . $id
            );
            $this->set('invoice', $this->CourseGrade->read(null, $id));
        ...

    }

If I navigate to /courseGrades/viewReport, I get the error "Error: The requested address '/cakephp/courseGrades/viewReport' was not found on this server."

If I naviate to /courseGrades/viewReport/1.pdf, then I just see a completely blank screen.

1
A few notes: 1) you should update your previous question with relevant information when possible instead of starting a new question (though this does cover a new problem, presumably the original problem needs to be resolved); 2) Don't require_once dompdf.php, this is an add-on script that uses the dompdf class, not part of the class itself; 3) you shouldn't need to spl_autoload_register('DOMPDF_autoload'); since this happens via dompdf_config.inc.php. - BrianS
As for your CakePHP problems... you show the code for the getStudentReport_pdf action, is it the same for getStudentReport. Also, are you sure $this->Post->read() is returning anything? Try $post = $this->Post->read() then debug($post) to see if the data is being retrieved. FYI, you don't need to specify any parameters to the read() method since you've already set the model ID earlier in the code. - BrianS
I made a new question because I decided to try this method since more people seemed to have used it. I made the changes that you suggested though, and I updated my code in the post, but I am getting the same problem. - Isaac Senior
Well, first try to address the errors. Regarding the "Indirect modification" error see the following note in the migration guide: book.cakephp.org/2.0/en/appendices/… - BrianS

1 Answers

0
votes

I would strongly recommend the use of this plugin for this kind of work.

https://github.com/ceeram/CakePdf