mbamarante/cakephp-dompdf
Composer 安装命令:
composer require mbamarante/cakephp-dompdf
包简介
Dompdf plugin for CakePHP
README 文档
README
Requirements
- PHP version 7.2 or higher
- CakePhp 4.0 or higher
- Dompdf 0.8.6
Installation
You can install this plugin into your CakePHP application using composer.
The recommended way to install composer packages is:
composer require mbamarante/cakephp-dompdf
After installation, generate symlink for CSS (http://book.cakephp.org/3.0/en/deployment.html#symlink-assets)
// In a shell
bin/cake plugin assets symlink
Quick Start
Loading the Plugin
// In config/bootstrap.php Plugin::load('Dompdf');
Activate pdf extension (http://book.cakephp.org/3.0/en/development/routing.html#routing-file-extensions)
// In config/routes.php Router::scope('/', function ($routes) { $routes->extensions(['pdf']); ... }
Loading component RequestHandler
// In src/controller/AppController.php public function initialize() { parent::initialize(); $this->loadComponent('RequestHandler'); }
In a controller
class YopController extends AppController { public function view($filename) { $this->viewBuilder() ->className('Dompdf.Pdf') ->layout('Dompdf.default') ->options(['config' => [ 'filename' => $filename, 'render' => 'browser', ]]); } }
Create a view (pdf content)
<!-- 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>
Show the pdf in your browser : http://dev.local/myproject/yop/view/test.pdf
Configuration
Use $this->viewBuilder() with :
-
->className() : set the view classname http://api.cakephp.org/3.1/class-Cake.View.ViewBuilder.html#_className Use the plugin view by default
className('Dompdf.Pdf') -
->layout() : set the name of the layout file to render the view http://api.cakephp.org/3.1/class-Cake.View.ViewBuilder.html#_layout Use the plugin layout by default
layout('Dompdf.default') -
->options() : Set additional options for the view http://api.cakephp.org/3.1/class-Cake.View.ViewBuilder.html#_options Use array with key
configand valuearraywith dompdf config- filename : pdf name
- upload_filename : path with filename for upload render
- render : (see render )
- browser : show in browser
- download : download the pdf by browser
- upload : save file on the server
- stream : return a stream resource for sending file without save
- size : paper size : default
A4 - orientation : paper orientation (
portaitORlandscape) : defaultportrait - dpi : Image DPI setting : default
192 - isRemoteEnabled : Enable remote file access : default
true - paginate: activate pagination (array) : default
false(see paginate ) - More options : see dompdf documention https://github.com/dompdf/dompdf/wiki
View
Header
with default layout and dompdf.css
$this->start('header'); echo '<p>I'm a header</p>'; $this->end();
Footer
with default layout and dompdf.css
$this->start('footer'); echo '<p>I'm a footer</p>'; $this->end();
Image
use 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) { ... }
Exemple :
echo $this->Dompdf->image('test.png', ['class' => 'imgclass']);
CSS stylesheets
use 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) { ... }
Exemple :
echo $this->Dompdf->css('mycss');
Page break
with dompdf.css
<p>Page 1</p> <?= $this->Dompdf->page_break(); ?> <p>Page 2</p>
Render
Display on browser
$this->viewBuilder() ->className('Dompdf.Pdf') ->layout('Dompdf.default') ->options(['config' => [ 'render' => 'browser', ]]);
Force download on browser
$this->viewBuilder() ->className('Dompdf.Pdf') ->layout('Dompdf.default') ->options(['config' => [ 'filename' => 'mydocument', 'render' => 'download', ]]);
Upload on server
$this->viewBuilder() ->className('Dompdf.Pdf') ->layout('Dompdf.default') ->options(['config' => [ 'upload_filename' => WWW_ROOT.'pdf/mydocument.pdf', 'render' => 'upload', ]]);
Stream
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();
Paginate
With helper
You can show page number but not number of pages
<!-- In a view --> <?php $this->start('footer'); ?> <p><?= $this->Dompdf->page_number(); ?></p> <?php $this->end(); ?>
With PdfView
You can show page number and number of pages Use paginate key in view config
$this->viewBuilder() ->className('Dompdf.Pdf') ->layout('Dompdf.default') ->options(['config' => [ 'filename' => $filename, 'render' => 'browser', 'paginate' => [ 'x' => 550, 'y' => 5, ], ]]);
Paginate options :
- x : left position : default
0 - y : top position : default
0 - font : font family : default
null - size : font size : default
12 - text : default
"{PAGE_NUM} / {PAGE_COUNT}" - color : rgb (array) : default
[0,0,0]= black
mbamarante/cakephp-dompdf 适用场景与选型建议
mbamarante/cakephp-dompdf 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 944 次下载、GitHub Stars 达 0, 最近一次更新时间为 2020 年 11 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「css」 「pdf」 「cakephp」 「html」 「dompdf」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 mbamarante/cakephp-dompdf 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mbamarante/cakephp-dompdf 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 mbamarante/cakephp-dompdf 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
CakePHP 4.x AdminLTE Theme.
A Twig extension to insert css as inline styles with a tag
Caching and compression for Twig assets (JavaScript and CSS).
Email Toolkit Plugin for CakePHP
Rhino, a CMS and Application-Framework plugin for CakePHP
A CakePHP plugin for sending mail via SparkPost's REST API
统计信息
- 总下载量: 944
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 5
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-11-24