0

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
Community
  • 1
  • 1
Jon
  • 31
  • 1
  • 5

1 Answers1

1

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.

Kev Wilson
  • 470
  • 6
  • 19
  • can you be more specific? as i tried as you mentioned but still couldn't get it work. Thnaks. – Waseem Akram Aug 30 '16 at 06:55
  • @waseemAkram Sure, tell me what you tried and what results you got and I'll see what I can do. – Kev Wilson Aug 30 '16 at 08:01
  • I followed with [Setting Up cakepdf in cakephp 3](http://www.edsonmm.com/generar-pdf-en-cakephp-con-el-plugin-cakepdf/#cakephp3), and also [From this](http://stackoverflow.com/questions/38277129/how-to-get-cakepdf-to-work-in-cakephp-3?noredirect=1&lq=1), but I am not able to get the pdf generated file but instead i am just able to display my data in browser (Pdf is not getting generated). – Waseem Akram Aug 30 '16 at 08:21
  • i have installed wkhtmltopdf in my vendor folder. – Waseem Akram Aug 30 '16 at 08:22
  • I never got the `WkHtmlToPdf` engine to work, if you read my answer, I swapped the engine for `dompdf` and found it worked a lot easier. Try switching to `dompdf` and following my answer. – Kev Wilson Aug 30 '16 at 08:26
  • i just tried with dompdf as well but i dint get the pdf. i will post below what all i did, please help me if i am wrong anywhere – Waseem Akram Aug 30 '16 at 08:26
  • I used a .pdf extension to my url and i got the pdf generated file. Thanks @kev Wilson that worked for me, i will up vote your answer – Waseem Akram Aug 30 '16 at 08:55
  • No probs happy to help – Kev Wilson Aug 30 '16 at 08:59
  • is it possible to display image in the pdf? – Waseem Akram Aug 30 '16 at 09:02
  • Yeah, if you use the full directory path with a .jpg image – Kev Wilson Aug 30 '16 at 09:06
  • can you give me an example? i did used `Html->image('one.jpg', array('alt' => 'Delete', 'border' => '0', 'width' => '25')); ?>` but couldnt get the image in pdf – Waseem Akram Aug 30 '16 at 09:09
  • `Html->image('path/to/the/images/directory/one.jpg', array('alt' => 'Delete', 'border' => '0', 'width' => '25')); ?>` – Kev Wilson Aug 30 '16 at 09:14