1. Go to this page and download the library: Download kongka/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/ */
kongka / cakepdf example snippets
$routes->scope('/', function (\Cake\Routing\RouteBuilder $routes) {
$routes->setExtensions(['pdf']);
// ...
});
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 usable depend on the engine used.
'options' => [
'print-media-type' => false,
'outline' => true,
'dpi' => 96,
'cover' => [
'url' => 'cover.html',
'enable-smart-shrinking' => true,
],
'toc' => true,
],
/**
* 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',
/**
* Skip the binary execution checking for many Linux servers when set to `true`.
* Default value is `false`, which mean we need to check for security reason.
* It has been used when your Linux server cannot run directly the `/usr/bin/wkhtmltopdf`
* so, you need to install another package called `xvfb` to support "X server".
*/
//'skipCheckingExecutableBinary' => false,
//'binary' => '/usr/bin/xvfb-run /usr/bin/wkhtmltopdf'
],
]);
$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(['key' => 'value']);
// Get the PDF string returned
$pdf = $CakePdf->output();
// Or write it to file directly
$pdf = $CakePdf->write(APP . 'files' . DS . 'newsletter.pdf');