PHP code example of androideo / cakepdf

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

    

androideo / cakepdf example snippets


Router::extensions(['pdf']);

Router::scope('/', function (\Cake\Routing\RouteBuilder $routes) {
    $routes->addExtensions(['pdf']);
    // ...
});

Configure::write('CakePdf', [
    'engine' => 'CakePdf.Mpdf',
    'margin' => [
        'bottom' => 15,
        'left' => 50,
        'right' => 30,
        'top' => 45
    ],
    'orientation' => 'landscape',
    'download' => true,
                'customFontDir' => WWW_ROOT. 'font/dinpro',
            'customFontArray' => [
                'dinproregular' => [
                    'R' => 'DINPro-Regular.ttf',
                    'I' => 'DINPro-Regular.ttf',
                ],
                'dinprolight' => [
                    'R' => 'DINPro-Light.ttf',
                    'I' => 'DINPro-Light.ttf',
                ],
                'dinpromedium' => [
                    'R' => 'DINPro-Mediumtr.ttf',
                    'I' => 'DINPro-Mediumtr.ttf',
                ],
            ],
            'headerOnFirstPage' => false,
            'header' => '<img src="'.WWW_ROOT.'img'.DS.'logo.png" style="width:120px;float:right"/>',
                'footer' => '<table width="100%">
         <tr>
                 <td width="33%">{DATE j-m-Y}</td>
                 <td width="33%" align="center">{PAGENO}/{nbpg}</td>
             <td width="33%" style="text-align: right;">My document</td>
            </tr>
         </table>'
]);

class InvoicesController extends AppController
{
    // In your Invoices controller you could set additional configs,
    // or override the global ones:
    public function view($id = null)
    {
        $invoice = $this->Invoice->get($id);
        $this->viewBuilder()->setOption(
            'pdfConfig',
            [
                'orientation' => 'portrait',
                'filename' => 'Invoice_' . $id
            ]
        );
        $this->set('invoice', $invoice);
    }
}

    Configure::write('CakePdf', [
        'engine' => [
            'className' => 'CakePdf.WkHtmlToPdf',
            'options' => [
                'print-media-type' => false,
                'outline' => true,
                'dpi' => 96
            ],

            /**
             * For Mac OS X / Linux by default the `wkhtmltopdf` binary should
             * be available through environment path or you can specify location as:
             */
            // 'binary' => '/usr/local/bin/wkhtmltopdf',

            /**
             * On Windows the engine uses the path shown below as default.
             * You NEED to use the path like old fashioned MS-DOS Paths,
             * otherwise you will get error like:
             * "WKHTMLTOPDF didn't return any data"
             */
            // 'binary' => 'C:\\Progra~1\\wkhtmltopdf\\bin\\wkhtmltopdf.exe',
        ],
    ]);

$this->viewBuilder()->setClassName('CakePdf.Pdf');

$this->viewBuilder()->setOption(
    'pdfConfig',
    [
        'download' => true, // This can be omitted if "filename" is specified.
        'filename' => 'Invoice_' . $id // This can be omitted if you want file name based on URL.
    ]
);


    $CakePdf = new \CakePdf\Pdf\CakePdf();
    $CakePdf->template('newsletter', 'default');
    $CakePdf->viewVars($this->viewVars);
    // 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');

echo $this->Html->image('logo.png', ['fullBase' => true]);
echo $this->Html->css('bootstrap.css', ['fullBase' => true]);

<img src="<?= WWW_ROOT