Dompdf plugin for CakePHP
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., (*1)
The recommended way to install composer packages is:, (*2)
composer require daoandco/cakephp-dompdf
After installation, generate symlink for CSS (http://book.cakephp.org/3.0/en/deployment.html#symlink-assets), (*3)
// In a shell
bin/cake plugin assets symlink
Quick Start
Loading the Plugin, (*4)
  // In config/bootstrap.php
  Plugin::load('Dompdf');
Activate pdf extension (http://book.cakephp.org/3.0/en/development/routing.html#routing-file-extensions), (*5)
  // In config/routes.php
  Router::scope('/', function ($routes) {
    $routes->extensions(['pdf']);
    ...
  }
Loading component RequestHandler, (*6)
  // In src/controller/AppController.php
  public function initialize() {
    parent::initialize();
    $this->loadComponent('RequestHandler');
  }
In a controller, (*7)
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), (*8)
start('header'); ?>
    <p>Header.</p>
end(); ?>
start('footer'); ?>
    <p>Footer.</p>
end(); ?>
My title
Banana, (*9)
Boom !!!, (*10)
Show the pdf in your browser :
http://dev.local/myproject/yop/view/test.pdf, (*11)
Configuration
Use $this->viewBuilder() with :, (*12)
- 
->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'), (*13)
 
- 
->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'), (*14)
 
- 
->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, (*15)
 
- 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
with default layout and dompdf.css, (*16)
$this->start('header');
    echo '<p>I'm a header</p>';
$this->end();
with default layout and dompdf.css, (*17)
$this->start('footer');
    echo '<p>I'm a footer</p>';
$this->end();
Image
use Helper, (*18)
/**
  * 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 :, (*19)
echo $this->Dompdf->image('test.png', ['class' => 'imgclass']);
CSS stylesheets
use Helper, (*20)
/**
  * 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 :, (*21)
echo $this->Dompdf->css('mycss');
Page break
with dompdf.css, (*22)
Page 1, (*23)
= $this->Dompdf->page_break(); ?>
Page 2, (*24)
Render
Display on browser
``` PHP
$this->viewBuilder()
    ->className('Dompdf.Pdf')
    ->layout('Dompdf.default')
    ->options(['config' => [
        'render' => 'browser',
    ]]);, (*25)
### Force download on browser
``` PHP
$this->viewBuilder()
    ->className('Dompdf.Pdf')
    ->layout('Dompdf.default')
    ->options(['config' => [
        'filename' => 'mydocument',
        'render' => 'download',
    ]]);
Upload on server
``` PHP
$this->viewBuilder()
    ->className('Dompdf.Pdf')
    ->layout('Dompdf.default')
    ->options(['config' => [
        'upload_filename' => WWW_ROOT.'pdf/mydocument.pdf',
        'render' => 'upload',
    ]]);, (*26)
### Stream
``` PHP
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, (*27)
<!-- 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, (*28)
$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, (*29)