2017 © Pedro Peláez
 

cakephp-plugin cakepdf

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

image

danny3b/cakepdf

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

  • Tuesday, May 8, 2018
  • by danny3b
  • Repository
  • 1 Watchers
  • 0 Stars
  • 4 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 159 Forks
  • 0 Open issues
  • 25 Versions
  • 0 % Grown

The README.md

CakePdf plugin

Build Status Total Downloads License, (*1)

Plugin containing CakePdf lib which will use a PDF engine to convert HTML to PDF., (*2)

Current engines: * DomPdf (^0.8) * Mpdf (^7.0) * Tcpdf (^6.2) * WkHtmlToPdf RECOMMENDED ENGINE, (*3)

Requirements

  • CakePHP 3.4+
  • One of the following render engines: DomPdf, Mpdf, Tcpdf or wkhtmltopdf
  • pdftk (optional) See: http://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/

Installation

Using Composer:, (*4)

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. The recommend wkhtmltopdf engine can be downloaded from http://wkhtmltopdf.org/, by default CakePdf expects the wkhtmltopdf binary to be located in /usr/bin/wkhtmltopdf. If you are using wkhtmltopdf in Windows, remove any spaces in the path name. For example use C:/Progra~1/wkhtmltopdf/bin/wkhtmltopdf.exe, (*5)

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

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

Setup

In config/bootstrap.php add:, (*7)

Plugin::load('CakePdf', ['bootstrap' => true]);

or using CakePHP's console:, (*8)

./bin/cake plugin load CakePdf -b

If you plan to use the PDF view functionality 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:, (*9)

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

or for a specific route scope:, (*10)

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

Further setup information can be found in the usage section., (*11)

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., (*12)

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: * WkHtmlToPdfEngine: The options are passed as CLI arguments * TexToPdfEngine: The options are passed as CLI arguments * DomPdfEngine: The options are passed to constructor of Dompdf class * 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 potrait * 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, (*13)

Example:, (*14)

 'CakePdf.WkHtmlToPdf',
        'margin' => [
            'bottom' => 15,
            'left' => 50,
            'right' => 30,
            'top' => 45
        ],
        'orientation' => 'landscape',
        'download' => true
    ]);
?>

Invoice->get($id);
            $this->viewBuilder()->options([
                '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:, (*15)

    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',
            // 'cwd' => 'C:\\Progra~1\\wkhtmltopdf\\bin',
            'options' => [
                'print-media-type' => false,
                'outline' => true,
                'dpi' => 96
            ],
        ],
    ]);

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., (*16)

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/Layout/pdf/default.ctp., (*17)

Make sure your InvoicesController class loads the RequestHandler component and browse to http://localhost/invoices/view/1.pdf, (*18)

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., (*19)

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 call RequestHandler::render() to switch the view class and template paths., (*20)

$this->RequestHandler->renderAs($this, 'pdf', ['attachment' => 'filename.pdf']);

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/Layout/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., (*21)

Example:, (*22)

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

Encryption

You can optionally encrypt the PDF with permissions, (*23)

To use encryption you first need to select a crypto engine. Currently we support the following crypto engines: * Pdftk, (*24)

Usage

Add the following in your bootstrap., (*25)

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, (*26)

Permissions:, (*27)

By default, we deny all permissions., (*28)

To allow all permissions:, (*29)

Set 'permission' to true, (*30)

To allow specific permissions:, (*31)

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, (*32)

How to

Ensure css, images etc. are loaded in PDF

Use absolute URLs for static assets in your view templates for PDFs. If you use HtmlHelper::image(), or HtmlHelper::css() make sure you have set fullBase option to true., (*33)

For example, (*34)

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

Here are a couple of CSS based solutions you can refer to for easily getting header footer on all PDF pages., (*35)

  • https://ourcodeworld.com/articles/read/687/how-to-configure-a-header-and-footer-in-dompdf
  • http://www.jessicaschillinger.us/2017/blog/print-repeating-header-browser/

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., (*36)

The Versions

08/05 2018

dev-master

9999999-dev http://github.com/friendsofcake/CakePdf

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

  Sources   Download

MIT

The Requires

 

The Development Requires

30/04 2018

3.5.1

3.5.1.0 http://github.com/friendsofcake/CakePdf

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

  Sources   Download

MIT

The Requires

 

The Development Requires

17/04 2018

3.5.0

3.5.0.0 http://github.com/friendsofcake/CakePdf

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

  Sources   Download

MIT

The Requires

 

The Development Requires

15/03 2018

3.4.0

3.4.0.0 http://github.com/friendsofcake/CakePdf

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

  Sources   Download

MIT

The Requires

 

The Development Requires

15/03 2018

3.3.4

3.3.4.0 http://github.com/friendsofcake/CakePdf

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

  Sources   Download

MIT

The Requires

 

The Development Requires

09/03 2018

3.3.3

3.3.3.0 http://github.com/friendsofcake/CakePdf

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

  Sources   Download

MIT

The Requires

 

The Development Requires

09/03 2018

3.3.2

3.3.2.0 http://github.com/friendsofcake/CakePdf

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

  Sources   Download

MIT

The Requires

 

The Development Requires

22/12 2017

3.3.1

3.3.1.0 http://github.com/friendsofcake/CakePdf

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

  Sources   Download

MIT

The Requires

 

The Development Requires

21/12 2017

dev-mpdf-orientation

dev-mpdf-orientation http://github.com/friendsofcake/CakePdf

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

  Sources   Download

MIT

The Requires

 

The Development Requires

21/12 2017

3.3.0

3.3.0.0 http://github.com/friendsofcake/CakePdf

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

  Sources   Download

MIT

The Requires

 

The Development Requires

23/02 2017

3.2.3

3.2.3.0 http://github.com/friendsofcake/CakePdf

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

  Sources   Download

MIT

The Requires

 

The Development Requires

20/02 2017

dev-wkhtmltopdf-test

dev-wkhtmltopdf-test http://github.com/friendsofcake/CakePdf

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

  Sources   Download

MIT

The Requires

 

The Development Requires

10/02 2017

3.2.2

3.2.2.0 http://github.com/friendsofcake/CakePdf

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

  Sources   Download

MIT

The Requires

 

The Development Requires

18/10 2016

dev-master-app-view

dev-master-app-view http://github.com/friendsofcake/CakePdf

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

  Sources   Download

MIT

The Requires

 

The Development Requires

22/08 2016

3.2.1

3.2.1.0 http://github.com/friendsofcake/CakePdf

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

  Sources   Download

MIT

The Requires

 

The Development Requires

14/04 2016

3.2.0

3.2.0.0 http://github.com/friendsofcake/CakePdf

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

  Sources   Download

MIT

The Requires

 

The Development Requires

12/04 2016

3.1.2

3.1.2.0 http://github.com/friendsofcake/CakePdf

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

  Sources   Download

MIT

The Requires

 

The Development Requires

20/03 2016

3.1.1

3.1.1.0 http://github.com/friendsofcake/CakePdf

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

  Sources   Download

MIT

The Requires

 

The Development Requires

02/11 2015

3.1.0

3.1.0.0 http://github.com/friendsofcake/CakePdf

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

  Sources   Download

MIT

The Requires

 

The Development Requires

08/06 2015

3.0.0

3.0.0.0 http://github.com/friendsofcake/CakePdf

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

  Sources   Download

MIT

The Requires

 

The Development Requires

31/05 2015

1.0.x-dev

1.0.9999999.9999999-dev https://github.com/FriendsOfCake/CakePdf

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

  Sources   Download

MIT

The Requires

 

31/05 2015

1.0.3

1.0.3.0 https://github.com/FriendsOfCake/CakePdf

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

  Sources   Download

MIT

The Requires

 

19/09 2014

1.0.2

1.0.2.0 http://github.com/ceeram/CakePdf

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

  Sources   Download

MIT

The Requires

 

06/09 2013

1.0.1

1.0.1.0 http://github.com/ceeram/CakePdf

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

  Sources   Download

MIT

The Requires

 

08/02 2013

1.0

1.0.0.0 http://github.com/ceeram/CakePdf

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

  Sources   Download

MIT

The Requires