daoandco / cakephp-dompdf
CakePHP 的 Dompdf 插件
Requires
- php: >=5.4.16
- cakephp/cakephp: ~3.0
- dompdf/dompdf: 0.8.*
Requires (Dev)
This package is not auto-updated.
Last update: 2024-09-14 18:26:13 UTC
README
要求
- PHP 版本 5.4.16 或更高
- CakePhp 3.0 或更高
- Dompdf 0.7
安装
您可以使用 composer 将此插件安装到您的 CakePHP 应用程序中。
安装 composer 包的推荐方式是
composer require daoandco/cakephp-dompdf
安装后,为 CSS 生成符号链接(https://book.cakephp.com.cn/3.0/en/deployment.html#symlink-assets)
// In a shell
bin/cake plugin assets symlink
快速入门
加载插件
// In config/bootstrap.php Plugin::load('Dompdf');
激活 pdf 扩展(https://book.cakephp.com.cn/3.0/en/development/routing.html#routing-file-extensions)
// In config/routes.php Router::scope('/', function ($routes) { $routes->extensions(['pdf']); ... }
加载组件 RequestHandler
// In src/controller/AppController.php public function initialize() { parent::initialize(); $this->loadComponent('RequestHandler'); }
在控制器中
class YopController extends AppController { public function view($filename) { $this->viewBuilder() ->className('Dompdf.Pdf') ->layout('Dompdf.default') ->options(['config' => [ 'filename' => $filename, 'render' => 'browser', ]]); } }
创建一个视图(pdf 内容)
<!-- src/Template/Yop/pdf/view.ctp --> <?php $this->start('header'); ?> <p>Header.</p> <?php $this->end(); ?> <?php $this->start('footer'); ?> <p>Footer.</p> <?php $this->end(); ?> <h1>My title</h1> <p>Banana</p> <p>Boom !!!</p>
在浏览器中显示 pdf: http://dev.local/myproject/yop/view/test.pdf
配置
使用 $this->viewBuilder()
与
-
->className() : 设置视图类名 https://api.cakephp.com.cn/3.1/class-Cake.View.ViewBuilder.html#_className 默认使用插件视图
className('Dompdf.Pdf')
-
->layout() : 设置渲染视图的布局文件名 https://api.cakephp.com.cn/3.1/class-Cake.View.ViewBuilder.html#_layout 默认使用插件布局
layout('Dompdf.default')
-
->options() : 设置视图的附加选项 https://api.cakephp.com.cn/3.1/class-Cake.View.ViewBuilder.html#_options 使用带有键
config
和值array
的数组,其中包含 dompdf 配置- filename : pdf 名称
- upload_filename : 上传渲染的文件路径和名称
- render : (见 render )
- browser : 在浏览器中显示
- download : 通过浏览器下载 pdf
- upload : 在服务器上保存文件
- stream : 返回一个流资源以发送文件而不保存
- size : 纸张大小:默认
A4
- orientation : 纸张方向(
portait
ORlandscape
): 默认portrait
- dpi : 图像 DPI 设置:默认
192
- isRemoteEnabled : 启用远程文件访问:默认
true
- paginate: 激活分页(数组): 默认
false
(见 paginate ) - 更多选项:见 dompdf 文档 https://github.com/dompdf/dompdf/wiki
视图
页眉
使用默认布局和 dompdf.css
$this->start('header'); echo '<p>I'm a header</p>'; $this->end();
页脚
使用默认布局和 dompdf.css
$this->start('footer'); echo '<p>I'm a footer</p>'; $this->end();
图片
使用 Helper
/** * Générate an image * @param string $path : Path to the image file, relative to the app/webroot/img/ directory * @param array $options : Array of HTML attributes * @return string <img> */ public function image($path, $options = false) { ... }
示例
echo $this->Dompdf->image('test.png', ['class' => 'imgclass']);
CSS 样式表
使用 Helper
/** * Creates a link element for CSS stylesheets * @param string $path : The name of a CSS style sheet * @param bool $plugin : (true) add a plugin css file || (false) add a file in webroot/css /// default : false * @return string <link> */ public function css($path, $plugin) { ... }
示例
echo $this->Dompdf->css('mycss');
页面分割
使用 dompdf.css
<p>Page 1</p> <?= $this->Dompdf->page_break(); ?> <p>Page 2</p>
渲染
在浏览器中显示
$this->viewBuilder() ->className('Dompdf.Pdf') ->layout('Dompdf.default') ->options(['config' => [ 'render' => 'browser', ]]);
强制在浏览器中下载
$this->viewBuilder() ->className('Dompdf.Pdf') ->layout('Dompdf.default') ->options(['config' => [ 'filename' => 'mydocument', 'render' => 'download', ]]);
在服务器上上传
$this->viewBuilder() ->className('Dompdf.Pdf') ->layout('Dompdf.default') ->options(['config' => [ 'upload_filename' => WWW_ROOT.'pdf/mydocument.pdf', 'render' => 'upload', ]]);
流
use Cake\View\ViewBuilder; $builder = new ViewBuilder(); $builder->className('Dompdf.Pdf') ->layout('Dompdf.pdf/default') ->template('Pdf/pdf/view') ->options(['config' => [ 'render' => 'stream', ]]); $view = $builder->build(); $stream = $view->render();
分页
使用 Helper
您可以显示页码,但不能显示页数
<!-- In a view --> <?php $this->start('footer'); ?> <p><?= $this->Dompdf->page_number(); ?></p> <?php $this->end(); ?>
使用 PdfView
您可以显示页码和页数。在视图配置中使用分页键
$this->viewBuilder() ->className('Dompdf.Pdf') ->layout('Dompdf.default') ->options(['config' => [ 'filename' => $filename, 'render' => 'browser', 'paginate' => [ 'x' => 550, 'y' => 5, ], ]]);
分页选项
- x : 左位置:默认
0
- y : 顶部位置:默认
0
- 字体:字体族:默认
null
- 大小:字体大小:默认
12
- 文本:默认
"{PAGE_NUM} / {PAGE_COUNT}"
- 颜色:RGB(数组):默认
[0,0,0]
= 黑色