PHP code example of pieceofcake2 / cakepdf

1. Go to this page and download the library: Download pieceofcake2/cakepdf 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/ */

    

pieceofcake2 / cakepdf example snippets


CakePlugin::load('CakePdf', ['bootstrap' => true, 'routes' => true]);

Configure::write('CakePdf', [
    'engine' => 'CakePdf.WkHtmlToPdf',
    'options' => [
        'print-media-type' => false,
        'outline' => true,
        'dpi' => 96,
    ],
    'margin' => [
        'bottom' => 15,
        'left' => 50,
        'right' => 30,
        'top' => 45,
    ],
    'orientation' => 'landscape',
    'download' => true
]);

class InvoicesController extends AppController {
    //in your Invoices controller you could set additional configs, or override the global ones:
    public function view($id = null) {
        $this->Invoice->id = $id;
        if (!$this->Invoice->exists()) {
            throw new NotFoundException(__('Invalid invoice'));
        }
        $this->pdfConfig = [
            'orientation' => 'portrait',
            'filename' => 'Invoice_' . $id,
        ];
        $this->set('invoice', $this->Invoice->read(null, $id));
    }
}

$cakePdf = new CakePdf();
$cakePdf->template('newsletter', 'default');
//get the pdf string returned
$pdf = $cakePdf->output();
//or write it to file directly
$pdf = $cakePdf->write(APP . 'files' . DS . 'newsletter.pdf');

Configure::write('CakePdf.crypto', 'CakePdf.Pdftk');

class AppHelper extends Helper {
    public function assetUrl($path, $options = []) {
        if (!empty($this->request->params['ext']) && $this->request->params['ext'] === 'pdf') {
            $options['fullBase'] = true;
        }
        return parent::assetUrl($path, $options);
    }
}