如何在CakePHP 3.x中使用CakePdf?

3

我已经在app/plugins文件夹中安装了CakePdf插件,并按照所有可能的文档进行了操作,因此我的设置如下:

// config/bootstrap.php

Plugin::load('CakePdf', ['bootstrap' => true, 'routes' => true]);

Configure::write('CakePdf', [
            'engine' => 'CakePdf.WkHtmlToPdf',
            'binary' => '/wkhtmltopdf/bin/wkhtmltopdf',
            'margin' => [
                'bottom' => 15,
                'left' => 50,
                'right' => 30,
                'top' => 45
            ],
            'orientation' => 'landscape',
            'download' => true
]);


// config/routes.php

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

// controller/AppController.php


    public function initialize()
{
    parent::initialize();

    $this->loadComponent('RequestHandler');
    $this->loadComponent('Flash');
    $this->loadComponent('Auth', [
            'authenticate' => ['Form' => ['fields' => ['username' => 'email', 'password' => 'password']]],
            'loginAction' => ['controller' => 'Users', 'action' => 'login'],
            'loginRedirect' => ['controller' => 'Users', 'action' => 'index'],
            'logoutRedirect' => ['controller' => 'Users', 'action' => 'login'],
            'authorize' => 'Controller'
    ]);  
}

以下是示例 agendaPdf 动作的样子:
function agendaPdf(){

    $agenda = 'sample agenda';
    $this->viewBuilder()->options([
        'pdfConfig' => [
            'orientation' => 'portrait',
            'filename' => 'agenda_123'
        ]
    ]);

    $this->set('agenda', $agenda);

}

我已经完成了PDF布局,以及模型操作的templates文件夹内的PDF文件夹,但是,如果我访问app/users/agendapdf.pdf,我会得到以下信息:

在UsersController中未定义agendapdf.pdf操作 错误:在文件src/Controller/UsersController.php中创建UsersController :: agendapdf.pdf()。

我真的很想知道出了什么问题,以及如何修复它使其正常工作。

在你的 config/routes.php 文件中,Router::extensions(['pdf']); 调用确切地在哪里被调用了?请展示一些相关的上下文。 - ndm
1
我同意@ndm的观点,Router::extensions('pdf']);应该放在正确位置。你应该将其添加到与agendaPdf()方法相关的Router::scope();之前。所以很可能你正在使用默认的路由Router::scope('/', function ($routes) {,因此在这之前添加它。 - AKKAweb
非常感谢你们的帮助,问题已经解决。extensions 调用是在 config/routes.php 的最后进行的。 - WpDoe
1个回答

1
CakePdf不包括任何支持的PDF引擎,所以我尝试了wkhtmltopdf(参考链接)。
逐步过程:
1. Install wkhtmltopdf binary file in your local or server system (Ubuntu, Window, Mac) - Download link : [wkhtmltopdf][2]

2. Check the installed location of wkhtmltopdf binary file - (i am using    ubuntu so installed location is /usr/local/bin)

3. configure the wkhtmltopdf with cakephp:
    - in : config/bootstrap.php like below:

    Plugin::load('CakePdf', ['bootstrap' => true, 'routes' => true]);

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

    - Create a folder name "pdf" under your working template view folder:
        * for ex: src/template/(working view folder)/pdf (src/template/budget/pdf)

    - Create a file name "view.ctp" under newly created pdf folder under working directory:
        * for ex: src/template/(working view folder)/pdf/view.ctp (src/template/budget/pdf/view.ctp)

    - use below code in working controller - action(view - method)

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

        * for ex:

        public function view($id = null)
        {
            $budget = $this->budget->get($id);
            $this->viewBuilder()->options([
                'pdfConfig' => [
                    'orientation' => 'portrait',
                    'filename' => 'Invoice_' . $id
                ]
            ]);
            $this->set('budget', $budget);
        }

        - Hit the controller and action to download as PDF.
            * for ex: http://localhost:8765/projects/view/1.pdf

        - if you are facing "wkhtmltopdf binary is not found or not executable" error. copy your wkhtmltopdf file from "/usr/local/bin" to "/usr/bin"
            * cd /usr/local/bin
            * sudo cp wkhtmltoimage /usr/bin/wkhtmltoimage
            * sudo cp wkhtmltopdf /usr/bin/wkhtmltopdf

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接