What I did:
-
installed the plugin with
composer require friendsofcake/cakepdf
-
installed dompdf with
composer require dompdf/dompdf
-
loaded the plugin with
./bin/cake plugin load CakePdf
-
changed routes.php (added the last 3 lines)to
$routes->setRouteClass(DashedRoute::class);
$routes->scope('/', function (RouteBuilder $builder) {
$builder->connect('/', ['controller' => 'Pages', 'action' => 'display', 'home']);
$builder->connect('/pages/*', 'Pages::display');
$builder->fallbacks();
});
$routes->prefix('Admin', function (RouteBuilder $routes) {
$routes->fallbacks(DashedRoute::class);
});
//for pdf plugin:
$routes->scope('/', function (\Cake\Routing\RouteBuilder $routes) {
$routes->setExtensions(['pdf']);
});
- added this to bootstrap.php:
Configure::write('CakePdf', [
'engine' => 'CakePdf.DomPdf',
'margin' => [
'bottom' => 15,
'left' => 50,
'right' => 30,
'top' => 45
],
'orientation' => 'landscape',
'download' => true
]);
- created a function in my controller
class UsersController extends AppController
{
public function initialize(): void
{
parent::initialize();
$this->loadComponent('RequestHandler');
}
public function beforeRender(\Cake\Event\EventInterface $event)
{
$this->viewBuilder()->setLayout('haas');
if ($this->request->getAttribute('identity')->userrole_id <> 2) {
return $this->redirect(['prefix'=>false,'controller'=>'Users','action'=>'logout']);
}
}
public function createpdf($id = null)
{
$user = $this->Users->get($id, [
'contain' => ['Userroles', 'Departments', 'Companyroles', 'Userstatuses'],
]);
$this->viewBuilder()->setClassName('CakePdf.Pdf');
$this->viewBuilder()->setOption(
'pdfConfig',
[
'orientation' => 'portrait',
'download' => true, // This can be omitted if "filename" is specified.
'filename' => 'Usertestpdf_' . $id // This can be omitted if you want file name based on URL.
]
);
$this->set('user',$user);
}//rest of users controller not shown....
- created a file named “createpdf.php” in “templates/Admin/users/pdf/”. It has the same content as my view.php
- created a file named “haas.php” in “templates/layout/pdf”:
<!DOCTYPE html>
<html>
<head>
<?= $this->Html->charset() ?>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<?php
echo $this->Html->css('haas.css', ['fullBase' => true]);
echo $this->fetch('css');;
?>
</head>
<body>
<header class="row">
</header>
<div class="">
<div class="text-5xl">
<?php
//die();
echo $this->fetch('content')
?>
</div>
</div>
</body>
</html>
When I go to http://localhost/haas/admin/users/createdpdf/1 the pdf is created, but it is missing all the defined styles from my css named “haas.css”
Can someone see what I did wrong or where im thinking wrong?
If you need more information, please let me know