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

fix disableCaptureURLS #873

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ jobs:
- name: Checkout code
uses: actions/checkout@v4

- name: Install poppler-utils
run: sudo apt-get install -y poppler-utils

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
Expand Down
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
## 4.3.0 - 2024-08-22

### What's Changed

* Fix empty PDF issue with Puppeteer ^23.0.0 by @JeppeKnockaert in https://github.com/spatie/browsershot/pull/876

**Full Changelog**: https://github.com/spatie/browsershot/compare/4.2.1...4.3.0

## 4.2.1 - 2024-08-20

Revert changes of 4.2.1 because PDFs do not render correctly anymore (see https://github.com/spatie/laravel-pdf/issues/175)
Expand Down
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,18 @@ We highly appreciate you sending us a postcard from your hometown, mentioning wh

All documentation is available [on our documentation site](https://spatie.be/docs/browsershot).

## Testing

For running the testsuite, you'll need to have Puppeteer installed. Pleaser refer to the Browsershot requirements [here](https://spatie.be/docs/browsershot/v4/requirements). Usually `npm -g i puppeteer` will do the trick.

Additionally, you'll need the `pdftotext` CLI which is part of the poppler-utils package. More info can be found in in the [spatie/pdf-to-text readme](https://github.com/spatie/pdf-to-text?tab=readme-ov-file#requirements). Usually `brew install poppler-utils` will suffice.

Finally run the tests with:

```bash
composer test
```

## Contributing

Please see [CONTRIBUTING](https://github.com/spatie/.github/blob/main/CONTRIBUTING.md) for details.
Expand Down
9 changes: 6 additions & 3 deletions bin/browser.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ const getOutput = async (request, page = null) => {
const result = await page[request.action](request.options);

// Ignore output result when saving to a file
output.result = request.options.path ? '' : result.toString('base64');
output.result = request.options.path
? ''
: (result instanceof Uint8Array ? Buffer.from(result) : result).toString('base64');
}
}

Expand Down Expand Up @@ -99,6 +101,7 @@ const callChrome = async pup => {
...(request.options.env || {}),
...process.env
},
protocolTimeout: request.options.protocolTimeout ?? 30000,
});
}

Expand Down Expand Up @@ -158,7 +161,7 @@ const callChrome = async pup => {
page.on('request', interceptedRequest => {
var headers = interceptedRequest.headers();

if (request.options && request.options.disableCaptureURLS) {
if (!request.options || !request.options.disableCaptureURLS) {
requestsList.push({
url: interceptedRequest.url(),
});
Expand Down Expand Up @@ -384,7 +387,7 @@ const callChrome = async pup => {
if (request.options.waitForSelector) {
await page.waitForSelector(request.options.waitForSelector, (request.options.waitForSelectorOptions ? request.options.waitForSelectorOptions : undefined));
}

console.log(await getOutput(request, page));

if (remoteInstance && page) {
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"require-dev": {
"pestphp/pest": "^1.20",
"spatie/image": "^3.6",
"spatie/pdf-to-text": "^1.52",
"spatie/phpunit-snapshot-assertions": "^4.2.3"
},
"autoload": {
Expand Down
5 changes: 5 additions & 0 deletions src/Browsershot.php
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,11 @@ public function timeout(int $timeout): static
return $this->setOption('timeout', $timeout * 1000);
}

public function protocolTimeout(int $protocolTimeout): static
{
return $this->setOption('protocolTimeout', $protocolTimeout * 1000);
}

public function userAgent(string $userAgent): static
{
return $this->setOption('userAgent', $userAgent);
Expand Down
13 changes: 11 additions & 2 deletions tests/BrowsershotTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1004,7 +1004,9 @@

$this->expectException(ProcessFailedException::class);

$instance = Browsershot::url('https://example.com');
// Block the favicon request to prevent randomness in the output.
$instance = Browsershot::url('https://example.com')
->blockUrls(['https://example.com/favicon.ico']);

try {
$instance->save($targetPath);
Expand All @@ -1014,7 +1016,14 @@
expect($output)->not()->toBeNull();
expect($output)->toBeInstanceOf(ChromiumResult::class);
expect($output->getException())->not()->toBeEmpty();
expect($output->getConsoleMessages())->toBe([]);
expect($output->getConsoleMessages())->toBe([
[
'type' => 'error',
'message' => 'Failed to load resource: net::ERR_FAILED',
'location' => ['url' => 'https://example.com/favicon.ico'],
'stackTrace' => [['url' => 'https://example.com/favicon.ico']],
],
]);
expect($output->getRequestsList())->toMatchArray([[
'url' => 'https://example.com/',
]]);
Expand Down
12 changes: 12 additions & 0 deletions tests/PdfTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use Spatie\Browsershot\Browsershot;
use Spatie\PdfToText\Pdf;

it('can save a pdf by using the pdf extension', function () {
$targetPath = __DIR__.'/temp/testPdf.pdf';
Expand Down Expand Up @@ -66,6 +67,17 @@
expect(is_string($base64))->toBeTrue();
});

it('can return a pdf', function () {
$binPath = PHP_OS === 'Linux' ? '/usr/bin/pdftotext' : '/opt/homebrew/bin/pdftotext';
$targetPath = __DIR__.'/temp/testPdf.pdf';

$pdf = Browsershot::url('https://example.com')
->pdf();
file_put_contents($targetPath, $pdf);

expect(Pdf::getText($targetPath, $binPath))->toContain('Example Domain');
});

it('can write options to a file and generate a pdf', function () {
$targetPath = __DIR__.'/temp/testPdf.pdf';

Expand Down