From f73e1ac382b294edf9de0c4468c174d31e7f9ba8 Mon Sep 17 00:00:00 2001 From: Daum Date: Sun, 20 Nov 2022 08:58:43 -0500 Subject: [PATCH] Allow user to explicitly use a local file for html content. 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. --- README.md | 6 ++++ src/Browsershot.php | 33 +++++++++++++++++--- src/Exceptions/FileDoesNotExistException.php | 13 ++++++++ tests/BrowsershotTest.php | 19 +++++++++++ 4 files changed, 67 insertions(+), 4 deletions(-) create mode 100644 src/Exceptions/FileDoesNotExistException.php diff --git a/README.md b/README.md index 1d4bfc04..471183f3 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,12 @@ You can also use an arbitrary html input, simply replace the `url` method with ` Browsershot::html('

Hello world!!

')->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 diff --git a/src/Browsershot.php b/src/Browsershot.php index ddb5b2e1..b4adfa2a 100644 --- a/src/Browsershot.php +++ b/src/Browsershot.php @@ -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; @@ -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; @@ -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; @@ -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, @@ -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 = []; @@ -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, @@ -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; + } } diff --git a/src/Exceptions/FileDoesNotExistException.php b/src/Exceptions/FileDoesNotExistException.php new file mode 100644 index 00000000..cd32cab5 --- /dev/null +++ b/src/Exceptions/FileDoesNotExistException.php @@ -0,0 +1,13 @@ +toEqual($mimeType); }); + + +it('can set html contents from a file', function () { + $inputFile = __DIR__.'/temp/test.html'; + $inputHtml = '

Hello World

'; + + 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);