From 4d2710845316f926fb6796ea2620a0793147404c Mon Sep 17 00:00:00 2001 From: Ziaratban Date: Thu, 15 Aug 2024 16:47:12 +0330 Subject: [PATCH 1/7] fix --- bin/browser.cjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/browser.cjs b/bin/browser.cjs index a648123..1fef52b 100644 --- a/bin/browser.cjs +++ b/bin/browser.cjs @@ -158,7 +158,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(), }); From 733fa4863d1fce3a84f6919370d5ecf56b1465c8 Mon Sep 17 00:00:00 2001 From: Jeppe Knockaert Date: Wed, 21 Aug 2024 16:33:13 +0200 Subject: [PATCH 2/7] Revert "revert changes in 4.2" This reverts commit 7174e3fd75b60742da894ee94cf9bf97dc670b35. --- bin/browser.cjs | 14 ++++++++++---- src/Browsershot.php | 11 ++++++++--- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/bin/browser.cjs b/bin/browser.cjs index a648123..fd5ec4e 100644 --- a/bin/browser.cjs +++ b/bin/browser.cjs @@ -47,9 +47,14 @@ const getOutput = async (request, page = null) => { output.result = await page.evaluate(request.options.pageFunction); } else { const result = await page[request.action](request.options); + const resultBuffer = Buffer.from(result); - // Ignore output result when saving to a file - output.result = request.options.path ? '' : result.toString('base64'); + if (request.action === 'screenshot') { + output.result = resultBuffer.toString('base64'); + } else { + // Ignore output result when saving to a file + output.result = request.options.path ? '' : resultBuffer.toString(); + } } } @@ -99,6 +104,7 @@ const callChrome = async pup => { ...(request.options.env || {}), ...process.env }, + protocolTimeout: request.options.protocolTimeout ?? 30000, }); } @@ -158,7 +164,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(), }); @@ -384,7 +390,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) { diff --git a/src/Browsershot.php b/src/Browsershot.php index 3838ba7..a8fa007 100644 --- a/src/Browsershot.php +++ b/src/Browsershot.php @@ -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); @@ -669,7 +674,7 @@ public function pdf(): string $this->cleanupTemporaryHtmlFile(); - return base64_decode($encodedPdf); + return $encodedPdf; } public function savePdf(string $targetPath) @@ -689,11 +694,11 @@ public function base64pdf(): string { $command = $this->createPdfCommand(); - $encodedPdf = $this->callBrowser($command); + $pdf = $this->callBrowser($command); $this->cleanupTemporaryHtmlFile(); - return $encodedPdf; + return base64_encode($pdf); } public function evaluate(string $pageFunction): string From a270f121c844c246ce8944d17c1d2d4c9432ef01 Mon Sep 17 00:00:00 2001 From: Jeppe Knockaert Date: Wed, 21 Aug 2024 16:42:24 +0200 Subject: [PATCH 3/7] Use base64 for communication between puppeteer and Browsershot --- bin/browser.cjs | 11 ++++------- src/Browsershot.php | 6 +++--- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/bin/browser.cjs b/bin/browser.cjs index fd5ec4e..9d5b26d 100644 --- a/bin/browser.cjs +++ b/bin/browser.cjs @@ -47,14 +47,11 @@ const getOutput = async (request, page = null) => { output.result = await page.evaluate(request.options.pageFunction); } else { const result = await page[request.action](request.options); - const resultBuffer = Buffer.from(result); - if (request.action === 'screenshot') { - output.result = resultBuffer.toString('base64'); - } else { - // Ignore output result when saving to a file - output.result = request.options.path ? '' : resultBuffer.toString(); - } + // Ignore output result when saving to a file + output.result = request.options.path + ? '' + : (result instanceof Uint8Array ? Buffer.from(result) : result).toString('base64'); } } diff --git a/src/Browsershot.php b/src/Browsershot.php index a8fa007..d50ee04 100644 --- a/src/Browsershot.php +++ b/src/Browsershot.php @@ -674,7 +674,7 @@ public function pdf(): string $this->cleanupTemporaryHtmlFile(); - return $encodedPdf; + return base64_decode($encodedPdf); } public function savePdf(string $targetPath) @@ -694,11 +694,11 @@ public function base64pdf(): string { $command = $this->createPdfCommand(); - $pdf = $this->callBrowser($command); + $encodedPdf = $this->callBrowser($command); $this->cleanupTemporaryHtmlFile(); - return base64_encode($pdf); + return $encodedPdf; } public function evaluate(string $pageFunction): string From e007bf0517a527212c4d3cf40860b2a5aa79a932 Mon Sep 17 00:00:00 2001 From: Jeppe Knockaert Date: Wed, 21 Aug 2024 16:43:42 +0200 Subject: [PATCH 4/7] Add test that checks PDF contents --- .github/workflows/run-tests.yml | 3 +++ README.md | 12 ++++++++++++ composer.json | 1 + tests/PdfTest.php | 12 ++++++++++++ 4 files changed, 28 insertions(+) diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index 5f41fb5..842aeda 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -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: diff --git a/README.md b/README.md index 24cb1ff..04c048d 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/composer.json b/composer.json index 735ddb9..56355e9 100644 --- a/composer.json +++ b/composer.json @@ -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": { diff --git a/tests/PdfTest.php b/tests/PdfTest.php index 03c06b6..ca6ac3b 100644 --- a/tests/PdfTest.php +++ b/tests/PdfTest.php @@ -1,6 +1,7 @@ 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'; From 601f2758191d8c46b2ea587eea935a87da4f39e8 Mon Sep 17 00:00:00 2001 From: Jeppe Knockaert Date: Wed, 21 Aug 2024 17:41:50 +0200 Subject: [PATCH 5/7] Fix randomly failing test by blocking favicon --- tests/BrowsershotTest.php | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/tests/BrowsershotTest.php b/tests/BrowsershotTest.php index cbcae72..baf6df2 100644 --- a/tests/BrowsershotTest.php +++ b/tests/BrowsershotTest.php @@ -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); @@ -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/', ]]); From 172b713798471e65efdfe1743d442bf9ac0d8d88 Mon Sep 17 00:00:00 2001 From: freekmurze Date: Thu, 22 Aug 2024 09:14:56 +0000 Subject: [PATCH 6/7] Update CHANGELOG --- CHANGELOG.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 84c683a..29440ab 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) From 9682173cd46d3a1966e124f81193152c75363853 Mon Sep 17 00:00:00 2001 From: Ziaratban Date: Thu, 15 Aug 2024 16:47:12 +0330 Subject: [PATCH 7/7] fix --- bin/browser.cjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/browser.cjs b/bin/browser.cjs index 9d5b26d..532bb3c 100644 --- a/bin/browser.cjs +++ b/bin/browser.cjs @@ -161,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(), });