Skip to content

Commit

Permalink
Added disablePDFHeader()
Browse files Browse the repository at this point in the history
  • Loading branch information
DaanDeSmedt committed Jul 31, 2020
1 parent 5d85a3e commit 10f234f
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 49 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,4 +173,10 @@ $headlessChromer->useMobile();

```php
$headlessChromer->setWindowSize(375, 667);
```

## Disable display of header and footer in the PDF print

```php
$headlessChromer->disablePDFHeader();
```
17 changes: 17 additions & 0 deletions examples/PDFNoHeaderAndFooter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

require __DIR__ . '/../vendor/autoload.php';

use daandesmedt\PHPHeadlessChrome\HeadlessChrome;

$headlessChromer = new HeadlessChrome();
$headlessChromer->setUrl('http://www.google.be');
$headlessChromer->setBinaryPath('C:\Program Files (x86)\Google\Chrome\Application\chrome');
// or $headlessChromer = new HeadlessChrome('http://www.google.be','C:\Program Files (x86)\Google\Chrome\Application\chrome');

$headlessChromer->disablePDFHeader();

$headlessChromer->setOutputDirectory(__DIR__);
$headlessChromer->toPDF('output.pdf');

print 'PDF generated to : ' . $headlessChromer->getFilePath();
124 changes: 75 additions & 49 deletions src/HeadlessChrome.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<?php
<?php

namespace daandesmedt\PHPHeadlessChrome;

use Exception;
use mikehaertl\shellcommand\Command;


class HeadlessChrome {
class HeadlessChrome
{

/**
* Path to the Chrome binary
Expand All @@ -18,7 +19,7 @@ class HeadlessChrome {
* Argument list to execute for Headless Chrome
* @var String
*/
private $arguments;
private $arguments;

/**
* Output directory for render
Expand Down Expand Up @@ -54,37 +55,38 @@ class HeadlessChrome {
* Check the value for <Executable Path>
*
*/
public function __construct($url = null, $binaryPath = null, $outputDirectory = null){

public function __construct($url = null, $binaryPath = null, $outputDirectory = null)
{

$this->setArguments([
'--headless' => '',
'--disable-gpu' => '',
'--incognito' => '',
'--enable-viewport' => ''
]);

if(isset($binaryPath)) {
if (isset($binaryPath)) {
$this->setBinaryPath($binaryPath);
}
if (isset($url)){

if (isset($url)) {
$this->setUrl($url);
}

if(isset($outputDirectory)){
if (isset($outputDirectory)) {
$this->setOutputDirectory($outputDirectory);
} else {
$this->setOutputDirectory(sys_get_temp_dir());
}

}


/**
* Set a list of arguments
* @param Array $arguments
*/
public function setArguments(array $arguments) {
public function setArguments(array $arguments)
{
foreach ($arguments as $argument => $value) {
$this->setArgument($argument, $value);
}
Expand All @@ -96,8 +98,9 @@ public function setArguments(array $arguments) {
* @param String $argument
* @param String $value
*/
public function setArgument($argument, $value) {
if(!strlen(trim($argument)) && !strlen(trim($value))) return;
public function setArgument($argument, $value)
{
if (!strlen(trim($argument)) && !strlen(trim($value))) return;
$argument = trim($argument);
if (!empty($value) && !strstr($argument, '=')) {
$argument .= '=';
Expand All @@ -110,7 +113,8 @@ public function setArgument($argument, $value) {
* Set path to the Chrome binary
* @param String $binaryPath
*/
public function setBinaryPath($binaryPath){
public function setBinaryPath($binaryPath)
{
$this->binaryPath = $binaryPath;
}

Expand All @@ -119,7 +123,8 @@ public function setBinaryPath($binaryPath){
* Set the target URL
* @param String $url
*/
public function setUrl($url) {
public function setUrl($url)
{
$this->url = trim($url);
}

Expand All @@ -142,7 +147,8 @@ public function setOutputDirectory($directory)
* @param integer $width
* @param integer $height
*/
public function setWindowSize($width, $height) {
public function setWindowSize($width, $height)
{
$this->setArgument('--window-size', $width . ',' . $height);
}

Expand All @@ -151,7 +157,8 @@ public function setWindowSize($width, $height) {
* Set raw HTML
* @param String $html
*/
public function setHTML($html) {
public function setHTML($html)
{
$this->setUrl('data:text/html,' . rawurlencode($html));
}

Expand All @@ -161,7 +168,8 @@ public function setHTML($html) {
* @param String $filePath
* @throws Exception If the file is not found
*/
public function setHTMLFile($filePath) {
public function setHTMLFile($filePath)
{
if (!file_exists($filePath)) {
throw new Exception("$filePath not found");
}
Expand All @@ -172,17 +180,28 @@ public function setHTMLFile($filePath) {
/**
* Sets the mobile mode
*/
public function useMobile(){
public function useMobile()
{
$this->setArgument('--user-agent', 'Mozilla/5.0 (Linux; U; Android 4.0.3; ko-kr; LG-L160L Build/IML74K) AppleWebkit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30');
}


/**
* Disable display of header and footer in the PDF file
*/
public function disablePDFHeader()
{
$this->setArgument('--print-to-pdf-no-header', '');
}


/**
*
* Get path to the Chrome binary
* @return String $binaryPath
*/
public function getBinaryPath(){
public function getBinaryPath()
{
return $this->binaryPath;
}

Expand All @@ -191,7 +210,8 @@ public function getBinaryPath(){
* Get the target URL
* @return String $url
*/
public function getUrl() {
public function getUrl()
{
return $this->url;
}

Expand All @@ -200,7 +220,8 @@ public function getUrl() {
* Get the directory for render output (PDF / Screenshot)
* @return String $directory
*/
public function getOutputDirectory() {
public function getOutputDirectory()
{
return $this->outputDirectory;
}

Expand All @@ -209,7 +230,8 @@ public function getOutputDirectory() {
* Gets arguments list used for executing Headless Chrome
* @return Array
*/
public function getArguments(){
public function getArguments()
{
return $this->arguments;
}

Expand All @@ -219,7 +241,8 @@ public function getArguments(){
* @param String $extension (pdf / html)
* @return String unique filename
*/
private function getUniqueName($extension) {
private function getUniqueName($extension)
{
return md5(date('Y-m-d H:i:s:u')) . '.' . $extension;
}

Expand All @@ -230,9 +253,10 @@ private function getUniqueName($extension) {
* @return String $location
* @throws Exception
*/
public function toPDF($PDFFilename = null){

if(!isset($PDFFilename)){
public function toPDF($PDFFilename = null)
{

if (!isset($PDFFilename)) {
$PDFFilename = $this->getUniqueName('pdf');
}

Expand All @@ -253,7 +277,6 @@ public function toPDF($PDFFilename = null){
}

return $location;

}

/**
Expand All @@ -262,9 +285,10 @@ public function toPDF($PDFFilename = null){
* @return String $location
* @throws Exception
*/
public function toScreenShot($imageFilename = null) {
public function toScreenShot($imageFilename = null)
{

if(!isset($imageFilename)){
if (!isset($imageFilename)) {
$imageFilename = $this->getUniqueName('jpg');
}

Expand All @@ -285,20 +309,20 @@ public function toScreenShot($imageFilename = null) {
}

return $location;

}


/**
* Execute Chrome using all provided arguments
* @param Array $arguments
* @return Boolean
*/
private function executeChrome(array $arguments) {
$this->command = new Command('' . $this->binaryPath . '');
private function executeChrome(array $arguments)
{
$this->command = new Command('' . $this->binaryPath . '');
foreach ($arguments as $argument => $value) {
$this->command->addArg($argument, $value ? $value : null);
}
}
$this->command->addArg($this->url, null);

if (!$this->command->execute()) {
Expand All @@ -312,8 +336,9 @@ private function executeChrome(array $arguments) {
* Get the last error message
* @return String the error message, either stderr or internal message. Empty if none.
*/
public function getError(){
if(!$this->command) return;
public function getError()
{
if (!$this->command) return;
return $this->command->getError();
}

Expand All @@ -322,8 +347,9 @@ public function getError(){
* Get the last command exit code
* @return Int|null the exit code or null if command was not executed yet
*/
public function getExitCode(){
if(!$this->command) return;
public function getExitCode()
{
if (!$this->command) return;
return $this->command->getExitCode();
}

Expand All @@ -332,8 +358,9 @@ public function getExitCode(){
* Get command string command
* @return string the current command string to execute
*/
public function getCommandString(){
if(!$this->command) return;
public function getCommandString()
{
if (!$this->command) return;
return $this->command->__toString();
}

Expand All @@ -342,8 +369,9 @@ public function getCommandString(){
* Get last know filePath location
* @return string the current command string to execute
*/
public function getFilePath(){
if(!$this->command) return;
public function getFilePath()
{
if (!$this->command) return;
return $this->filePath;
}

Expand All @@ -353,7 +381,8 @@ public function getFilePath(){
* @return String HTML DOM
* @throws Exception
*/
public function getDOM(){
public function getDOM()
{

$specific_arguments = [
'--dump-dom' => ''
Expand All @@ -362,11 +391,8 @@ public function getDOM(){
$arguments = array_merge($specific_arguments, $this->getArguments());
if (!$this->executeChrome($arguments)) {
throw new Exception('An error occurred while getting the DOM; <' . $this->getExitCode() . '> with message "' . $this->getError() . '"');
} else {
return $this->command->getOutput();
}
else {
return $this->command->getOutput();
}
}


}

0 comments on commit 10f234f

Please sign in to comment.