我发现了这个类似的帖子,但我仍然遇到麻烦: How to get CakePdf to work in CakePHP 3.x?
任何人都可以帮我弄清楚我做错了什么或我错过了什么?
// config/bootstrap.php
Plugin::load('CakePdf', ['bootstrap' => true, 'routes' => true]);
Configure::write('CakePdf', [
'engine' => 'CakePdf.WkHtmlToPdf',
'binary' => 'vendor\wkhtmltopdf\bin\wkhtmltopdf.exe',
'margin' => [
'top' => 45,
'left' => 50,
'bottom' => 15,
'right' => 30
],
'download' => true
]);
// config/routes.php
Router::extensions(['pdf']); // This is above the Router::scope()
// controller/AppController.php
public function initialize()
{
parent::initialize();
$this->loadComponent('Flash');
$this->loadComponent('RequestHandler');
... //more code
}
// action for pdf
public function view($id = null)
{
... //more code
$this->viewBuilder()->options([
'pdfConfig' => [
'title' => 'Supported Cameras',
'filename' => 'SupportedCamerasList'
]
]);
.. //more code
}
}
我使用此网址时遇到此错误(localhost / cameras / view / 1.pdf):
( ! ) Fatal error: [Cake\View\Exception\MissingTemplateException] Template file "Error\pdf\error500.ctp" is missing.
0 C:\wamp\www\camerasapp\vendor\cakephp\cakephp\src\View\View.php(584): Cake\View\View->_getViewFileName('error500')
1 C:\wamp\www\camerasapp\vendor\friendsofcake\cakepdf\src\View\PdfView.php(103): Cake\View\View->render('error500', 'error')
2 C:\wamp\www\camerasapp\vendor\cakephp\cakephp\src\Error\ExceptionRenderer.php(356): CakePdf\View\PdfView->render('error500', 'error')
3 C:\wamp\www\camerasapp\vendor\cakephp\cakephp\src\Error\ExceptionRenderer.php(325): Cake\Error\ExceptionRenderer->_outputMessageSafe('error500')
4 C:\wamp\www\camerasapp\vendor\cakephp\cakephp\src\Error\ExceptionRenderer.php(327): Cake\Error\ExceptionRenderer->_outputMessage('error500')
5 C:\wamp\www\camerasapp\vendor\cakephp\cakephp\src\Error\ExceptionRenderer.php(192): Cake\Error\ExceptionRenderer->_outputMessage('fatalError')
6 C:\wamp\www\camerasapp\vendor\cakephp\cakephp\src\Error\ErrorHandler.php(144): Cake\Error\ExceptionRenderer->render()
7 C:\w in C:\wamp\www\camerasapp\vendor\cakephp\cakephp\src\Error\ErrorHandler.php on line 156
答案 0 :(得分:1)
我今天实际上遇到了同样的问题。您在那里看到的错误是关于缺少错误模板。您应该将src/Template/Error
中的模板复制到src/Template/Error/pdf
,然后您应该会看到实际错误。
我怀疑它试图在Missing X server
向您展示错误,这就是我的意思。我尝试了几个黑客来让它工作,但最终决定尝试其他引擎。
我首先尝试了dompdf
enigine并没有回头看。也许这是你可以尝试的东西。
您应该准备好以下文件:
src/Template/Cameras/pdf/view.ctp
src/Template/Layout/pdf/default.ctp
这些将是您的pdf布局和视图的pdf版本。
根据您的设置运行composer require dompdf/dompdf
或php composer.phar require dompdf/dompdf
。
将您的bootstrap.php
配置替换为:
Plugin::load('CakePdf', ['bootstrap' => true, 'routes' => true]);
Configure::write('CakePdf', [
'engine' => 'CakePdf.dompdf',
'margin' => [
'top' => 45,
'left' => 50,
'bottom' => 15,
'right' => 30
],
'download' => true
]);
如果您现在浏览到localhost/cameras/view/1.pdf
,您应该会看到您的pdf版本。你将不得不搞乱布局并添加一些css给你视图以使样式正确。