CakePDF,CakePHP 3.x,PDF没有渲染

时间:2016-01-27 10:30:10

标签: cakephp pdf

我安装了CakePDF插件,如https://github.com/FriendsOfCake/CakePdf所述。

PDF没有呈现。

composer.json:

"require": {
        "php": ">=5.4.16",
        "cakephp/cakephp": "~3.1",
        "mobiledetect/mobiledetectlib": "2.*",
        "cakephp/migrations": "~1.0",
        "cakephp/plugin-installer": "*",
        "friendsofcake/cakepdf": "^3.1",
        "tecnickcom/tcpdf": "^6.2"
    },

使用cmd检查了更新:

Nothing to install or update.

在config / bootstrap.php中:

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

Configure::write('CakePdf', [
    'engine' => 'CakePdf.Tcpdf',
    'margin' => [
        'bottom' => 15,
        'left' => 50,
        'right' => 30,
        'top' => 45
    ],
    'download' => true,
    'encoding' => 'UTF-8'
]);

AppController:

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

        $this->loadComponent('RequestHandler');
        [...]
    }

The DeceasesController:

public function test($id = null){

        $decease = $this->Deceases->get($id, [
            'contain' => [
                ]
        ]);
        $this->set('decease', $decease);

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

模板/ Deceases / pdf / test.ctp中的视图:

<h3>Test</h3>

vendor / cakephp / cakephp / src / View / Helper / AppHelper.php中的新类:

class AppHelper extends Helper {

    public function assetUrl($path, $options = []){

        if(!empty($this->request->params['_ext']) && $this->request->params['_ext'] === 'pdf'){
            $options['fullBase'] = true;
        }

        return parent::assetUrl($path, $options);
    }
}

在config / routes.php中:

Router::mapResources(['Deceases']);

我的文件夹结构(已更新):

| Source Files
| | bin
| | config
| | logs
| | plugins
| | src
| | | Console
| | | Controller
| | | | Component
| | | | AppController.php
| | | | DeceasesController.php 
| | | | [other Controllers]
| | | Model
| | | Shell
| | | Template
| | | | Deceases
| | | | | pdf
| | | | | | view.ctp
| | | | | add.ctp
| | | | | edit.ctp
| | | | | index.ctp
| | | | | view.ctp
| | | | Layout
| | | | | pdf
| | | | | | default.ctp
| | | | | [other folders and files]
| | | | [other template folders]
[......]

正如我所知,CakePDF需要在模板文件夹中命名为'pdf'的子目录。我如何调用PDF?

当我浏览localhost/cake3/deceases/test/test.pdf时,我收到以下错误:Error: The view for DeceasesController::test() was not found.

我的谬误在哪里?我做错了什么?

非常感谢提前。

修改

当我浏览http://localhost/cake3/deceases/1.pdf时(感谢提示@ndm),我收到此错误:

    Fatal error: [Cake\View\Exception\MissingTemplateException] Template file "Error\pdf\error500.ctp" is missing. #0 
D:\Programme\Programme\Xampp\htdocs\cake3\vendor\cakephp\cakephp\src\View\View.php(584): Cake\View\View->_getViewFileName('error500') #1 
D:\Programme\Programme\Xampp\htdocs\cake3\vendor\friendsofcake\cakepdf\src\View\PdfView.php(117): Cake\View\View->render('error500', 'error') #2 
D:\Programme\Programme\Xampp\htdocs\cake3\vendor\cakephp\cakephp\src\Error\ExceptionRenderer.php(358): CakePdf\View\PdfView->render('error500', 'error') #3 
D:\Programme\Programme\Xampp\htdocs\cake3\vendor\cakephp\cakephp\src\Error\ExceptionRenderer.php(327): Cake\Error\ExceptionRenderer->_outputMessageSafe('error500') #4 
D:\Programme\Programme\Xampp\htdocs\cake3\vendor\cakephp\cakephp\src\Error\ExceptionRenderer.php(329): Cake\Error\ExceptionRenderer->_outputMessage('error500') #5 
D:\Programme\Programme\Xampp\htdocs\cake3\vendor\cakephp\cakephp\src\Error\ExceptionRenderer.php(194): Cake\Error\ExceptionRenderer->_outputMessage('fatalE in D:\Programme\Programme\Xampp\htdocs\cake3\vendor\cakephp\cakephp\src\Error\ErrorHandler.php on line 156

编辑#2:

routes.php文件:

use Cake\Core\Plugin;
use Cake\Routing\Router;

Router::defaultRouteClass('DashedRoute');

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

    $routes->connect('/', ['controller' => 'Pages', 'action' => 'display', 'home']);


    $routes->connect('/pages/*', ['controller' => 'Pages', 'action' => 'display']);


    $routes->fallbacks('DashedRoute');
});

/**
 * Load all plugin routes.  See the Plugin documentation on
 * how to customize the loading of plugin routes.
 */
Plugin::routes();
Router::mapResources(['Deceases']);

编辑#3:

现在我已创建了缺少的模板,并将新的目录'Pdf'添加到src/Template

当我浏览http://localhost/cake3/deceases/1.pdf时,我收到此错误(error.log):

2016-01-28 10:20:52 Error: [Cake\View\Exception\MissingTemplateException] Template file "Pdf\.ctp" is missing.
Exception Attributes: array (
  'file' => 'Pdf\\.ctp',
)
Request URL: /cake3/deceases/1.pdf
Referer URL: http://localhost/cake3/users/login
Stack Trace:
#0 D:\Programme\Programme\Xampp\htdocs\cake3\vendor\cakephp\cakephp\src\View\View.php(584): Cake\View\View->_getViewFileName(NULL)
#1 D:\Programme\Programme\Xampp\htdocs\cake3\vendor\friendsofcake\cakepdf\src\Pdf\CakePdf.php(818): Cake\View\View->render()
#2 D:\Programme\Programme\Xampp\htdocs\cake3\vendor\friendsofcake\cakepdf\src\Pdf\CakePdf.php(244): CakePdf\Pdf\CakePdf->_render()
#3 D:\Programme\Programme\Xampp\htdocs\cake3\vendor\friendsofcake\cakepdf\src\View\PdfView.php(131): CakePdf\Pdf\CakePdf->output('')
#4 D:\Programme\Programme\Xampp\htdocs\cake3\vendor\cakephp\cakephp\src\Controller\Controller.php(612): CakePdf\View\PdfView->render(NULL, NULL)
#5 D:\Programme\Programme\Xampp\htdocs\cake3\vendor\cakephp\cakephp\src\Routing\Dispatcher.php(120): Cake\Controller\Controller->render()
#6 D:\Programme\Programme\Xampp\htdocs\cake3\vendor\cakephp\cakephp\src\Routing\Dispatcher.php(87): Cake\Routing\Dispatcher->_invoke(Object(App\Controller\DeceasesController))
#7 D:\Programme\Programme\Xampp\htdocs\cake3\webroot\index.php(37): Cake\Routing\Dispatcher->dispatch(Object(Cake\Network\Request), Object(Cake\Network\Response))
#8 {main}

file "Pdf\.ctp"表示什么文件?

编辑#4:

修改完整个项目后,我再也没有错误了。非常感谢@ndm。

我已删除了可选路由,因为我认为无需使用它。

当我浏览到/deceases/view/1.pdf时,我会检索与/deceases/view相同的视图,而不是带有浏览器下载或打印按钮的PDF视图。

应该这样吗?当它应该是这样时,我如何获得浏览器中显示的PDF?

0 个答案:

没有答案