PHPackages                             pieceofcake2/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. [PDF &amp; Document Generation](/categories/documents)
4. /
5. pieceofcake2/cakepdf

ActiveCakephp-plugin[PDF &amp; Document Generation](/categories/documents)

pieceofcake2/cakepdf
====================

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

v1.0.4(10mo ago)01MITPHPPHP ^8.0CI passing

Since Oct 6Pushed 10mo agoCompare

[ Source](https://github.com/pieceofcake2/cakepdf)[ Packagist](https://packagist.org/packages/pieceofcake2/cakepdf)[ RSS](/packages/pieceofcake2-cakepdf/feed)WikiDiscussions 1.x Synced 1w ago

READMEChangelogDependencies (10)Versions (2)Used By (0)

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

[](#cakepdf-plugin)

[![GitHub License](https://camo.githubusercontent.com/83664b02b98a409a9035122a97654f29396369b30ce2e62d2e96c911b5dcd724/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f70696563656f6663616b65322f63616b657064663f6c6162656c3d4c6963656e7365)](LICENSE)[![Packagist Version](https://camo.githubusercontent.com/409606059fb3a31efbdda448ca1dacaccff0fc558110f170847da860261f5c22/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f70696563656f6663616b65322f63616b657064663f6c6162656c3d5061636b6167697374)](https://packagist.org/packages/pieceofcake2/cakepdf)[![PHP](https://camo.githubusercontent.com/53dd5c077d656c8df1c07f0f2b2d6aa32f7e668e7cf424a82b27e37a88ae6b0a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f646570656e64656e63792d762f70696563656f6663616b65322f63616b657064662f7068703f6c6f676f3d706870266c6f676f436f6c6f723d253233464646464646266c6162656c3d504850266c6162656c436f6c6f723d25323337373742423426636f6c6f723d253233464646464646)](https://camo.githubusercontent.com/53dd5c077d656c8df1c07f0f2b2d6aa32f7e668e7cf424a82b27e37a88ae6b0a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f646570656e64656e63792d762f70696563656f6663616b65322f63616b657064662f7068703f6c6f676f3d706870266c6f676f436f6c6f723d253233464646464646266c6162656c3d504850266c6162656c436f6c6f723d25323337373742423426636f6c6f723d253233464646464646)[![CakePHP](https://camo.githubusercontent.com/a8909430640308acbc876b95e24c86a366e8370a36a6d9a761448345ae8b946d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f646570656e64656e63792d762f70696563656f6663616b65322f63616b657064662f70696563656f6663616b65322f63616b657068703f6c6f676f3d63616b65706870266c6f676f436f6c6f723d253233464646464646266c6162656c3d43616b65504850266c6162656c436f6c6f723d25323344333343343326636f6c6f723d253233464646464646)](https://camo.githubusercontent.com/a8909430640308acbc876b95e24c86a366e8370a36a6d9a761448345ae8b946d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f646570656e64656e63792d762f70696563656f6663616b65322f63616b657064662f70696563656f6663616b65322f63616b657068703f6c6f676f3d63616b65706870266c6f676f436f6c6f723d253233464646464646266c6162656c3d43616b65504850266c6162656c436f6c6f723d25323344333343343326636f6c6f723d253233464646464646)[![CI](https://camo.githubusercontent.com/28fb6e7b588850b73a4a69b1208862f4333e70fc5a2625849453b71f9135dc3b/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f70696563656f6663616b65322f63616b657064662f43492e796d6c3f6c6162656c3d4349)](https://github.com/pieceofcake2/cakepdf/actions/workflows/CI.yml)[![Codecov](https://camo.githubusercontent.com/4c823c5549997018b19a44dcb7057d401d364106fb04a217c9cbee8ce94c63c2/68747470733a2f2f696d672e736869656c64732e696f2f636f6465636f762f632f67682f70696563656f6663616b65322f63616b657064663f6c6162656c3d436f766572616765)](https://codecov.io/gh/pieceofcake2/cakepdf)

**This is forked for CakePHP2.**

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

Current engines:

- DomPdf
- Mpdf
- Tcpdf
- WkHtmlToPdf (requires additional installation) **RECOMMENDED ENGINE**

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

[](#requirements)

- PHP 8.0+
- CakePHP 2.10+
- wkhtmltopdf (optional) See:
- pdftk (optional) See:

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

[](#installation)

In your app directory type

```
composer require pieceofcake2/cakepdf
```

Setup
-----

[](#setup)

In `app/Config/bootstrap.php` add:

```
CakePlugin::load('CakePdf', ['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)
- options: Engine options, this may vary between Engines
- 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
- binary: Path to binary (WkHtmlToPdfEngine only), defaults to /usr/bin/wkhtmltopdf
- 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.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));
    }
}
```

Usage
-----

[](#usage)

You can use CakePdf in 2 ways, read carefully which one you actually need. Many people mix both ways and dont 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 `app/View/Invoices/pdf/view.ctp`Layouts will be in `app/View/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 `app/View/Pdf/newsletter.ctp`. Layout file path would be like `app/View/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:

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

Encryption
----------

[](#encryption)

You can optionally encrypt the PDF with permissions

To use encryption you first need to select a crypto engine. Currently we support the following crypto engines:

- Pdftk

### Usage

[](#usage-1)

Add the following in your bootstrap.

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

Options in pdfConfig:

- protect: Set to true to enable encryption
- userPassword (optional): Set a password to open the PDF file
- ownerPassword (optional): Set the password to unlock the locked permissions
- one of the above must be present, either userPassword or ownerPassword
- permissions (optional): Define the permissions

Permissions:

By default, we deny all permissions.

To allow all permissions:

Set 'permission' to true

To allow specific permissions:

Set 'permissions' to an array with a combination of the following available permissions:

- print
- degraded\_print
- modify,
- assembly,
- copy\_contents,
- screen\_readers,
- annotate,
- fill\_in

Note about static assets
------------------------

[](#note-about-static-assets)

Use absolute URLs for static assets in your view templates for PDFs. If you use `HtmlHelper::image()`, `HtmlHelper::script()` or `HtmlHelper::css()` make sure you have `$options['fullBase'] = true`

Another solution would be to create a `AppHelper` of which it would force `$options['fullBase'] = true` for PDF requests. e.g:

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

Thanks
------

[](#thanks)

Many thanks to Kim Biesbjerg and Jelle Henkens for their contributions. Want your name here as well? Create a pull request for improvements/other PDF engines.

###  Health Score

28

—

LowBetter than 51% of packages

Maintenance54

Moderate activity, may be stable

Popularity1

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 81.6% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~0 days

Total

2

Last Release

315d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/578548?v=4)[TSURU](/maintainers/ttsuru)[@ttsuru](https://github.com/ttsuru)

---

Top Contributors

[![ceeram](https://avatars.githubusercontent.com/u/111448?v=4)](https://github.com/ceeram "ceeram (102 commits)")[![renan](https://avatars.githubusercontent.com/u/28046?v=4)](https://github.com/renan "renan (5 commits)")[![ttsuru](https://avatars.githubusercontent.com/u/578548?v=4)](https://github.com/ttsuru "ttsuru (5 commits)")[![dakota](https://avatars.githubusercontent.com/u/83255?v=4)](https://github.com/dakota "dakota (4 commits)")[![tenebrousedge](https://avatars.githubusercontent.com/u/616105?v=4)](https://github.com/tenebrousedge "tenebrousedge (2 commits)")[![lorenzo](https://avatars.githubusercontent.com/u/37621?v=4)](https://github.com/lorenzo "lorenzo (2 commits)")[![vtek21](https://avatars.githubusercontent.com/u/36813?v=4)](https://github.com/vtek21 "vtek21 (1 commits)")[![bostjanpisler](https://avatars.githubusercontent.com/u/1169409?v=4)](https://github.com/bostjanpisler "bostjanpisler (1 commits)")[![ravage84](https://avatars.githubusercontent.com/u/625761?v=4)](https://github.com/ravage84 "ravage84 (1 commits)")[![uzyn](https://avatars.githubusercontent.com/u/280505?v=4)](https://github.com/uzyn "uzyn (1 commits)")[![bar](https://avatars.githubusercontent.com/u/88155?v=4)](https://github.com/bar "bar (1 commits)")

---

Tags

pdfcakephpcakephp2

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/pieceofcake2-cakepdf/health.svg)

```
[![Health](https://phpackages.com/badges/pieceofcake2-cakepdf/health.svg)](https://phpackages.com/packages/pieceofcake2-cakepdf)
```

###  Alternatives

[helsingborg-stad/municipio

A bootstrap theme for creating municipality sites.

4028.6k10](/packages/helsingborg-stad-municipio)[pressbooks/pressbooks-book

This theme is named after Canadian media theorist Marshall McLuhan, who coined the phrase “the medium is the message.” It is designed for academic writing and is also suitable for fiction. Headings are set in Cormorant Garamond, and body type is set in Lora.

226.7k](/packages/pressbooks-pressbooks-book)[renatio/dynamicpdf-plugin

October HTML to PDF converter using dompdf library.

3013.6k3](/packages/renatio-dynamicpdf-plugin)[luketowers/oc-snappypdf-plugin

SnappyPDF integration for OctoberCMS

121.1k](/packages/luketowers-oc-snappypdf-plugin)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
