Search code examples
cakephppdfdompdf

CakePdf & Cakephp 3.1.6


I can´t get CakePdf to work, If I would get an error, I could work with it. But nothing really shows up.

CakePhp: 3.1.6

CakePdf: current Version

I added an InvoicesController with this code

<?php
namespace App\Controller;
class InvoicesController extends AppController
{
    // In your Invoices controller you could set additional configs, or override the global ones:
    public function view($id)
    {
        $this->pdfConfig = array(
            'orientation' => 'landscape',
            'download' => true,
            'filename' => 'invoucne.pdf'
        );
    }
}
?>

Also added this to my bootstrap.php

use Cake\Event\EventManager;
EventManager::instance()
    ->on(
        'Controller.initialize',
        function (Cake\Event\Event $event) {
            $controller = $event->subject();
            if ($controller->components()->has('RequestHandler')) {
                $controller->RequestHandler->config('viewClassMap.pdf', 'CakePdf.Pdf');
            }
        }
    );

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

This to my routes.php

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

Also I go my default.ctp in the src/Template/Layout/pdf

<h2>Rendered with default layout</h2>
<?php echo $this->fetch('content'); ?>

And my views in my Template/Invoices/pdf

<html>
<body>
<h1>test</h1>
</body>
</html>

My url looks like:

http://localhost/caketest/invoices/view/1.pdf

I installed it with composer and my plugins lie in vendor/dompdf and vendor/friendsofcake/cakepdf


Solution

  • I had this problem before the solution is to put

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

    before

    Router::scope('/', function ($routes) { //some code}
    

    not after it