3

I have installed CakePdf plugin in app/plugins folder and followed all the documentation possbile, thus my settings are as following:

// config/bootstrap.php

Plugin::load('CakePdf', ['bootstrap' => true, 'routes' => true]);

Configure::write('CakePdf', [
            'engine' => 'CakePdf.WkHtmlToPdf',
            'binary' => '/wkhtmltopdf/bin/wkhtmltopdf',
            'margin' => [
                'bottom' => 15,
                'left' => 50,
                'right' => 30,
                'top' => 45
            ],
            'orientation' => 'landscape',
            'download' => true
]);


// config/routes.php

Router::extensions(['pdf']);

// controller/AppController.php


    public function initialize()
{
    parent::initialize();

    $this->loadComponent('RequestHandler');
    $this->loadComponent('Flash');
    $this->loadComponent('Auth', [
            'authenticate' => ['Form' => ['fields' => ['username' => 'email', 'password' => 'password']]],
            'loginAction' => ['controller' => 'Users', 'action' => 'login'],
            'loginRedirect' => ['controller' => 'Users', 'action' => 'index'],
            'logoutRedirect' => ['controller' => 'Users', 'action' => 'login'],
            'authorize' => 'Controller'
    ]);  
}

Here is how a sample agendaPdf action looks like:

function agendaPdf(){

    $agenda = 'sample agenda';
    $this->viewBuilder()->options([
        'pdfConfig' => [
            'orientation' => 'portrait',
            'filename' => 'agenda_123'
        ]
    ]);

    $this->set('agenda', $agenda);

}

I have PDF layouts done, as well as a PDF folder inside the templates folder for the model's actions, however, if I go to app/users/agendapdf.pdf, I am given the following messages:

The action agendapdf.pdf is not defined in UsersController Error: Create UsersController::agendapdf.pdf() in file: src/Controller/UsersController.php.

I would really like to know what could have went wrong and how I can fix it to work.

WpDoe
  • 476
  • 1
  • 7
  • 22
  • Where exactly is the `Router::extensions(['pdf']);` call being invoked in your `config/routes.php` file? Please show some context around it. – ndm Mar 21 '16 at 17:20
  • 1
    I agree with @ndm that where you have `Router::extensions('pdf']);` matter. You should add it before your `Router::scope();` that pertains to the `agendaPdf()` method. So most likely you are using the default `Router::scope('/', function ($routes) { `, so add it before this. – AKKAweb Mar 21 '16 at 17:59
  • Thank you both a bunch, that was the problem. The `extensions` call was invoked in the end of `config/routes.php`. – WpDoe Mar 22 '16 at 08:49

1 Answers1

1

CakePdf does not include any of the supported PDF engines, so i tried wkhtmltopdf( Refered Link).

Step by Step process:

1. Install wkhtmltopdf binary file in your local or server system (Ubuntu, Window, Mac) - Download link : [wkhtmltopdf][2]

2. Check the installed location of wkhtmltopdf binary file - (i am using    ubuntu so installed location is /usr/local/bin)

3. configure the wkhtmltopdf with cakephp:
    - in : config/bootstrap.php like below:

    Plugin::load('CakePdf', ['bootstrap' => true, 'routes' => true]);

    Configure::write('CakePdf', [
        'engine' => 'CakePdf.WkHtmlToPdf',
        'margin' => [
            'bottom' => 15,
            'left' => 50,
            'right' => 30,
            'top' => 45
        ],
        'orientation' => 'landscape',
        'download' => true
    ]);

    - Create a folder name "pdf" under your working template view folder:
        * for ex: src/template/(working view folder)/pdf (src/template/budget/pdf)

    - Create a file name "view.ctp" under newly created pdf folder under working directory:
        * for ex: src/template/(working view folder)/pdf/view.ctp (src/template/budget/pdf/view.ctp)

    - use below code in working controller - action(view - method)

            $this->viewBuilder()->options([
                'pdfConfig' => [
                    'orientation' => 'portrait',
                    'filename' => 'Invoice_' . $id
                ]
            ]);

        * for ex:

        public function view($id = null)
        {
            $budget = $this->budget->get($id);
            $this->viewBuilder()->options([
                'pdfConfig' => [
                    'orientation' => 'portrait',
                    'filename' => 'Invoice_' . $id
                ]
            ]);
            $this->set('budget', $budget);
        }

        - Hit the controller and action to download as PDF.
            * for ex: http://localhost:8765/projects/view/1.pdf

        - if you are facing "wkhtmltopdf binary is not found or not executable" error. copy your wkhtmltopdf file from "/usr/local/bin" to "/usr/bin"
            * cd /usr/local/bin
            * sudo cp wkhtmltoimage /usr/bin/wkhtmltoimage
            * sudo cp wkhtmltopdf /usr/bin/wkhtmltopdf
Siva
  • 375
  • 3
  • 5