From fa0f63933f59042b9297a62449b8ac246de2cf21 Mon Sep 17 00:00:00 2001 From: "A. Alyusuf" Date: Sun, 17 Nov 2024 13:40:21 +0000 Subject: [PATCH 1/2] Save on disk using Browsershot save() directly --- src/PdfBuilder.php | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/PdfBuilder.php b/src/PdfBuilder.php index 0bcfd45..7be775a 100755 --- a/src/PdfBuilder.php +++ b/src/PdfBuilder.php @@ -10,6 +10,7 @@ use Spatie\LaravelPdf\Enums\Format; use Spatie\LaravelPdf\Enums\Orientation; use Spatie\LaravelPdf\Enums\Unit; +use Spatie\TemporaryDirectory\TemporaryDirectory; use Wnx\SidecarBrowsershot\BrowsershotLambda; class PdfBuilder implements Responsable @@ -257,10 +258,19 @@ public function disk(string $diskName, string $visibility = 'private'): self protected function saveOnDisk(string $diskName, string $path): self { - $pdfContent = $this->getBrowsershot()->pdf(); - $visibility = $this->visibility; + $fileName = pathinfo($path, PATHINFO_BASENAME); - Storage::disk($diskName)->put($path, $pdfContent, $visibility); + $temporaryDirectory = (new TemporaryDirectory)->create(); + + $this->getBrowsershot()->save($temporaryDirectory->path($fileName)); + + $content = file_get_contents($temporaryDirectory->path($fileName)); + + $temporaryDirectory->delete(); + + $visibility = $this->visibility; + + Storage::disk($diskName)->put($path, $content, $visibility); return $this; } From 58fb198108384750a8c3a36ff456d5deb7ef30f9 Mon Sep 17 00:00:00 2001 From: "A. Alyusuf" Date: Tue, 19 Nov 2024 08:28:33 +0000 Subject: [PATCH 2/2] Add png save test --- tests/PdfTest.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/tests/PdfTest.php b/tests/PdfTest.php index 003888a..b0193c2 100644 --- a/tests/PdfTest.php +++ b/tests/PdfTest.php @@ -157,3 +157,22 @@ expect($this->targetPath)->toContainText('hello'); }); + +it('can save as png in local and disk', function () { + Storage::fake('local'); + + $firstPath = getTempPath('first.png'); + Pdf::view('test') + ->save($firstPath); + + expect(mime_content_type($firstPath)) + ->toBe('image/png'); + + Pdf::view('test') + ->disk('local') + ->save('second.png'); + + expect(Storage::disk('local') + ->mimeType('second.png')) + ->toBe('image/png'); +});