|
| 1 | +# PHP wrapper for chrome-pdf |
| 2 | + |
| 3 | +This is a PHP wrapper for [SynergiTech/chrome-pdf](https://github.com/SynergiTech/chrome-pdf) inspired by [KnpLabs/snappy](https://github.com/KnpLabs/snappy). |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +The `chrome-pdf` binary should be present on your system and optionally available on your path. |
| 8 | + |
| 9 | +``` |
| 10 | +composer require synergitech/chrome-pdf-php |
| 11 | +``` |
| 12 | + |
| 13 | +## Usage |
| 14 | + |
| 15 | +Instantiate the class, passing the path to the `chrome-pdf` binary if necessary, and apply options in the same way you would with Snappy. |
| 16 | + |
| 17 | +**Please Note** You must remember to set `displayHeaderFooter` if you want to set either a header or footer and apply margin to make it visible in the PDF. |
| 18 | + |
| 19 | +The available options can be seen in the code. There is also functionality to handle a CSS-style declaring of margins to make input easier. |
| 20 | + |
| 21 | +## Examples |
| 22 | + |
| 23 | +### Laravel Example |
| 24 | + |
| 25 | +```php |
| 26 | +use SynergiTech\ChromePDF\PDF; |
| 27 | + |
| 28 | +// a controller function |
| 29 | + |
| 30 | +$contents = view('enquiries.quote', array('enquiry' => $enquiry))->render(); |
| 31 | + |
| 32 | +$pdf = new PDF(); |
| 33 | + |
| 34 | +$contents = $pdf->getOutputFromHtml($contents); |
| 35 | + |
| 36 | +return response($contents)->withHeaders(array( |
| 37 | + 'Content-Type' => 'application/pdf', |
| 38 | + 'Content-Disposition' => 'inline; filename="enquiry.pdf"', |
| 39 | +)); |
| 40 | +``` |
| 41 | + |
| 42 | +### FuelPHP Example (converting from Snappy) |
| 43 | + |
| 44 | +```php |
| 45 | + |
| 46 | +// a controller function |
| 47 | + |
| 48 | +$pdf = new \SynergiTech\ChromePDF\SnappyPDF(); |
| 49 | + |
| 50 | +$pdf->setOption('displayHeaderFooter', 'true'); |
| 51 | +$pdf->setOption('header-html', \View::forge('manage/invoices/pdfheader')); |
| 52 | +$pdf->setOption('footer-html', \View::forge('manage/invoices/pdffooter')->set(array( |
| 53 | + 'invoice' => $invoice |
| 54 | +))); |
| 55 | +$pdf->setOption('margin', '100px 0'); |
| 56 | + |
| 57 | +$contents = $pdf->getOutputFromHtml( |
| 58 | + \View::forge('manage/invoices/pdfbody') |
| 59 | + ->set(array( |
| 60 | + 'invoice' => $invoice, |
| 61 | + )) |
| 62 | +); |
| 63 | +return \Response::forge( |
| 64 | + $contents, |
| 65 | + 200, |
| 66 | + array( |
| 67 | + 'Content-Type' => 'application/pdf', |
| 68 | + 'Content-Disposition' => 'inline; filename="invoice.pdf"' |
| 69 | + ) |
| 70 | + ); |
| 71 | +``` |
| 72 | + |
| 73 | + |
0 commit comments