@@ -29,6 +29,7 @@ somewhere in your codebase.
29
29
### Single page PDF
30
30
31
31
``` php
32
+ <?php
32
33
use mikehaertl\wkhtmlto\Pdf;
33
34
34
35
// You can pass a filename, a HTML string, an URL or an options array to the constructor
@@ -47,6 +48,7 @@ if (!$pdf->saveAs('/path/to/page.pdf')) {
47
48
48
49
49
50
``` php
51
+ <?php
50
52
use mikehaertl\wkhtmlto\Pdf;
51
53
52
54
$pdf = new Pdf;
@@ -85,6 +87,7 @@ $content = $pdf->toString();
85
87
### Creating an image
86
88
87
89
``` php
90
+ <?php
88
91
use mikehaertl\wkhtmlto\Image;
89
92
90
93
// You can pass a filename, a HTML string, an URL or an options array to the constructor
@@ -115,6 +118,7 @@ The `wkhtmltopdf` shell command accepts different types of options:
115
118
Please see ` wkhtmltopdf -H ` for a full explanation. All options are passed as array, for example:
116
119
117
120
``` php
121
+ <?php
118
122
$options = array(
119
123
'no-outline', // option without argument
120
124
'encoding' => 'UTF-8', // option with argument
@@ -139,6 +143,7 @@ $options = array(
139
143
Options can be passed to several methods for PDFs:
140
144
141
145
``` php
146
+ <?php
142
147
$pdf = new Pdf($globalOptions); // Set global PDF options
143
148
$pdf->setOptions($globalOptions); // Set global PDF options (alternative)
144
149
$pdf->addPage($page, $pageOptions); // Add page with options
@@ -152,6 +157,7 @@ $pdf->addToc($tocOptions); // Add TOC with options
152
157
For ` wkhtmltoimage ` there's only one set of options:
153
158
154
159
``` php
160
+ <?php
155
161
$image = new Image($options); // Set image options
156
162
$image->setOptions($options); // Set image options (alternative)
157
163
```
@@ -177,6 +183,7 @@ the `Image` class also has a `type` option:
177
183
want to pass UTF-8 encoded arguments, you may have to set the ` LANG ` environment variable.
178
184
179
185
``` php
186
+ <?php
180
187
$pdf = new Pdf(array(
181
188
'binary' => '/obscure/path/to/wkhtmltopdf',
182
189
'ignoreWarnings' => true,
@@ -201,6 +208,7 @@ If this doesn't work correctly you can also pass an instance of our `File`
201
208
helper as a last resort:
202
209
203
210
``` php
211
+ <?php
204
212
use mikehaertl\tmp\File;
205
213
$options = [
206
214
'header-html' => new File('Complex content', '.html'),
@@ -213,6 +221,7 @@ $options = [
213
221
available from ` getError() ` :
214
222
215
223
``` php
224
+ <?php
216
225
if (!$pdf->send()) {
217
226
throw new Exception('Could not create PDF: '.$pdf->getError());
218
227
}
@@ -223,13 +232,16 @@ if ($content === false) {
223
232
}
224
233
```
225
234
226
- ## Note for Windows users
235
+ ## Known Issues
236
+
237
+ ### Use under Windows
227
238
228
239
If you use double quotes (` " ` ) or percent signs (` % ` ) as option values, they may get converted to spaces.
229
240
In this case you can disable argument escaping in the [ command] ( https://github.com/mikehaertl/php-shellcommand ) .
230
- There are also two interesting options to ` proc_open() ` that you may want to use:
241
+ There are also two interesting options to ` proc_open() ` that you may want to use on Windows :
231
242
232
243
``` php
244
+ <?php
233
245
$pdf = new Pdf(array(
234
246
'commandOptions' => array(
235
247
'escapeArgs' => false,
@@ -250,6 +262,37 @@ surround your argument values with extra double quotes.
250
262
I also found that some options don't work on Windows (tested with wkhtmltopdf 0.11 rc2), like the
251
263
` user-style-sheet ` option used in the example below.
252
264
265
+ ### Download Problems
266
+
267
+ There have been many reports about corrupted PDFs or images when using ` send() ` .
268
+ They are often caused by the webserver (Apache, Nginx, ...) performing additional
269
+ compression. This will mess up the ` Content-Length ` header which is added by this
270
+ library. It's useful to let the browser show a progress bar.
271
+
272
+ To fix this there are two options:
273
+
274
+ 1 . Exclude the download URL from compression in your Webserver. For example if your
275
+ script is called ` pdf.php ` then for [ mod_deflate] ( https://httpd.apache.org/docs/2.4/mod/mod_deflate.html ) in Apache
276
+ you could try to add this to your configuration:
277
+ ```
278
+ SetEnvIfNoCase REQUEST_URI ^/pdf.php$ no-gzip dont-vary
279
+ ```
280
+
281
+ For Nginx there are [similar solutions](https://serverfault.com/questions/438237/turn-off-gzip-for-a-location-in-nginx)
282
+ to disable `gzip` for a specific location.
283
+
284
+ 2. Suppress the `Content-Length` header when you send a file (available since 2.5.0):
285
+
286
+ ```php
287
+ <?php
288
+ $pdf->send('name.pdf', false, array(
289
+ 'Content-Length' => false,
290
+ ));
291
+ $image->send('name.png', false, array(
292
+ 'Content-Length' => false,
293
+ ));
294
+ ```
295
+
253
296
254
297
## Installation of wkhtmltopdf
255
298
@@ -281,6 +324,7 @@ which will create quite some extra load on your CPU. So this setup is only recom
281
324
To use the built in support you have to set `enableXvfb` in the `commandOptions`. There are also some options you can set.
282
325
283
326
```php
327
+ <?php
284
328
$pdf = new Pdf(array(
285
329
// Explicitly tell wkhtmltopdf that we're using an X environment
286
330
'use-xserver',
@@ -314,6 +358,7 @@ If your `Xvfb` process is running, you just have to tell the class to use this X
314
358
rendering. This is done via an environment variable.
315
359
316
360
``` php
361
+ <?php
317
362
$pdf = new Pdf(array(
318
363
'use-xserver',
319
364
'commandOptions' => array(
@@ -330,6 +375,7 @@ But then I had scaling issues which went away after I set all margins to zero an
330
375
added the margins through CSS. You can also use ` cm ` or ` in ` in CSS as this is more apropriate for print styles.
331
376
332
377
``` php
378
+ <?php
333
379
use mikehaertl\wkhtmlto\Pdf;
334
380
335
381
// Create a new Pdf object with some global PDF options
0 commit comments