gpt4 book ai didi

cakephp - 如何让 CakePdf 在 CakePHP 3.x 中工作?

转载 作者:行者123 更新时间:2023-12-02 04:42:43 25 4
gpt4 key购买 nike

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

// 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,我会得到以下内容消息:

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

我真的很想知道可能出了什么问题,以及如何解决它才能正常工作。

最佳答案

CakePdf 不包含任何受支持的 PDF 引擎,因此我尝试了 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

关于cakephp - 如何让 CakePdf 在 CakePHP 3.x 中工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36133675/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com