Skip to content

Commit

Permalink
Merge pull request #301 from FriendsOfCake/cake-4.x
Browse files Browse the repository at this point in the history
Cake 4.x
  • Loading branch information
ADmad authored Jul 10, 2020
2 parents 9184b6f + ba8b32d commit 9581c78
Show file tree
Hide file tree
Showing 47 changed files with 530 additions and 423 deletions.
13 changes: 13 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
; This file is for unifying the coding style for different editors and IDEs.
; More information at http://editorconfig.org
root = true

[*]
indent_style = space
indent_size = 4
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true

[*.yml]
indent_size = 2
24 changes: 24 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Define the line ending behavior of the different file extensions
# Set default behavior, in case users don't have core.autocrlf set.
* text=auto eol=lf

# Explicitly declare text files we want to always be normalized and converted
# to native line endings on checkout.
*.php text
*.yml text

# Remove files for archives generated using `git archive`
.github export-ignore
tests export-ignore
.coveralls export-ignore
.editorconfig export-ignore
.gitattributes export-ignore
.gitignore export-ignore
.stickler.yml export-ignore
.travis.yml export-ignore
phpcs.xml.dist export-ignore
phpstan.neon export-ignore
phpstan-baseline.neon export-ignore
phpunit.xml.dist export-ignore
psalm.xml export-ignore
psalm-baseline.xml export-ignore
19 changes: 2 additions & 17 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,20 +1,5 @@
*.diff
*.err
*.orig
*.rej
*.swo
*.swp
*.vi
*~
.DS_Store
.cache
.project
.settings
.svn
errors.err
tags
/nbproject/
/composer.lock
/vendor
/phpunit.xml
/tmp
/tmp
.phpunit.result.cache
42 changes: 23 additions & 19 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
language: php

php:
- 5.6
- 7.0
- 7.1
- 7.2
- 7.3
- 7.4

env:
global:
Expand All @@ -14,38 +13,43 @@ matrix:
fast_finish: true

include:
- php: 7.0
- php: 7.2
env: PHPCS=1 DEFAULT=0

- php: 7.1
env: PHPSTAN=1 DEFAULT=0

- php: 5.6
env: PREFER_LOWEST=1
- php: 7.2
env: STATIC_ANALYSIS=1 DEFAULT=0

before_script:
- if [[ $TRAVIS_PHP_VERSION != 7.0 ]]; then phpenv config-rm xdebug.ini; fi
- phpenv config-rm xdebug.ini

- sudo apt-get install wkhtmltopdf -y
- sudo touch /usr/bin/latexpdf && sudo chmod +x /usr/bin/latexpdf

- if [[ $PREFER_LOWEST != 1 ]]; then composer update --no-interaction ; fi
- if [[ $PREFER_LOWEST == 1 ]]; then composer update --no-interaction --prefer-lowest --prefer-stable; fi
- composer update --no-interaction

- if [[ $PHPCS = 1 ]]; then composer require cakephp/cakephp-codesniffer:^3.0; fi
- if [[ $PHPCS = 1 ]]; then composer require --dev cakephp/cakephp-codesniffer:^4.0; fi

- if [[ $PHPSTAN = 1 ]]; then composer require --dev phpstan/phpstan:^0.9; fi
- if [[ $STATIC_ANALYSIS = 1 ]]; then composer require --dev phpstan/phpstan:^0.12 psalm/phar:^3.7; fi

script:
- if [[ $DEFAULT = 1 && $TRAVIS_PHP_VERSION != 7.0 ]]; then vendor/bin/phpunit; fi
- if [[ $DEFAULT = 1 && $TRAVIS_PHP_VERSION = 7.0 ]]; then vendor/bin/phpunit --coverage-clover=clover.xml; fi
- |
if [[ $DEFAULT = 1 && $TRAVIS_PHP_VERSION = 7.2 ]]; then
mkdir -p build/logs
vendor/bin/phpunit --coverage-clover=build/logs/clover.xml
fi
- if [[ $DEFAULT = 1 && $TRAVIS_PHP_VERSION != 7.2 ]]; then vendor/bin/phpunit; fi

- if [[ $PHPCS = 1 ]]; then vendor/bin/phpcs -p --extensions=php --standard=vendor/cakephp/cakephp-codesniffer/CakePHP ./src ./tests ./config; fi
- if [[ $PHPCS = 1 ]]; then vendor/bin/phpcs -p --extensions=php --standard=vendor/cakephp/cakephp-codesniffer/CakePHP ./src ./tests/TestCase ./config; fi

- if [[ $PHPSTAN = 1 ]]; then vendor/bin/phpstan analyze -l 3 -a tests/bootstrap.php src/; fi
- if [[ $STATIC_ANALYSIS = 1 ]]; then vendor/bin/phpstan.phar analyze && vendor/bin/psalm.phar; fi

after_success:
- if [[ $DEFAULT = 1 && $TRAVIS_PHP_VERSION = 7.0 ]]; then bash <(curl -s https://codecov.io/bash); fi
- |
if [[ $DEFAULT = 1 && $TRAVIS_PHP_VERSION = 7.2 ]]; then
wget https://github.com/php-coveralls/php-coveralls/releases/download/v2.1.0/php-coveralls.phar
chmod +x php-coveralls.phar
./php-coveralls.phar
fi
notifications:
email: false
135 changes: 64 additions & 71 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ Plugin containing CakePdf lib which will use a PDF engine to convert HTML to PDF

Engines included in the plugin:
* DomPdf (^0.8)
* Mpdf (^7.0)
* Tcpdf (^6.2)
* Mpdf (^8.0.4)
* Tcpdf (^6.3)
* WkHtmlToPdf **RECOMMENDED ENGINE**

Community maintained engines:
Expand All @@ -19,7 +19,6 @@ Community maintained engines:

## 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/

Expand All @@ -34,11 +33,8 @@ 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`

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:

```
Expand All @@ -49,16 +45,10 @@ composer require mpdf/mpdf

## Setup

In `config/bootstrap.php` add:

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

or using CakePHP's console:
Loading the plugin using CakePHP's console:

```
./bin/cake plugin load CakePdf -b
./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)
Expand All @@ -84,10 +74,11 @@ Further setup information can be found in the usage section.

## 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.
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
Expand All @@ -114,38 +105,37 @@ Configuration options:

Example:
```php
<?php
Configure::write('CakePdf', [
'engine' => 'CakePdf.WkHtmlToPdf',
'margin' => [
'bottom' => 15,
'left' => 50,
'right' => 30,
'top' => 45
],
'orientation' => 'landscape',
'download' => true
]);
?>
Configure::write('CakePdf', [
'engine' => 'CakePdf.WkHtmlToPdf',
'margin' => [
'bottom' => 15,
'left' => 50,
'right' => 30,
'top' => 45
],
'orientation' => 'landscape',
'download' => true
]);
```

<?php
class InvoicesController extends AppController
```php
class InvoicesController extends AppController
{
// In your Invoices controller you could set additional configs,
// or override the global ones:
public function view($id = null)
{
// 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()->options([
'pdfConfig' => [
'orientation' => 'portrait',
'filename' => 'Invoice_' . $id
]
]);
$this->set('invoice', $invoice);
}
$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
Expand All @@ -155,18 +145,25 @@ 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
'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',
// 'cwd' => 'C:\\Progra~1\\wkhtmltopdf\\bin',
'options' => [
'print-media-type' => false,
'outline' => true,
'dpi' => 96
],
],
]);
```
Expand All @@ -181,8 +178,8 @@ Many people mix both ways and don't get the expected results.

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`.
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)
Expand All @@ -205,19 +202,20 @@ downloaded by using `download` option. Additionally you can specify custom filen
using `filename` options.

```php
$this->viewBuilder()->options([
'pdfConfig' => [
$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 `src/Template/Pdf/newsletter.ctp`.
Layout file path would be like `src/Template/Layout/pdf/default.ctp`
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.
Expand Down Expand Up @@ -307,8 +305,3 @@ getting header footer on all PDF pages.

* 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.
13 changes: 7 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@
"homepage": "http://github.com/friendsofcake/CakePdf",
"license": "MIT",
"require": {
"cakephp/cakephp": "^3.5"
"cakephp/cakephp": "^4.0"
},
"require-dev": {
"cakephp/chronos": "^1.1",
"phpunit/phpunit": "^5.7.14|^6.0",
"phpunit/phpunit": "~8.5.0",
"dompdf/dompdf": "^0.8",
"mpdf/mpdf": "^7.0",
"tecnickcom/tcpdf": "^6.2"
"mpdf/mpdf": "^8.0.4",
"tecnickcom/tcpdf": "^6.3"
},
"suggest": {
"dompdf/dompdf": "If you wish to use the DomPdf engine",
Expand All @@ -27,7 +26,9 @@
"autoload-dev": {
"psr-4": {
"Cake\\Test\\": "vendor/cakephp/cakephp/tests",
"CakePdf\\Test\\": "tests"
"CakePdf\\Test\\": "tests",
"MyPlugin\\": "tests/test_app/plugins/MyPlugin/src",
"TestApp\\": "tests/test_app/src"
}
},
"replace": {
Expand Down
2 changes: 2 additions & 0 deletions config/bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php
declare(strict_types=1);

use Cake\Event\Event;
use Cake\Event\EventManager;

Expand Down
2 changes: 2 additions & 0 deletions config/routes.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php
declare(strict_types=1);

use Cake\Routing\Router;

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

0 comments on commit 9581c78

Please sign in to comment.