Skip to content

Commit f51c176

Browse files
committed
Issue #270 Add note about content-length fix to readme
1 parent 17ee713 commit f51c176

File tree

1 file changed

+48
-2
lines changed

1 file changed

+48
-2
lines changed

README.md

+48-2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ somewhere in your codebase.
2929
### Single page PDF
3030

3131
```php
32+
<?php
3233
use mikehaertl\wkhtmlto\Pdf;
3334

3435
// 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')) {
4748

4849

4950
```php
51+
<?php
5052
use mikehaertl\wkhtmlto\Pdf;
5153

5254
$pdf = new Pdf;
@@ -85,6 +87,7 @@ $content = $pdf->toString();
8587
### Creating an image
8688

8789
```php
90+
<?php
8891
use mikehaertl\wkhtmlto\Image;
8992

9093
// 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:
115118
Please see `wkhtmltopdf -H` for a full explanation. All options are passed as array, for example:
116119

117120
```php
121+
<?php
118122
$options = array(
119123
'no-outline', // option without argument
120124
'encoding' => 'UTF-8', // option with argument
@@ -139,6 +143,7 @@ $options = array(
139143
Options can be passed to several methods for PDFs:
140144

141145
```php
146+
<?php
142147
$pdf = new Pdf($globalOptions); // Set global PDF options
143148
$pdf->setOptions($globalOptions); // Set global PDF options (alternative)
144149
$pdf->addPage($page, $pageOptions); // Add page with options
@@ -152,6 +157,7 @@ $pdf->addToc($tocOptions); // Add TOC with options
152157
For `wkhtmltoimage` there's only one set of options:
153158

154159
```php
160+
<?php
155161
$image = new Image($options); // Set image options
156162
$image->setOptions($options); // Set image options (alternative)
157163
```
@@ -177,6 +183,7 @@ the `Image` class also has a `type` option:
177183
want to pass UTF-8 encoded arguments, you may have to set the `LANG` environment variable.
178184

179185
```php
186+
<?php
180187
$pdf = new Pdf(array(
181188
'binary' => '/obscure/path/to/wkhtmltopdf',
182189
'ignoreWarnings' => true,
@@ -201,6 +208,7 @@ If this doesn't work correctly you can also pass an instance of our `File`
201208
helper as a last resort:
202209

203210
```php
211+
<?php
204212
use mikehaertl\tmp\File;
205213
$options = [
206214
'header-html' => new File('Complex content', '.html'),
@@ -213,6 +221,7 @@ $options = [
213221
available from `getError()`:
214222

215223
```php
224+
<?php
216225
if (!$pdf->send()) {
217226
throw new Exception('Could not create PDF: '.$pdf->getError());
218227
}
@@ -223,13 +232,16 @@ if ($content === false) {
223232
}
224233
```
225234

226-
## Note for Windows users
235+
## Known Issues
236+
237+
### Use under Windows
227238

228239
If you use double quotes (`"`) or percent signs (`%`) as option values, they may get converted to spaces.
229240
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:
231242

232243
```php
244+
<?php
233245
$pdf = new Pdf(array(
234246
'commandOptions' => array(
235247
'escapeArgs' => false,
@@ -250,6 +262,37 @@ surround your argument values with extra double quotes.
250262
I also found that some options don't work on Windows (tested with wkhtmltopdf 0.11 rc2), like the
251263
`user-style-sheet` option used in the example below.
252264

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+
253296
254297
## Installation of wkhtmltopdf
255298
@@ -281,6 +324,7 @@ which will create quite some extra load on your CPU. So this setup is only recom
281324
To use the built in support you have to set `enableXvfb` in the `commandOptions`. There are also some options you can set.
282325
283326
```php
327+
<?php
284328
$pdf = new Pdf(array(
285329
// Explicitly tell wkhtmltopdf that we're using an X environment
286330
'use-xserver',
@@ -314,6 +358,7 @@ If your `Xvfb` process is running, you just have to tell the class to use this X
314358
rendering. This is done via an environment variable.
315359

316360
```php
361+
<?php
317362
$pdf = new Pdf(array(
318363
'use-xserver',
319364
'commandOptions' => array(
@@ -330,6 +375,7 @@ But then I had scaling issues which went away after I set all margins to zero an
330375
added the margins through CSS. You can also use `cm` or `in` in CSS as this is more apropriate for print styles.
331376

332377
```php
378+
<?php
333379
use mikehaertl\wkhtmlto\Pdf;
334380

335381
// Create a new Pdf object with some global PDF options

0 commit comments

Comments
 (0)