|
| 1 | +# [Browsershot](https://github.com/spatie/browsershot) wrapper for Laravel 5.5+ |
| 2 | +This package takes advantage of Google Chrome's Headless mode to take screenshots and generate PDFs from websites, views and raw html |
| 3 | + |
| 4 | + |
| 5 | +# Requirements |
| 6 | + |
| 7 | +* [Node](https://nodejs.org/) 7.6.0 or higher |
| 8 | +* [Google Chrome](https://www.google.com/chrome/) |
| 9 | +* [Puppeteer Node library](https://github.com/GoogleChrome/puppeteer). |
| 10 | + |
| 11 | +You can install Puppeteer in your project via NPM: |
| 12 | + |
| 13 | +```bash |
| 14 | +npm install puppeteer |
| 15 | +``` |
| 16 | + |
| 17 | +Or you could opt to just install it globally |
| 18 | + |
| 19 | +```bash |
| 20 | +npm install puppeteer --global |
| 21 | +``` |
| 22 | + |
| 23 | +On a [Forge](https://forge.laravel.com) provisioned Ubuntu 16.04 server you can install the latest stable version of Chrome like this: |
| 24 | + |
| 25 | +```bash |
| 26 | +curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash - |
| 27 | +sudo apt-get install -y nodejs gconf-service libasound2 libatk1.0-0 libc6 libcairo2 libcups2 libdbus-1-3 libexpat1 libfontconfig1 libgcc1 libgconf-2-4 libgdk-pixbuf2.0-0 libglib2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 libxcomposite1 libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6 ca-certificates fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils wget |
| 28 | +sudo npm install --global --unsafe-perm puppeteer |
| 29 | +sudo chmod -R o+rx /usr/lib/node_modules/puppeteer/.local-chromium |
| 30 | +``` |
| 31 | + |
| 32 | +# Installation |
| 33 | + |
| 34 | +Install the package through composer |
| 35 | + |
| 36 | +```bash |
| 37 | +composer require verumconsilium/laravel-browsershot |
| 38 | +``` |
| 39 | + |
| 40 | +After the package is installed the service provider will be automatically discoverted and two new Facades `PDF` and `Screenshot` will be available |
| 41 | + |
| 42 | +# Usage |
| 43 | + |
| 44 | +The recommended way to use this package is trhough its Facades |
| 45 | + |
| 46 | +## PDF |
| 47 | + |
| 48 | +### Generating a PDF from a view and returning it inline |
| 49 | + |
| 50 | +```php |
| 51 | + |
| 52 | + use VerumConsilium\Browsershot\Facades\PDF; |
| 53 | + |
| 54 | + ... |
| 55 | + |
| 56 | + return PDF::loadView('view.name', $data) |
| 57 | + ->inline(); |
| 58 | + |
| 59 | +``` |
| 60 | + |
| 61 | +You can chain all the methods available in the [browsershot master library](https://github.com/spatie/browsershot) |
| 62 | + |
| 63 | +### Returning the PDF as a download |
| 64 | + |
| 65 | +```php |
| 66 | + use VerumConsilium\Browsershot\Facades\PDF; |
| 67 | + |
| 68 | + ... |
| 69 | + |
| 70 | + return PDF::loadView('view.name', $data) |
| 71 | + ->margins(20, 0, 0, 20) |
| 72 | + ->download(); |
| 73 | +``` |
| 74 | + |
| 75 | +You can pass the custom file name and additional headers the response will have to the `inline` and `download` methods like |
| 76 | + |
| 77 | +```php |
| 78 | + PDF::loadHtml('<h1>Awesome PDF</h1>') |
| 79 | + ->download('myawesomepdf.pdf', [ |
| 80 | + 'Authorization' => 'token' |
| 81 | + ]); |
| 82 | +``` |
| 83 | + |
| 84 | +### Persisting PDF to disk |
| 85 | + |
| 86 | +If you would like to save the generated pdf file to your storage disk you can call the `store` or `storeAs` method |
| 87 | + |
| 88 | +``` |
| 89 | + $pdfStoredPath = PDF::loadUrl('https://google.com') |
| 90 | + ->store('pdfs/') |
| 91 | +``` |
| 92 | + |
| 93 | +This will use the default storage driver to store the pdf in the `pdfs/` folder giving it a unique name. If you would like to specify the name you can call de `storeAs` method |
| 94 | + |
| 95 | + |
| 96 | +``` |
| 97 | + $pdfStoredPath = PDF::loadUrl('https://google.com') |
| 98 | + ->storeAs('pdfs/', 'google.pdf') |
| 99 | +``` |
| 100 | + |
| 101 | +## Screenshots |
| 102 | + |
| 103 | +Screenshots are created the same way as PDFs just change the facade to `Screenshot` |
| 104 | + |
| 105 | +### Generating screenshots as JPG/JPEG |
| 106 | + |
| 107 | +By default screenshots will be taken as PNG format if you would like to use JPG instead call the `useJPG()` method |
| 108 | + |
| 109 | +```php |
| 110 | +use VerumConsilium\Browsershot\Facades\Screenshot; |
| 111 | + |
| 112 | +Screenshot::loadView('view.name', $data) |
| 113 | + ->useJPG() |
| 114 | + ->margins(20, 0, 0, 20) |
| 115 | + ->download(); |
| 116 | +``` |
0 commit comments