PHPackages                             iamthom/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. iamthom/cakepdf

ActiveCakephp-plugin

iamthom/cakepdf
===============

CakePHP plugin for creating and/or rendering Pdfs, several Pdf engines supported.

3.2.0(9y ago)045MITPHPPHP &gt;=5.4.16

Since Feb 8Pushed 9y ago1 watchersCompare

[ Source](https://github.com/iamthom/CakePdf)[ Packagist](https://packagist.org/packages/iamthom/cakepdf)[ Docs](http://github.com/iamthom/CakePdf)[ RSS](/packages/iamthom-cakepdf/feed)WikiDiscussions master Synced 2mo ago

READMEChangelogDependencies (3)Versions (9)Used By (0)

Disclaimer
==========

[](#disclaimer)

I costumized this plugin for my own need. Please refer to the original source for better support

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

[](#cakepdf-plugin)

[![Build Status](https://camo.githubusercontent.com/9acbffff99c129e2c9b649db10c13844ac62fb978c3911200f01951621f4d665/68747470733a2f2f7472617669732d63692e6f72672f467269656e64734f6643616b652f43616b655064662e7376673f6272616e63683d332e30)](https://travis-ci.org/FriendsOfCake/CakePdf)[![License](https://camo.githubusercontent.com/4a40ca3020a4781f977556b95bd801afbd99b0d14fe1ae982c0f977ea3d48dce/68747470733a2f2f706f7365722e707567782e6f72672f467269656e64734f6643616b652f43616b655064662f6c6963656e73652e706e67)](https://packagist.org/packages/FriendsOfCake/CakePdf)

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

Current engines:

- DomPdf
- Mpdf
- Tcpdf
- WkHtmlToPdf **RECOMMENDED ENGINE**

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

[](#requirements)

- PHP 5.4.16+
- CakePHP 3.0+
- One of the following render engines: DomPdf, Mpdf, Tcpdf or wkhtmltopdf
- pdftk (optional) See:

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

[](#installation)

You can install this plugin into your CakePHP application using [composer](http://getcomposer.org). For existing applications you can add the following to your `composer.json` file:

```
"require": {
    "friendsofcake/cakepdf": "^3.1"
}
```

And run `php composer.phar update`, or `composer update` (Depending on your composer setup)

CakePdf does not include any of the supported PDF engines, you need to install them yourself. The recommend wkhtmltopdf engine can be downloaded from , by default CakePdf expects the wkhtmltopdf binary to be located in /usr/bin/wkhtmltopdf.

DomPdf, Mpdf and Tcpdf can be installed via composer using on of the following commands:

```
composer require dompdf/dompdf
composer require tecnick.com/tcpdf
composer require mpdf/mpdf

```

Setup
-----

[](#setup)

In `config/bootstrap.php` add:

```
Plugin::load('CakePdf', array('bootstrap' => true, 'routes' => true));
```

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

[](#configuration)

Use `Configure::write('CakePdf', $config);` or set Controller property `$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)
    - options: Options to pass on to wkhtmltopdf
- 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 potrait
- margin: Array or margins with the keys: bottom, left, right, top and their values
- title: Title of the document
- 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:

```

```

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',
            // Mac OS X / Linux is usually like:
            'binary' => '/usr/local/bin/wkhtmltopdf',
            // On Windows environmnent you NEED to use the path like
            // old fashioned MS-DOS Paths, otherwise you will keep getting:
            // WKHTMLTOPDF didn't return any data
            // 'binary' => 'C:\\Progra~1\\wkhtmltopdf\\bin\\wkhtmltopdf.exe',
	        'options' => [
	            'print-media-type' => false,
	            'outline' => true,
	            'dpi' => 96
	        ],
        ],
    ]);
```

Usage
-----

[](#usage)

You can use CakePdf in 2 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 `src/Template/Invoices/pdf/view.ctp`Layouts will be in `src/Template/Layouts/pdf/default.ctp`

Make sure your InvoicesController has RequestHandler Component in the `$components` array. Browse to

Additionally you can map resources by adding `Router::mapResources(array('Invoices'));` to your routes file and you can access the same document at

### 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 `src/Template/Pdf/newsletter.ctp`. Layout file path would be like `src/Template/Layouts/pdf/default.ctp`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:

```
