|
1 | 1 | PHPHeadlessChrome
|
2 | 2 | ===============
|
3 | 3 |
|
| 4 | +[](https://packagist.org/packages/daandesmedt/phpheadlesschrome) |
| 5 | +[](https://packagist.org/packages/daandesmedt/phpheadlesschrome) |
| 6 | +[](https://packagist.org/packages/daandesmedt/phpheadlesschrome) |
| 7 | + |
| 8 | + |
4 | 9 | Headless Chrome is shipping in Chrome 59. It's a way to run the Chrome browser in a headless environment. Essentially, running Chrome without chrome! It brings all modern web platform features provided by Chromium and the Blink rendering engine to the command line.
|
5 | 10 |
|
6 | 11 | PHPHeadlessChrome provides a simple usage helper class to create PDF and / or screenshots using Headless Chrome.
|
7 | 12 | Trigger PDF / Screenshots generation from local file / URL.
|
8 | 13 |
|
9 |
| -In order to use this PHPHeadlessChrome helper make sure Google Chrome is correctly installer from version 59 and onwards. |
| 14 | +In order to use this PHPHeadlessChrome helper make sure Google Chrome is correctly installer from version 59 and onwards. |
| 15 | + |
| 16 | + |
| 17 | +## Installation |
| 18 | + |
| 19 | +Install the package through [composer](http://getcomposer.org): |
| 20 | + |
| 21 | +``` |
| 22 | +composer require daandesmedt/phpheadlesschrome |
| 23 | +``` |
| 24 | + |
| 25 | +Make sure, that you include the composer [autoloader](https://getcomposer.org/doc/01-basic-usage.md#autoloading) somewhere in your codebase. |
| 26 | + |
| 27 | + |
| 28 | +## Usage |
| 29 | + |
| 30 | +Use the `PHPHeadlessChrome` tool when you want to convert a webpage / HTML text or (local) HTML file to a PDF or image screenshot. |
| 31 | + |
| 32 | + |
| 33 | +## Working examples |
| 34 | + |
| 35 | +Working examples can be found in the `examples` folder. |
| 36 | + |
| 37 | + |
| 38 | +## Webpage (URL) to PDF |
| 39 | + |
| 40 | +```php |
| 41 | +<?php |
| 42 | + |
| 43 | +require __DIR__ . '/../vendor/autoload.php'; |
| 44 | + |
| 45 | +use daandesmedt\PHPHeadlessChrome\HeadlessChrome; |
| 46 | + |
| 47 | +$headlessChromer = new HeadlessChrome(); |
| 48 | +$headlessChromer->setUrl('http://www.google.be'); |
| 49 | +$headlessChromer->setBinaryPath('C:\Program Files (x86)\Google\Chrome\Application\chrome'); |
| 50 | +$headlessChromer->setOutputDirectory(__DIR__); |
| 51 | +$headlessChromer->toPDF('output.pdf'); |
| 52 | + |
| 53 | +print 'PDF generated to : ' . $headlessChromer->getFilePath(); |
| 54 | +``` |
| 55 | + |
| 56 | + |
| 57 | +## Webpage (URL) to Screenshot (image) |
| 58 | + |
| 59 | +```php |
| 60 | +<?php |
| 61 | + |
| 62 | +require __DIR__ . '/../vendor/autoload.php'; |
| 63 | + |
| 64 | +use daandesmedt\PHPHeadlessChrome\HeadlessChrome; |
| 65 | + |
| 66 | +$headlessChromer = new HeadlessChrome(); |
| 67 | +$headlessChromer->setUrl('http://www.google.be'); |
| 68 | +$headlessChromer->setBinaryPath('C:\Program Files (x86)\Google\Chrome\Application\chrome'); |
| 69 | +$headlessChromer->setOutputDirectory(__DIR__); |
| 70 | +$headlessChromer->toScreenShot('output.jpg'); |
| 71 | + |
| 72 | +print 'Screenshot saved to : ' . $headlessChromer->getFilePath(); |
| 73 | +``` |
| 74 | + |
| 75 | + |
| 76 | +## HTML (String) to PDF |
| 77 | + |
| 78 | +```php |
| 79 | +<?php |
| 80 | + |
| 81 | +require __DIR__ . '/../vendor/autoload.php'; |
| 82 | + |
| 83 | +use daandesmedt\PHPHeadlessChrome\HeadlessChrome; |
| 84 | + |
| 85 | +$headlessChromer = new HeadlessChrome(); |
| 86 | +$headlessChromer->setBinaryPath('C:\Program Files (x86)\Google\Chrome\Application\chrome'); |
| 87 | +$headlessChromer->setOutputDirectory(__DIR__); |
| 88 | +$headlessChromer->setHTML('<h1>Headless Chrome PHP example</h1><h3>HTML to PDF</h3>'); |
| 89 | +$headlessChromer->toPDF('output.pdf'); |
| 90 | + |
| 91 | +print 'PDF generated to : ' . $headlessChromer->getFilePath(); |
| 92 | +``` |
| 93 | + |
| 94 | + |
| 95 | +## HTML (String) to Screenshot (image) |
| 96 | + |
| 97 | +```php |
| 98 | +<?php |
| 99 | + |
| 100 | +require __DIR__ . '/../vendor/autoload.php'; |
| 101 | + |
| 102 | +use daandesmedt\PHPHeadlessChrome\HeadlessChrome; |
| 103 | + |
| 104 | +$headlessChromer = new HeadlessChrome(); |
| 105 | +$headlessChromer->setBinaryPath('C:\Program Files (x86)\Google\Chrome\Application\chrome'); |
| 106 | +$headlessChromer->setOutputDirectory(__DIR__); |
| 107 | +$headlessChromer->setHTML('<h1>Headless Chrome PHP example</h1><h3>HTML to PDF</h3>'); |
| 108 | +$headlessChromer->toScreenShot('output.jpg'); |
| 109 | + |
| 110 | +print 'Screenshot saved to : ' . $headlessChromer->getFilePath(); |
| 111 | +``` |
| 112 | + |
| 113 | + |
| 114 | + |
| 115 | +## HTML local file to PDF |
| 116 | + |
| 117 | +```php |
| 118 | +<?php |
| 119 | + |
| 120 | +require __DIR__ . '/../vendor/autoload.php'; |
| 121 | + |
| 122 | +use daandesmedt\PHPHeadlessChrome\HeadlessChrome; |
| 123 | + |
| 124 | +$headlessChromer = new HeadlessChrome(); |
| 125 | +$headlessChromer->setBinaryPath('C:\Program Files (x86)\Google\Chrome\Application\chrome'); |
| 126 | +$headlessChromer->setOutputDirectory(__DIR__); |
| 127 | +$headlessChromer->setHTMLFile(__DIR__ . '\assets\HTMLFile.html'); |
| 128 | +$headlessChromer->toPDF('output.pdf'); |
| 129 | + |
| 130 | +print 'PDF generated to : ' . $headlessChromer->getFilePath(); |
| 131 | +``` |
| 132 | + |
| 133 | + |
| 134 | +## HTML local file to Screenshot (image) |
| 135 | + |
| 136 | +```php |
| 137 | +<?php |
| 138 | + |
| 139 | +require __DIR__ . '/../vendor/autoload.php'; |
| 140 | + |
| 141 | +use daandesmedt\PHPHeadlessChrome\HeadlessChrome; |
| 142 | + |
| 143 | +$headlessChromer = new HeadlessChrome(); |
| 144 | +$headlessChromer->setBinaryPath('C:\Program Files (x86)\Google\Chrome\Application\chrome'); |
| 145 | +$headlessChromer->setOutputDirectory(__DIR__); |
| 146 | +$headlessChromer->setHTMLFile(__DIR__ . '\assets\HTMLFile.html'); |
| 147 | +$headlessChromer->toScreenShot('output.jpg'); |
| 148 | + |
| 149 | +print 'Screenshot saved to : ' . $headlessChromer->getFilePath(); |
| 150 | +``` |
| 151 | + |
| 152 | + |
| 153 | +## Set mobile mode |
| 154 | + |
| 155 | +```php |
| 156 | +$headlessChromer->useMobile(); |
| 157 | +``` |
| 158 | + |
| 159 | +## Set window size |
| 160 | + |
| 161 | +```php |
| 162 | +$headlessChromer->setWindowSize(375, 667); |
| 163 | +``` |
0 commit comments