0

I installed the plugins of CakePdf using composer. So in my vendor folder I have CakePdf plugin & other dependencies.

I downloaded wkhtmltopdf and installed it in a directory. The directory is C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe

Next by running the command ./bin/cake plugin load CakePdf -b I added the plugin in src/application.php

$this->addPlugin('CakePdf', ['bootstrap' => true]);

Then I wrote the below line in config/routes.php just before Router::scope

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

And in config/bootstrap.php

Configure::write('CakePdf', [
    'engine' => [
        'className' => 'CakePdf.WkHtmlToPdf',
        'binary' => 'C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe',
        'options' => [
            'print-media-type' => false,
            'outline' => true,
            'dpi' => 96
        ],
    ],
]);

Now in my abc controller view action I wrote

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

I have also created view.ctp inside src/Templates/abc/pdf/view.ctp and a default.pdf layout inside src/Templates/Layout/pdf/default.ctp .

Now when I go to localhost/abc/view/1.pdf, I get an error saying Failed to load PDF document!

Failed to load the PDF document

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

Community
  • 1
  • 1
nishan
  • 51
  • 1
  • 4
  • You have `'filename' => 'Invoice_' . $id.'.pdf'` but you are looking for `localhost/abc/view/1.pdf`. – Tigger Dec 02 '18 at 10:15
  • @Tigger Can't I have the file name as my wish! – nishan Dec 02 '18 at 10:27
  • You can set the file name to what ever valid characters you want. Re-read what I wrote. The file name you have set (as an example) `Invoice_1.pdf` but that is nothing at all like `1.pdf` (what you are looking for). – Tigger Dec 02 '18 at 10:30
  • No, the options are correct as they are, the `filename` option will be used in the response's `Content-Disposition` header, it has nothing to do with the request URL. Make sure that you have debug mode enabled, and check your logs for possible errors. Also download the invalid PDF and look at its source to figure what output exactly is being produced. – ndm Dec 02 '18 at 12:44
  • That's what I was wondering about `'filename'`! Debug mode is enabled, but no error logs. Downloaded the pdf and opened it in notepad, It's plain Html. @ndm – nishan Dec 04 '18 at 03:53
  • That sounds like the plugin isn't loaded. Make sure that the `$this->addPlugin()` call in your `Application` class' `bootstrap()` method is actually being invoked. – ndm Dec 04 '18 at 11:11
  • Here's my all settings related to CakePdf `https://gist.github.com/azmain/e2456a8cf769bc10706a575332db2c84` How can I make sure that if this plugin is being called? – nishan Dec 04 '18 at 13:23

1 Answers1

1

In my case I had AdminLTE layout and template files set up in AppController's beforeRender method so it was overriding my layout and template just before providing the view.

I altered beforeRender method of AppController class to something like this:

    public function beforeRender(Event $event)
    {
        switch ($this->request->params['_ext']) {
          case 'pdf':
            break;
          default:
            $this->viewBuilder()->setTheme('AdminLTE');
            $this->viewBuilder()->setClassName('AdminLTE.AdminLTE');
            break;
        }
    }

and CakePdf plugin started to generate PDF's like it should.

kuvukala
  • 66
  • 7
  • what version of cakephp do you use? I would guess 3? It doesn't seem to work with 4 – gabrielkolbe Jul 01 '20 at 18:46
  • Yes I was using 3.x don't remember which one. I'm not sure if AdminLTE package is compatible with 4.x – kuvukala Jul 05 '20 at 07:54
  • Thanks @kuvukala I'm using cakePHP 4 and the AdminLTE theme and I could not get the PDF rendered. Using your idea, for the time being what I've done is to reset the view builder class name, in the controller where I want a PDF printed (in my application it's just one controller which provides PDFs), like this: public function beforeRender(\Cake\Event\EventInterface $event) { parent::beforeRender($event); if ( $this->getRequest()->getParam('_ext','') === 'pdf' ) { $this->viewBuilder()->setClassName('CakePdf.Pdf'); } } – Ruth Oct 02 '20 at 14:12