All Projects → FriendsOfCake → Cakepdf

FriendsOfCake / Cakepdf

Licence: mit
CakePHP plugin for creating and/or rendering PDFs, supporting several popular PDF engines.

Projects that are alternatives of or similar to Cakepdf

cakephp-glide
CakePHP plugin for using Glide image manipulation library
Stars: ✭ 34 (-90.56%)
Mutual labels:  cakephp, cakephp-plugin
mixerapi
A CakePHP Plugin for RESTful API Development [READ-ONLY]
Stars: ✭ 26 (-92.78%)
Mutual labels:  cakephp, cakephp-plugin
cakephp-sequence
CakePHP plugin for maintaining a contiguous sequence of records
Stars: ✭ 41 (-88.61%)
Mutual labels:  cakephp, cakephp-plugin
Crud
Production-grade rapid controller development with built in love for API and Search
Stars: ✭ 339 (-5.83%)
Mutual labels:  cakephp, cakephp-plugin
Cakephp Tools
A CakePHP Tools plugin containing lots of useful helpers, behaviors, components, shells, ...
Stars: ✭ 325 (-9.72%)
Mutual labels:  cakephp, cakephp-plugin
cakephp-translate
A CakePHP plugin to manage translations of your static content the easy way via web backend.
Stars: ✭ 18 (-95%)
Mutual labels:  cakephp, cakephp-plugin
cakephp-api-pagination
📑 CakePHP 4 plugin that injects pagination information into API responses.
Stars: ✭ 39 (-89.17%)
Mutual labels:  cakephp, cakephp-plugin
cakephp-i18n
A CakePHP plugin with I18n related tools.
Stars: ✭ 40 (-88.89%)
Mutual labels:  cakephp, cakephp-plugin
queue
A queue-interop compatible Queueing library
Stars: ✭ 25 (-93.06%)
Mutual labels:  cakephp, cakephp-plugin
cakephp-rest
CakePHP REST Plugin - Easily build REST API endpoints in your CakePHP application.
Stars: ✭ 23 (-93.61%)
Mutual labels:  cakephp, cakephp-plugin
Orderly
Default ordering for your CakePHP tables
Stars: ✭ 21 (-94.17%)
Mutual labels:  cakephp, cakephp-plugin
Enum
Enumeration list for CakePHP 3
Stars: ✭ 27 (-92.5%)
Mutual labels:  cakephp, cakephp-plugin
elastic-search
Elastic search datasource for CakePHP
Stars: ✭ 85 (-76.39%)
Mutual labels:  cakephp, cakephp-plugin
asset-mix
Provides helpers functions for CakePHP to use Laravel Mix.
Stars: ✭ 27 (-92.5%)
Mutual labels:  cakephp, cakephp-plugin
cakephp-swagger-bake
Automatically generate OpenAPI, Swagger, and Redoc documentation from your existing CakePHP code.
Stars: ✭ 48 (-86.67%)
Mutual labels:  cakephp, cakephp-plugin
cakephp-mailgun
Mailgun plugin for CakePHP 3
Stars: ✭ 23 (-93.61%)
Mutual labels:  cakephp, cakephp-plugin
cakephp-shim
CakePHP plugin to "shim" functionality up and down for major versions of the framework.
Stars: ✭ 37 (-89.72%)
Mutual labels:  cakephp, cakephp-plugin
cakephp-ajax
AJAX for CakePHP: A plugin to ease handling AJAX requests.
Stars: ✭ 55 (-84.72%)
Mutual labels:  cakephp, cakephp-plugin
crud-view
CakePHP: Automated admin backend based on your CRUD configuration
Stars: ✭ 45 (-87.5%)
Mutual labels:  cakephp, cakephp-plugin
crud-users
CakePHP: Users plugin based on the CRUD action classes
Stars: ✭ 17 (-95.28%)
Mutual labels:  cakephp, cakephp-plugin

CakePdf plugin

Build Status Total Downloads License

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

Engines included in the plugin:

  • DomPdf (^0.8)
  • Mpdf (^8.0.4)
  • Tcpdf (^6.3)
  • WkHtmlToPdf RECOMMENDED ENGINE

Community maintained engines:

Requirements

Installation

Using Composer:

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 https://wkhtmltopdf.org/downloads.html. 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

Loading the plugin using CakePHP's console:

./bin/cake plugin load CakePdf

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:

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

Further setup information can be found in the usage section.

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:
      • 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 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.WkHtmlToPdf',
    '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)
    {
        $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 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',
    ],
]);

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

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 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.

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:

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

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

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

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.

For example

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

If you are enable to get URLs for assets working properly, you can try using file system paths instead for the assets.

<img src="<?= WWW_ROOT ?>img/logo.png" />

Get header and footer on all pages

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].