Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow user to explicitly use a local file for html content. #687

Merged
merged 1 commit into from
Nov 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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);