0

I have been trying for a couple of days now to enable PDFs on my web app. However, I can't make it work.

I have the following configured:

// AppController.php
class AppController extends Controller
{

    /**
     * Initialization hook method.
     *
     * Use this method to add common initialization code like loading components.
     *
     * e.g. `$this->loadComponent('Security');`
     *
     * @return void
     */
    public function initialize()
    {
        parent::initialize();

        $this->loadComponent('RequestHandler');
        $this->loadComponent('Flash');
        // some other code for authentication
    }
}



// bootstrap.php (at the end of the file)
Plugin::load('CakePdf', ['bootstrap' => true, 'routes' => true]);
Configure::write('CakePdf', [
    'engine' => 'CakePdf.DomPdf',
    'margin' => [
        'bottom' => 15,
        'left' => 50,
        'right' => 30,
        'top' => 45
    ],
    'download' => true
]);



// routes.php
Router::defaultRouteClass(DashedRoute::class);

// Add PDF Extensions
Router::extensions(['pdf']);

Router::scope('/', function (RouteBuilder $routes) {



// OrdersController.php
public function view($id = null)
{
  // some code

  $this->viewBuilder()->options([
    'pdfConfig' => [
      'title' => 'Work Order PDF',
      'filename' => 'workorder'
    ]
  ]);

  // other code
}

I have a created a subfolder 'pdf' in Template\Orders where I have a view.ctp file. I also have a default.ctp and BootstrapUI.default.ctp in Layout\pdf (I also use FriendsOfCake/bootstrap-ui).

I have already tried WkHtmlToPdf (downloaded binaries and installed it) and DomPdf (used composer in PhpStorm to download and implement it), but it does not work.

I get the message "The requested resource /orders/view/28.pdf was not found on this server."

What am I forgetting?

NCo
  • 1
  • Did you add pdf extention `Router::extensions(['pdf']);` ? – tarikul05 Feb 19 '17 at 17:31
  • Unless you've changed the defaults, that error message doesn't sound as if it would stem from CakePHP, but rather from the server, ie as if your request doesn't even make it to CakePHP. If you actually receive an error from CakePHP, please always post **the _complete_ error**, that is, **including the _full_ stacktrace** (ideally copied from the logs where it is available in a properly readable fashion)! Also show/describe the corresponding context, ie show/highlight the code that actually triggers the error, and please always mention your _exact_ CakePHP version - thanks! – ndm Feb 19 '17 at 17:54
  • @tarikul05 I did add Router::extensions(['pdf']); (see my post). – NCo Feb 19 '17 at 18:45
  • @ndm I think the error is not coming from CakePhp. The only thing I see in logging is "/orders/view/28.pdf - No such file or directory". CakePhp version is 3.3.10 – NCo Feb 19 '17 at 18:48
  • Update: it doesn't seem to work on my development environment, but it does seem to work on a test environment on a separate server. Problem seems to be on my PC. – NCo Feb 19 '17 at 19:22
  • So it's likely a problem with your server, maybe URL rewriting doesn't work (properly). – ndm Feb 19 '17 at 20:38

0 Answers0