@@ -9,8 +9,8 @@ Plugin containing CakePdf lib which will use a PDF engine to convert HTML to PDF
99
1010Engines 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
1616Community 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
3534CakePdf does not include any of the supported PDF engines, you need to install
3635the 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 .
4238DomPdf, 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
6454If 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
9283Configuration options:
9384* engine: Engine to be used (required), or an array of engine config options
@@ -114,38 +105,37 @@ Configuration options:
114105
115106Example:
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
151141The ` 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
182179You can create PDF view and layout files for your controller actions and have
183180them 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
187184Make 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
205202using ` 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
218216You 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 `
221219Note that layouts for both usage types are within same directory, but the view
222220templates use different file paths Optionally you can also write the raw data to
223221file.
@@ -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.
0 commit comments