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