Jon
Jon

Reputation: 31

How to get CakePdf to work in CakePHP 3?

I found this similar post but I am still having trouble: How to get CakePdf to work in CakePHP 3.x?

Can anyone help me figure out what I'm doing wrong or what I'm missing?

// config/bootstrap.php
    Plugin::load('CakePdf', ['bootstrap' => true, 'routes' => true]);
    Configure::write('CakePdf', [
        'engine' => 'CakePdf.WkHtmlToPdf',
        'binary' => 'vendor\wkhtmltopdf\bin\wkhtmltopdf.exe',
        'margin' => [
            'top' => 45,
            'left' => 50,
            'bottom' => 15,
            'right' => 30
        ],
        'download' => true
    ]);



// config/routes.php
Router::extensions(['pdf']); // This is above the Router::scope()



// controller/AppController.php
public function initialize()
{
        parent::initialize();
        $this->loadComponent('Flash');
        $this->loadComponent('RequestHandler');
        ... //more code
}


// action for pdf
public function view($id = null)
{
    ... //more code
        $this->viewBuilder()->options([
            'pdfConfig' => [
                'title' => 'Supported Cameras',
                'filename' => 'SupportedCamerasList'
            ]
        ]);
        .. //more code
    }

}

I'm getting this error when I use this url (localhost/cameras/view/1.pdf):

    ( ! ) Fatal error: [Cake\View\Exception\MissingTemplateException] Template file "Error\pdf\error500.ctp" is missing. 
0 C:\wamp\www\camerasapp\vendor\cakephp\cakephp\src\View\View.php(584): Cake\View\View->_getViewFileName('error500') 
1 C:\wamp\www\camerasapp\vendor\friendsofcake\cakepdf\src\View\PdfView.php(103): Cake\View\View->render('error500', 'error') 
2 C:\wamp\www\camerasapp\vendor\cakephp\cakephp\src\Error\ExceptionRenderer.php(356): CakePdf\View\PdfView->render('error500', 'error') 
3 C:\wamp\www\camerasapp\vendor\cakephp\cakephp\src\Error\ExceptionRenderer.php(325): Cake\Error\ExceptionRenderer->_outputMessageSafe('error500') 
4 C:\wamp\www\camerasapp\vendor\cakephp\cakephp\src\Error\ExceptionRenderer.php(327): Cake\Error\ExceptionRenderer->_outputMessage('error500') 
5 C:\wamp\www\camerasapp\vendor\cakephp\cakephp\src\Error\ExceptionRenderer.php(192): Cake\Error\ExceptionRenderer->_outputMessage('fatalError') 
6 C:\wamp\www\camerasapp\vendor\cakephp\cakephp\src\Error\ErrorHandler.php(144): Cake\Error\ExceptionRenderer->render() 
7 C:\w in C:\wamp\www\camerasapp\vendor\cakephp\cakephp\src\Error\ErrorHandler.php on line 156

Upvotes: 0

Views: 3115

Answers (1)

Kev Wilson
Kev Wilson

Reputation: 469

I actually had the same problems today. The error you are seing there is about missing error templates. You should copy the templates in src/Template/Error into src/Template/Error/pdf and you should see the actual error.

I suspect the error it's trying to show you in Missing X server, thats what it was for me. I tried several hacks to get it working, but in the end decided to try the other engines.

I got the dompdf enigine working first try and didn't look back. Maybe it's something you could try.

You should have the following files in place:

src/Template/Cameras/pdf/view.ctp

src/Template/Layout/pdf/default.ctp

These will be your pdf layout and pdf version of your view.

run composer require dompdf/dompdf or php composer.phar require dompdf/dompdf depending on your setup.

Replace your bootstrap.php config with:

Plugin::load('CakePdf', ['bootstrap' => true, 'routes' => true]);
Configure::write('CakePdf', [
    'engine' => 'CakePdf.dompdf',
    'margin' => [
        'top' => 45,
        'left' => 50,
        'bottom' => 15,
        'right' => 30
    ],
    'download' => true
]);

If you now browse to localhost/cameras/view/1.pdf and you should see your pdf version. You will have to mess around with the layout and add some css to you view to get the styling right.

Upvotes: 1

Related Questions