如何使CakePdf在CakePHP 3.x中工作? [英] How to get CakePdf to work in CakePHP 3.x?

查看:58
本文介绍了如何使CakePdf在CakePHP 3.x中工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have installed CakePdf plugin in app/plugins folder and followed all the documentation possbile, thus my settings are as following:

// 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布局以及PDF文件夹但是,如果我进入 app / users / agendapdf.pdf ,则会收到以下消息:

I have PDF layouts done, as well as a PDF folder inside the templates folder for the model's actions, however, if I go to app/users/agendapdf.pdf, I am given the following messages:


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

The action agendapdf.pdf is not defined in UsersController Error: Create UsersController::agendapdf.pdf() in file: src/Controller/UsersController.php.

我真的很想知道可能出了什么问题以及如何解决该问题。

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

推荐答案

CakePdf不包含任何受支持的PDF引擎,因此我尝试了wkhtmltopdf(引用链接)。

CakePdf does not include any of the supported PDF engines, so i tried wkhtmltopdf( Refered Link).

分步过程:

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

这篇关于如何使CakePdf在CakePHP 3.x中工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆