Skip to content

Commit

Permalink
Allow user to explicitly use a local file for html content.
Browse files Browse the repository at this point in the history
Sometimes it is necessary to have all the html contents in a local
file rather than on a url or in a PHP string due to memory limits.
The introduced function htmlFromFilePath makes it clear you are
intending to set the html from a local file, and there for
if the input is not being generated by the user themselves, they
should validate the file path is allowed before allowing it to be
used.
  • Loading branch information
daum authored and freekmurze committed Nov 21, 2022
1 parent e7041d0 commit 554c3e5
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 4 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ You can also use an arbitrary html input, simply replace the `url` method with `
Browsershot::html('<h1>Hello world!!</h1>')->save('example.pdf');
```

If your HTML input is already in a file locally use the :

```php
Browsershot::htmlFromFilePath('/local/path/to/file.html')->save('example.pdf');
```

Browsershot also can get the body of an html page after JavaScript has been executed:

```php
Expand Down
33 changes: 29 additions & 4 deletions src/Browsershot.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Spatie\Browsershot\Exceptions\CouldNotTakeBrowsershot;
use Spatie\Browsershot\Exceptions\ElementNotFound;
use Spatie\Browsershot\Exceptions\FileDoesNotExistException;
use Spatie\Browsershot\Exceptions\FileUrlNotAllowed;
use Spatie\Browsershot\Exceptions\HtmlIsNotAllowedToContainFile;
use Spatie\Browsershot\Exceptions\UnsuccessfulResponse;
Expand Down Expand Up @@ -66,6 +67,11 @@ public static function html(string $html)
return (new static())->setHtml($html);
}

public static function htmlFromFilePath(string $filePath): self
{
return (new static())->setHtmlFromFilePath($filePath);
}

public function __construct(string $url = '', bool $deviceEmulate = false)
{
$this->url = $url;
Expand Down Expand Up @@ -247,6 +253,19 @@ public function setUrl(string $url)
return $this;
}

public function setHtmlFromFilePath(string $filePath): self
{
if(false === file_exists($filePath)){
throw new FileDoesNotExistException($filePath);
}

$this->url = 'file://'.$filePath;
$this->html = '';

return $this;

}

public function setProxyServer(string $proxyServer)
{
$this->proxyServer = $proxyServer;
Expand Down Expand Up @@ -675,14 +694,14 @@ public function applyManipulations(string $imagePath)

public function createBodyHtmlCommand(): array
{
$url = $this->html ? $this->createTemporaryHtmlFile() : $this->url;
$url = $this->getFinalContentsUrl();

return $this->createCommand($url, 'content');
}

public function createScreenshotCommand($targetPath = null): array
{
$url = $this->html ? $this->createTemporaryHtmlFile() : $this->url;
$url = $this->getFinalContentsUrl();

$options = [
'type' => $this->screenshotType,
Expand All @@ -706,7 +725,7 @@ public function createScreenshotCommand($targetPath = null): array

public function createPdfCommand($targetPath = null): array
{
$url = $this->html ? $this->createTemporaryHtmlFile() : $this->url;
$url = $this->getFinalContentsUrl();

$options = [];

Expand All @@ -733,7 +752,7 @@ public function createPdfCommand($targetPath = null): array

public function createEvaluateCommand(string $pageFunction): array
{
$url = $this->html ? $this->createTemporaryHtmlFile() : $this->url;
$url = $this->getFinalContentsUrl();

$options = [
'pageFunction' => $pageFunction,
Expand Down Expand Up @@ -990,4 +1009,10 @@ private function isWindows()
{
return strtoupper(substr(PHP_OS, 0, 3)) === 'WIN';
}

private function getFinalContentsUrl(): string
{
$url = $this->html ? $this->createTemporaryHtmlFile() : $this->url;
return $url;
}
}
13 changes: 13 additions & 0 deletions src/Exceptions/FileDoesNotExistException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Spatie\Browsershot\Exceptions;

use Exception;

class FileDoesNotExistException extends Exception
{
public function __construct($file)
{
parent::__construct("The file `{$file} does not exist");
}
}
19 changes: 19 additions & 0 deletions tests/BrowsershotTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1555,3 +1555,22 @@

expect('application/pdf')->toEqual($mimeType);
});


it('can set html contents from a file', function () {
$inputFile = __DIR__.'/temp/test.html';
$inputHtml = '<html><head></head><body><h1>Hello World</h1></body></html>';

file_put_contents($inputFile, $inputHtml);


$outputHtml = Browsershot::htmlFromFilePath($inputFile)
->usePipe()
->bodyHtml();

expect($outputHtml)->toEqual($inputHtml);
});

it('can not set html contents from a non-existent file', function () {
Browsershot::htmlFromFilePath(__DIR__.'/temp/non-existent-file.html');
})->throws(\Spatie\Browsershot\Exceptions\FileDoesNotExistException::class);

0 comments on commit 554c3e5

Please sign in to comment.