PHPackages                             androideo/cakepdf - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. androideo/cakepdf

ActiveCakephp-plugin

androideo/cakepdf
=================

CakePHP plugin for creating and/or rendering Pdfs with Mpdf

5.0.2(5y ago)06MITPHP

Since Feb 8Pushed 5y agoCompare

[ Source](https://github.com/androideo/CakePdf)[ Packagist](https://packagist.org/packages/androideo/cakepdf)[ Docs](http://github.com/friendsofcake/CakePdf)[ RSS](/packages/androideo-cakepdf/feed)WikiDiscussions master Synced 4w ago

READMEChangelog (3)Dependencies (6)Versions (32)Used By (0)

CakePdf plugin
==============

[](#cakepdf-plugin)

This project is a fork of  made especially for mpdf

[![Build Status](https://camo.githubusercontent.com/22f516975616e8934d484980b5db0131f13be5911e20be60048e47d1da66f3cd/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f467269656e64734f6643616b652f43616b655064662f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/FriendsOfCake/CakePdf)[![Total Downloads](https://camo.githubusercontent.com/2d7ca27fed34d276acf825c8cece0d35db6c86b74667af45c05c7b2c099c14dc/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f667269656e64736f6663616b652f43616b655064662e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/friendsofcake/CakePdf)[![License](https://camo.githubusercontent.com/942e017bf0672002dd32a857c95d66f28c5900ab541838c6c664442516309c8a/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d626c75652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/friendsofcake/CakePdf)

Plugin containing CakePdf lib which will use a PDF engine to convert HTML to PDF.

Engines included in the plugin:

- Mpdf (^8.0.4)

Community maintained engines:

- [PDFreactor](https://github.com/jmischer/cake-pdfreactor)

Requirements
------------

[](#requirements)

- One of the following render engines: Mpdf
- pdftk (optional) See:

Installation
------------

[](#installation)

Using [Composer](http://getcomposer.org):

```
composer require friendsofcake/cakepdf

```

CakePdf does not include any of the supported PDF engines, you need to install the ones you intend to use yourself.

Packages for the recommend wkhtmltopdf engine can be downloaded from . DomPdf, Mpdf and Tcpdf can be installed via composer using one of the following commands:

```
composer require dompdf/dompdf
composer require tecnickcom/tcpdf
composer require mpdf/mpdf

```

Setup
-----

[](#setup)

Loading the plugin using CakePHP's console:

```
./bin/cake plugin load CakePdf

```

If you plan to use [the PDF view functionality](#1-render-as-pdf-including-forced-download-in-the-browser-with-pdfview)that automatically renders and returns the PDF for sending it to the browser, you should also register the `pdf` extension in your `config/routes.php` file, either globally before the routes that should be affected:

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

or for a specific route scope:

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

Further setup information can be found in the usage section.

Configuration
-------------

[](#configuration)

Use `Configure::write('CakePdf', $config);` or in controller use view builder to set view option named `pdfConfig` (only when used with PdfView). You need to define at least `$config['engine']`. When using CakePdf directly you can also pass the config array to constructor. The value for engine should have the `Plugin.ClassName` format without the Engine suffix.

Configuration options:

- engine: Engine to be used (required), or an array of engine config options
    - className: Engine class to use
    - binary: Binary file to use (Only for wkhtmltopdf)
    - cwd: current working directory (Only for wkhtmltopdf)
    - options: Engine specific options. Currently used for following engine:
        - `MpdfEngine`: The options are passed to constructor of `Mpdf` class
- crypto: Crypto engine to be used, or an array of crypto config options
    - className: Crypto class to use
    - binary: Binary file to use
- pageSize: Change the default size, defaults to A4
- orientation: Change the default orientation, defaults to portrait
- margin: Array or margins with the keys: bottom, left, right, top and their values
- title: Title of the document
- delay: A delay in milliseconds to wait before rendering the pdf
- windowStatus: The required window status before rendering the pdf
- encoding: Change the encoding, defaults to UTF-8
- download: Set to true to force a download, only when using PdfView
- filename: Filename for the document when using forced download

Example:

```
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' => '',
                'footer' => '

                 {DATE j-m-Y}
                 {PAGENO}/{nbpg}
             My document

         '
]);
```

```
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);
    }
}
```

The `engine` and `crypto` config options can also be arrays with configuration options for the relevant class. For example:

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

Usage
-----

[](#usage)

You can use CakePdf in two ways, read carefully which one you actually need. Many people mix both ways and don't get the expected results.

### 1: Render as PDF (including forced download) in the browser with PdfView

[](#1-render-as-pdf-including-forced-download-in-the-browser-with-pdfview)

You can create PDF view and layout files for your controller actions and have them automatically rendered. Place the view templates in a 'pdf' subdir, for instance `templates/Invoices/pdf/view.php`, layouts will be in `templates/layout/pdf/default.php`.

Make sure your `InvoicesController` class [loads the `RequestHandler` component](http://book.cakephp.org/3.0/en/controllers/components/request-handling.html)and browse to `http://localhost/invoices/view/1.pdf`

Additionally you can map resources by adding `Router::mapResources(['Invoices']);`to your routes file and you can access the same document at `http://localhost/invoices/1.pdf`.

In case you don't want to use the `pdf` extension in your URLs, you can omit registering it in your routes configuration. Then in your controller action specify the view class to be used:

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

Instead of having the pdf rendered in browser itself you can force it to be downloaded by using `download` option. Additionally you can specify custom filename using `filename` options.

```
$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.
    ]
);
```

### 2: Create PDF for email attachment, file storage etc.

[](#2-create-pdf-for-email-attachment-file-storage-etc)

You can use CakePdf lib to create raw PDF data with a view template. The view file path would look like `templates/pdf/newsletter.php`. Layout file path would be like `templates/layout/pdf/default.php`Note that layouts for both usage types are within same directory, but the view templates use different file paths Optionally you can also write the raw data to file.

Example:

```
