PHP code example of daoandco / cakephp-dompdf

1. Go to this page and download the library: Download daoandco/cakephp-dompdf library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

daoandco / cakephp-dompdf example snippets


  // In config/bootstrap.php
  Plugin::load('Dompdf');

  // In config/routes.php
  Router::scope('/', function ($routes) {

    $routes->extensions(['pdf']);
    ...
  }

  // 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',
            ]]);
    }
}

$this->start('header');
    echo '<p>I'm a header</p>';
$this->end();

$this->start('footer');
    echo '<p>I'm a footer</p>';
$this->end();

/**
  * 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']);

/**
  * 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');

$this->viewBuilder()
    ->className('Dompdf.Pdf')
    ->layout('Dompdf.default')
    ->options(['config' => [
        'filename' => $filename,
        'render' => 'browser',
        'paginate' => [
            'x' => 550,
            'y' => 5,
        ],
    ]]);
HTML
<!-- src/Template/Yop/pdf/view.ctp -->
 $this->start('header'); 
HTML
<p>Page 1</p>

<?= $this->Dompdf->page_break(); 
 PHP
$this->viewBuilder()
    ->className('Dompdf.Pdf')
    ->layout('Dompdf.default')
    ->options(['config' => [
        'render' => 'browser',
    ]]);
 PHP
$this->viewBuilder()
    ->className('Dompdf.Pdf')
    ->layout('Dompdf.default')
    ->options(['config' => [
        'filename' => 'mydocument',
        'render' => 'download',
    ]]);
 PHP
$this->viewBuilder()
    ->className('Dompdf.Pdf')
    ->layout('Dompdf.default')
    ->options(['config' => [
        'upload_filename' => WWW_ROOT.'pdf/mydocument.pdf',
        'render' => 'upload',
    ]]);
HTML
<!-- In a view -->
 $this->start('footer');