Skip to content

Commit 9581c78

Browse files
authored
Merge pull request #301 from FriendsOfCake/cake-4.x
Cake 4.x
2 parents 9184b6f + ba8b32d commit 9581c78

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+530
-423
lines changed

.editorconfig

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
; This file is for unifying the coding style for different editors and IDEs.
2+
; More information at http://editorconfig.org
3+
root = true
4+
5+
[*]
6+
indent_style = space
7+
indent_size = 4
8+
end_of_line = lf
9+
insert_final_newline = true
10+
trim_trailing_whitespace = true
11+
12+
[*.yml]
13+
indent_size = 2

.gitattributes

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Define the line ending behavior of the different file extensions
2+
# Set default behavior, in case users don't have core.autocrlf set.
3+
* text=auto eol=lf
4+
5+
# Explicitly declare text files we want to always be normalized and converted
6+
# to native line endings on checkout.
7+
*.php text
8+
*.yml text
9+
10+
# Remove files for archives generated using `git archive`
11+
.github export-ignore
12+
tests export-ignore
13+
.coveralls export-ignore
14+
.editorconfig export-ignore
15+
.gitattributes export-ignore
16+
.gitignore export-ignore
17+
.stickler.yml export-ignore
18+
.travis.yml export-ignore
19+
phpcs.xml.dist export-ignore
20+
phpstan.neon export-ignore
21+
phpstan-baseline.neon export-ignore
22+
phpunit.xml.dist export-ignore
23+
psalm.xml export-ignore
24+
psalm-baseline.xml export-ignore

.gitignore

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,5 @@
1-
*.diff
2-
*.err
3-
*.orig
4-
*.rej
5-
*.swo
6-
*.swp
7-
*.vi
8-
*~
9-
.DS_Store
10-
.cache
11-
.project
12-
.settings
13-
.svn
14-
errors.err
15-
tags
16-
/nbproject/
171
/composer.lock
182
/vendor
193
/phpunit.xml
20-
/tmp
4+
/tmp
5+
.phpunit.result.cache

.travis.yml

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
language: php
22

33
php:
4-
- 5.6
5-
- 7.0
6-
- 7.1
74
- 7.2
5+
- 7.3
6+
- 7.4
87

98
env:
109
global:
@@ -14,38 +13,43 @@ matrix:
1413
fast_finish: true
1514

1615
include:
17-
- php: 7.0
16+
- php: 7.2
1817
env: PHPCS=1 DEFAULT=0
1918

20-
- php: 7.1
21-
env: PHPSTAN=1 DEFAULT=0
22-
23-
- php: 5.6
24-
env: PREFER_LOWEST=1
19+
- php: 7.2
20+
env: STATIC_ANALYSIS=1 DEFAULT=0
2521

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

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

32-
- if [[ $PREFER_LOWEST != 1 ]]; then composer update --no-interaction ; fi
33-
- if [[ $PREFER_LOWEST == 1 ]]; then composer update --no-interaction --prefer-lowest --prefer-stable; fi
28+
- composer update --no-interaction
3429

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

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

3934
script:
40-
- if [[ $DEFAULT = 1 && $TRAVIS_PHP_VERSION != 7.0 ]]; then vendor/bin/phpunit; fi
41-
- if [[ $DEFAULT = 1 && $TRAVIS_PHP_VERSION = 7.0 ]]; then vendor/bin/phpunit --coverage-clover=clover.xml; fi
35+
- |
36+
if [[ $DEFAULT = 1 && $TRAVIS_PHP_VERSION = 7.2 ]]; then
37+
mkdir -p build/logs
38+
vendor/bin/phpunit --coverage-clover=build/logs/clover.xml
39+
fi
40+
- if [[ $DEFAULT = 1 && $TRAVIS_PHP_VERSION != 7.2 ]]; then vendor/bin/phpunit; fi
4241

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

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

4746
after_success:
48-
- if [[ $DEFAULT = 1 && $TRAVIS_PHP_VERSION = 7.0 ]]; then bash <(curl -s https://codecov.io/bash); fi
47+
- |
48+
if [[ $DEFAULT = 1 && $TRAVIS_PHP_VERSION = 7.2 ]]; then
49+
wget https://github.com/php-coveralls/php-coveralls/releases/download/v2.1.0/php-coveralls.phar
50+
chmod +x php-coveralls.phar
51+
./php-coveralls.phar
52+
fi
4953
5054
notifications:
5155
email: false

README.md

Lines changed: 64 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ Plugin containing CakePdf lib which will use a PDF engine to convert HTML to PDF
99

1010
Engines included in the plugin:
1111
* DomPdf (^0.8)
12-
* Mpdf (^7.0)
13-
* Tcpdf (^6.2)
12+
* Mpdf (^8.0.4)
13+
* Tcpdf (^6.3)
1414
* WkHtmlToPdf **RECOMMENDED ENGINE**
1515

1616
Community maintained engines:
@@ -19,7 +19,6 @@ Community maintained engines:
1919

2020
## Requirements
2121

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

@@ -34,11 +33,8 @@ composer require friendsofcake/cakepdf
3433

3534
CakePdf does not include any of the supported PDF engines, you need to install
3635
the ones you intend to use yourself.
37-
The recommend wkhtmltopdf engine can be downloaded from http://wkhtmltopdf.org/,
38-
by default CakePdf expects the wkhtmltopdf binary to be located in /usr/bin/wkhtmltopdf.
39-
If you are using wkhtmltopdf in Windows, remove any spaces in the path name. For example
40-
use `C:/Progra~1/wkhtmltopdf/bin/wkhtmltopdf.exe`
4136

37+
Packages for the recommend wkhtmltopdf engine can be downloaded from https://wkhtmltopdf.org/downloads.html.
4238
DomPdf, Mpdf and Tcpdf can be installed via composer using one of the following commands:
4339

4440
```
@@ -49,16 +45,10 @@ composer require mpdf/mpdf
4945

5046
## Setup
5147

52-
In `config/bootstrap.php` add:
53-
54-
```php
55-
Plugin::load('CakePdf', ['bootstrap' => true]);
56-
```
57-
58-
or using CakePHP's console:
48+
Loading the plugin using CakePHP's console:
5949

6050
```
61-
./bin/cake plugin load CakePdf -b
51+
./bin/cake plugin load CakePdf
6252
```
6353

6454
If you plan to use [the PDF view functionality](#1-render-as-pdf-including-forced-download-in-the-browser-with-pdfview)
@@ -84,10 +74,11 @@ Further setup information can be found in the usage section.
8474

8575
## Configuration
8676

87-
Use `Configure::write('CakePdf', $config);` or set Controller property `$pdfConfig`
88-
(only when used with PdfView). You need to define at least `$config['engine']`.
89-
When using CakePdf directly you can also pass the config array to constructor.
90-
The value for engine should have the `Plugin.ClassName` format without the Engine suffix.
77+
Use `Configure::write('CakePdf', $config);` or in controller use view builder to
78+
set view option named `pdfConfig` (only when used with PdfView). You need to
79+
define at least `$config['engine']`. When using CakePdf directly you can also
80+
pass the config array to constructor. The value for engine should have the
81+
`Plugin.ClassName` format without the Engine suffix.
9182

9283
Configuration options:
9384
* engine: Engine to be used (required), or an array of engine config options
@@ -114,38 +105,37 @@ Configuration options:
114105

115106
Example:
116107
```php
117-
<?php
118-
Configure::write('CakePdf', [
119-
'engine' => 'CakePdf.WkHtmlToPdf',
120-
'margin' => [
121-
'bottom' => 15,
122-
'left' => 50,
123-
'right' => 30,
124-
'top' => 45
125-
],
126-
'orientation' => 'landscape',
127-
'download' => true
128-
]);
129-
?>
108+
Configure::write('CakePdf', [
109+
'engine' => 'CakePdf.WkHtmlToPdf',
110+
'margin' => [
111+
'bottom' => 15,
112+
'left' => 50,
113+
'right' => 30,
114+
'top' => 45
115+
],
116+
'orientation' => 'landscape',
117+
'download' => true
118+
]);
119+
```
130120

131-
<?php
132-
class InvoicesController extends AppController
121+
```php
122+
class InvoicesController extends AppController
123+
{
124+
// In your Invoices controller you could set additional configs,
125+
// or override the global ones:
126+
public function view($id = null)
133127
{
134-
// In your Invoices controller you could set additional configs,
135-
// or override the global ones:
136-
public function view($id = null)
137-
{
138-
$invoice = $this->Invoice->get($id);
139-
$this->viewBuilder()->options([
140-
'pdfConfig' => [
141-
'orientation' => 'portrait',
142-
'filename' => 'Invoice_' . $id
143-
]
144-
]);
145-
$this->set('invoice', $invoice);
146-
}
128+
$invoice = $this->Invoice->get($id);
129+
$this->viewBuilder()->setOption(
130+
'pdfConfig',
131+
[
132+
'orientation' => 'portrait',
133+
'filename' => 'Invoice_' . $id
134+
]
135+
);
136+
$this->set('invoice', $invoice);
147137
}
148-
?>
138+
}
149139
```
150140

151141
The `engine` and `crypto` config options can also be arrays with configuration
@@ -155,18 +145,25 @@ options for the relevant class. For example:
155145
Configure::write('CakePdf', [
156146
'engine' => [
157147
'className' => 'CakePdf.WkHtmlToPdf',
158-
// Mac OS X / Linux is usually like:
159-
'binary' => '/usr/local/bin/wkhtmltopdf',
160-
// On Windows environmnent you NEED to use the path like
161-
// old fashioned MS-DOS Paths, otherwise you will keep getting:
162-
// WKHTMLTOPDF didn't return any data
148+
'options' => [
149+
'print-media-type' => false,
150+
'outline' => true,
151+
'dpi' => 96
152+
],
153+
154+
/**
155+
* For Mac OS X / Linux by default the `wkhtmltopdf` binary should
156+
* be available through environment path or you can specify location as:
157+
*/
158+
// 'binary' => '/usr/local/bin/wkhtmltopdf',
159+
160+
/**
161+
* On Windows the engine uses the path shown below as default.
162+
* You NEED to use the path like old fashioned MS-DOS Paths,
163+
* otherwise you will get error like:
164+
* "WKHTMLTOPDF didn't return any data"
165+
*/
163166
// 'binary' => 'C:\\Progra~1\\wkhtmltopdf\\bin\\wkhtmltopdf.exe',
164-
// 'cwd' => 'C:\\Progra~1\\wkhtmltopdf\\bin',
165-
'options' => [
166-
'print-media-type' => false,
167-
'outline' => true,
168-
'dpi' => 96
169-
],
170167
],
171168
]);
172169
```
@@ -181,8 +178,8 @@ Many people mix both ways and don't get the expected results.
181178

182179
You can create PDF view and layout files for your controller actions and have
183180
them automatically rendered. Place the view templates in a 'pdf' subdir, for
184-
instance `src/Template/Invoices/pdf/view.ctp`, layouts will be in
185-
`src/Template/Layout/pdf/default.ctp`.
181+
instance `templates/Invoices/pdf/view.php`, layouts will be in
182+
`templates/layout/pdf/default.php`.
186183

187184
Make sure your `InvoicesController` class
188185
[loads the `RequestHandler` component](http://book.cakephp.org/3.0/en/controllers/components/request-handling.html)
@@ -205,19 +202,20 @@ downloaded by using `download` option. Additionally you can specify custom filen
205202
using `filename` options.
206203

207204
```php
208-
$this->viewBuilder()->options([
209-
'pdfConfig' => [
205+
$this->viewBuilder()->setOption(
206+
'pdfConfig',
207+
[
210208
'download' => true, // This can be omitted if "filename" is specified.
211209
'filename' => 'Invoice_' . $id // This can be omitted if you want file name based on URL.
212210
]
213-
]);
211+
);
214212
```
215213

216214
### 2: Create PDF for email attachment, file storage etc.
217215

218216
You can use CakePdf lib to create raw PDF data with a view template.
219-
The view file path would look like `src/Template/Pdf/newsletter.ctp`.
220-
Layout file path would be like `src/Template/Layout/pdf/default.ctp`
217+
The view file path would look like `templates/pdf/newsletter.php`.
218+
Layout file path would be like `templates/layout/pdf/default.php`
221219
Note that layouts for both usage types are within same directory, but the view
222220
templates use different file paths Optionally you can also write the raw data to
223221
file.
@@ -307,8 +305,3 @@ getting header footer on all PDF pages.
307305

308306
* https://ourcodeworld.com/articles/read/687/how-to-configure-a-header-and-footer-in-dompdf
309307
* http://www.jessicaschillinger.us/2017/blog/print-repeating-header-browser/
310-
311-
## Thanks
312-
313-
Many thanks to Kim Biesbjerg and Jelle Henkens for their contributions.
314-
Want your name here as well? Create a pull request for improvements/other PDF engines.

composer.json

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,13 @@
55
"homepage": "http://github.com/friendsofcake/CakePdf",
66
"license": "MIT",
77
"require": {
8-
"cakephp/cakephp": "^3.5"
8+
"cakephp/cakephp": "^4.0"
99
},
1010
"require-dev": {
11-
"cakephp/chronos": "^1.1",
12-
"phpunit/phpunit": "^5.7.14|^6.0",
11+
"phpunit/phpunit": "~8.5.0",
1312
"dompdf/dompdf": "^0.8",
14-
"mpdf/mpdf": "^7.0",
15-
"tecnickcom/tcpdf": "^6.2"
13+
"mpdf/mpdf": "^8.0.4",
14+
"tecnickcom/tcpdf": "^6.3"
1615
},
1716
"suggest": {
1817
"dompdf/dompdf": "If you wish to use the DomPdf engine",
@@ -27,7 +26,9 @@
2726
"autoload-dev": {
2827
"psr-4": {
2928
"Cake\\Test\\": "vendor/cakephp/cakephp/tests",
30-
"CakePdf\\Test\\": "tests"
29+
"CakePdf\\Test\\": "tests",
30+
"MyPlugin\\": "tests/test_app/plugins/MyPlugin/src",
31+
"TestApp\\": "tests/test_app/src"
3132
}
3233
},
3334
"replace": {

config/bootstrap.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
<?php
2+
declare(strict_types=1);
3+
24
use Cake\Event\Event;
35
use Cake\Event\EventManager;
46

config/routes.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
<?php
2+
declare(strict_types=1);
3+
24
use Cake\Routing\Router;
35

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

0 commit comments

Comments
 (0)