daoandco / cakephp-dompdf
Dompdf plugin for CakePHP
Installs: 13 264
Dependents: 0
Suggesters: 0
Security: 0
Stars: 4
Watchers: 2
Forks: 6
Open Issues: 4
Type:cakephp-plugin
Requires
- php: >=5.4.16
- cakephp/cakephp: ~3.0
- dompdf/dompdf: 0.8.*
Requires (Dev)
This package is not auto-updated.
Last update: 2024-10-26 19:05:22 UTC
README
Requirements
- PHP version 5.4.16 or higher
- CakePhp 3.0 or higher
- Dompdf 0.7
Installation
You can install this plugin into your CakePHP application using composer.
The recommended way to install composer packages is:
composer require daoandco/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
config
and valuearray
with 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 (
portait
ORlandscape
) : 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